-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialise shapes with server data automatically
- Loading branch information
Showing
4 changed files
with
71 additions
and
34 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 |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import React from "react" | ||
import { getSerializedShape } from "@electric-sql/react" | ||
import Home from "./Home" | ||
import SSRShapesInitializer from "./ssr-shapes-provider" | ||
|
||
const serverShapeOptions = { | ||
const serverOptions = { | ||
url: new URL(`http://localhost:3000/v1/shape/items`).href, | ||
} | ||
|
||
const Page = async () => { | ||
return <Home shapes={{ items: getSerializedShape(serverShapeOptions) }} /> | ||
const data = getSerializedShape(serverOptions) | ||
|
||
return ( | ||
<SSRShapesInitializer serializedShapes={[{ data, serverOptions }]}> | ||
<Home /> | ||
</SSRShapesInitializer> | ||
) | ||
} | ||
|
||
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,43 @@ | ||
"use client" | ||
|
||
import { createContext } from "react" | ||
import { SerializedShapeData, useShape } from "@electric-sql/react" | ||
import { ShapeStreamOptions } from "@electric-sql/client/*" | ||
|
||
export const SSRShapesContext = createContext({}) | ||
|
||
type SerializedShape = { | ||
serverOptions: ShapeStreamOptions | ||
data: SerializedShapeData | ||
} | ||
|
||
const getClientBaseUrl = () => window?.location.origin | ||
|
||
export default function SSRShapesInitializer({ | ||
children, | ||
serializedShapes, | ||
}: { | ||
children: React.ReactNode | ||
serializedShapes: SerializedShape[] | ||
}) { | ||
if (typeof window === `undefined`) { | ||
return children | ||
} | ||
|
||
for (const { serverOptions, data } of serializedShapes) { | ||
// FIX client url | ||
const clientUrl = new URL(`/shape-proxy/items`, getClientBaseUrl()).href | ||
const shapeOptions = { | ||
...serverOptions, | ||
url: clientUrl, | ||
offset: data.offset, | ||
shapeId: data.shapeId, | ||
} | ||
|
||
const shapeData = new Map(Object.entries(data.data ?? new Map())) | ||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
useShape({ ...shapeOptions, shapeData }) | ||
} | ||
|
||
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