Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Jan 17, 2025
1 parent 0149400 commit 32d8f97
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 21 additions & 12 deletions packages/react-hooks/src/react-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import React, { createContext, useContext } from 'react'
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector.js'

declare global {
var document: {
getElementById(id: string): { textContent?: string } | null
} | undefined
// eslint-disable-next-line
var document:
| {
getElementById(id: string): { textContent?: string } | null
}
| undefined
}

const isSSR = typeof globalThis !== 'undefined' && !('window' in globalThis)
const isSSR = typeof globalThis !== `undefined` && !(`window` in globalThis)

type UnknownShape = Shape<Row<unknown>>
type UnknownShapeStream = ShapeStream<Row<unknown>>
Expand All @@ -32,19 +35,22 @@ interface SSRShapeData<T extends Row<unknown>> {
}

interface SSRState {
// eslint-disable-next-line
shapes: { [key: string]: SSRShapeData<any> }
}

let ssrState: SSRState | undefined

function hydrateSSRState() {
if (typeof globalThis !== 'undefined' && globalThis.document) {
const scriptEl = globalThis.document.getElementById('__ELECTRIC_SSR_STATE__')
if (typeof globalThis !== `undefined` && globalThis.document) {
const scriptEl = globalThis.document.getElementById(
`__ELECTRIC_SSR_STATE__`
)
if (scriptEl?.textContent) {
try {
ssrState = JSON.parse(scriptEl.textContent)
} catch (e) {
console.error('Failed to parse SSR state:', e)
console.error(`Failed to parse SSR state:`, e)
}
}
}
Expand Down Expand Up @@ -201,7 +207,7 @@ function useShapes() {
// Only require provider during SSR
if (!shapes && isSSR) {
throw new Error(
'No ElectricProvider found. Wrap your app with ElectricProvider when using SSR.'
`No ElectricProvider found. Wrap your app with ElectricProvider when using SSR.`
)
}
return shapes || new Set()
Expand All @@ -214,6 +220,7 @@ function useTrackShape(optionsHash: string) {

// Function to serialize SSR state for ElectricScripts
function serializeSSRState(usedShapes: Set<string>): string {
// eslint-disable-next-line
const shapes: { [key: string]: SSRShapeData<any> } = {}

// Only get shapes that were used in this render
Expand All @@ -230,7 +237,7 @@ function serializeSSRState(usedShapes: Set<string>): string {
handle: shape.handle,
}
} catch (e) {
console.error('Failed to parse shape options:', e)
console.error(`Failed to parse shape options:`, e)
}
}

Expand All @@ -241,9 +248,11 @@ export function ElectricScripts() {
const shapes = useShapes()

// On client, reuse the server-rendered content
const content = !isSSR && globalThis.document
? globalThis.document.getElementById('__ELECTRIC_SSR_STATE__')?.textContent || ''
: serializeSSRState(shapes)
const content =
!isSSR && globalThis.document
? globalThis.document.getElementById(`__ELECTRIC_SSR_STATE__`)
?.textContent || ``
: serializeSSRState(shapes)

return (
<script
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-client/src/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Shape<T extends Row<unknown> = Row> {
initializeWithSSRData(data: Map<string, T>) {
// Clear existing data
this.#data.clear()

// Copy entries from SSR data
for (const [key, value] of data.entries()) {
this.#data.set(key, value)
Expand Down

0 comments on commit 32d8f97

Please sign in to comment.