-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
139 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"use client" | ||
|
||
import { cacheShapeState } from "@electric-sql/react" | ||
import { getUrl, SerializedShape } from "./utils" | ||
|
||
export default function ClientShapeProvider({ | ||
children, | ||
serializedShapes, | ||
}: { | ||
children: React.JSX.Element | ||
serializedShapes: SerializedShape[] | ||
}) { | ||
for (const { options, data } of serializedShapes) { | ||
const newShapeOptions = { ...options, url: getUrl() } | ||
cacheShapeState(newShapeOptions, data) | ||
} | ||
return children | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,16 @@ | ||
import React from "react" | ||
import { getSerializedShape } from "@electric-sql/react" | ||
import Home from "./Home" | ||
import SSRShapesInitializer from "./ssr-shapes-provider" | ||
import ServerShapeProvider from "./server-shape-provider" | ||
import { ShapeDefintion } from "./utils" | ||
|
||
const serverOptions = { | ||
url: new URL(`http://localhost:3000/v1/shape/items`).href, | ||
const itemsShape: ShapeDefintion = { | ||
table: `items`, | ||
} | ||
|
||
const Page = async () => { | ||
const data = getSerializedShape(serverOptions) | ||
|
||
return ( | ||
<SSRShapesInitializer serializedShapes={[{ data, serverOptions }]}> | ||
<Home /> | ||
</SSRShapesInitializer> | ||
) | ||
} | ||
const Page = async () => ( | ||
<ServerShapeProvider options={[itemsShape]}> | ||
<Home /> | ||
</ServerShapeProvider> | ||
) | ||
|
||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { getSerializedShape } from "@electric-sql/react" | ||
import React from "react" | ||
import ClientShapeProvider from "./client-shapes-provider" | ||
import { getUrl, SerializedShape, ShapeDefintion } from "./utils" | ||
|
||
export default function ServerShapeProvider({ | ||
children, | ||
options, | ||
}: { | ||
children: React.JSX.Element | ||
options: ShapeDefintion[] | ||
}) { | ||
const clientShapes: SerializedShape[] = [] | ||
for (const shapeOptions of options) { | ||
const serializedShape = getSerializedShape({ | ||
...shapeOptions, | ||
url: getUrl(), | ||
}) | ||
|
||
const clientOptions = { | ||
table: shapeOptions.table, | ||
columns: shapeOptions.columns, | ||
where: shapeOptions.where, | ||
shapeHandle: serializedShape.shapeHandle, | ||
offset: serializedShape.offset, | ||
} | ||
|
||
clientShapes.push({ | ||
options: clientOptions, | ||
data: serializedShape.data ?? new Map(), | ||
}) | ||
} | ||
|
||
return ( | ||
<ClientShapeProvider serializedShapes={clientShapes}> | ||
{children} | ||
</ClientShapeProvider> | ||
) | ||
} |
8 changes: 2 additions & 6 deletions
8
examples/nextjs-ssr-example/app/shape-proxy/[...table]/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
Offset, | ||
Shape, | ||
ShapeData, | ||
ShapeStreamOptions, | ||
} from "@electric-sql/client/*" | ||
|
||
export type ShapeDefintion = { | ||
table: string | ||
columns?: string[] | ||
where?: string | ||
} | ||
|
||
export type ShapeDefinitionWithPosiotion = ShapeDefintion & { | ||
offset: Offset | ||
shapeHandle: string | undefined | ||
} | ||
|
||
export type SerializedShape = { | ||
options: ShapeDefinitionWithPosiotion | ||
data: ShapeData | ||
} | ||
|
||
export function getUrl() { | ||
if (typeof window === `undefined`) { | ||
return `${process.env.ELECTRIC_URL || `http://localhost:3000`}/v1/shape` | ||
} | ||
return `${window?.location.origin}/shape-proxy/v1/shape` | ||
} | ||
|
||
export function getProxiedOptions( | ||
options: Omit<ShapeStreamOptions, "url"> | ||
): ShapeStreamOptions { | ||
// ensure shape is not syncing on the server | ||
const serverOptions: Partial<ShapeStreamOptions> = {} | ||
if (typeof window === `undefined`) { | ||
const controller = new AbortController() | ||
controller.abort() | ||
serverOptions.signal = controller.signal | ||
serverOptions.subscribe = false | ||
} | ||
|
||
return { ...options, ...serverOptions, url: getUrl() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters