Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
feat(react): ensure state can be modified in SSR phase
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez committed Jun 30, 2022
1 parent 95603f0 commit fdc6a10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/fastify-dx-react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function createHtmlFunction (source, scope, config) {
? onShellReady(body)
: onAllReady(body)
),
head: headTemplate({ ...context, head, hydration }),
head: headTemplate({ ...context, head }),
footer: () => footerTemplate({
...context,
// Decide whether or not to include the hydration script
Expand Down
15 changes: 10 additions & 5 deletions packages/fastify-dx-react/virtual/core.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { proxy, useSnapshot } from 'valtio'
import { waitResource, waitFetch } from '/dx:resource.js'
import layouts from '/dx:layouts.js'

const isServer = typeof process === 'object'

export const isServer = import.meta.env.SSR
export const Router = isServer ? StaticRouter : BrowserRouter
export const RouteContext = createContext({})

export function useRouteContext () {
const routeContext = useContext(RouteContext)
if (routeContext.state) {
routeContext.snapshot = useSnapshot(routeContext.state)
routeContext.snapshot = isServer
? routeContext.state
: useSnapshot(routeContext.state)
}
return routeContext
}
Expand Down Expand Up @@ -56,7 +57,9 @@ export function DXRoute ({ head, ctxHydration, ctx, children }) {
<RouteContext.Provider value={{
...ctx,
...ctxHydration,
state: proxy(ctxHydration.state),
state: isServer
? ctxHydration.state
: proxy(ctxHydration.state),
}}>
<Layout>
{children}
Expand Down Expand Up @@ -130,7 +133,9 @@ export function DXRoute ({ head, ctxHydration, ctx, children }) {
<RouteContext.Provider value={{
...ctxHydration,
...ctx,
state: proxy(ctxHydration.state),
state: isServer
? ctxHydration.state
: proxy(ctxHydration.state),
}}>
<Layout>
{children}
Expand Down

0 comments on commit fdc6a10

Please sign in to comment.