Skip to content

Commit 2eeead1

Browse files
AyoubElkairhorns
authored andcommitted
re-enable and fix hydration for imperative routes.
The fix consists of passing a couple of new query params for imperative routes when building the client entrypoint url, the query params are then passed to the route table to enable dynamically adding a route for the imperative component.
1 parent 6ca3c6f commit 2eeead1

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

packages/fastify-renderer/src/node/renderers/react/ReactRenderer.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ export class ReactRenderer implements Renderer {
121121

122122
/** Given a node-land module id (path), return the build time path to the virtual script to hydrate the render client side */
123123
public buildVirtualClientEntrypointModuleID(route: RenderableRoute) {
124+
const queryParams = {
125+
layout: route.layout,
126+
base: route.base,
127+
...(route.isImperative && { route: route.url, component: route.renderable }),
128+
}
129+
124130
return (
125-
path.join(CLIENT_ENTRYPOINT_PREFIX, route.renderable, 'hydrate.jsx') +
126-
'?' +
127-
querystring.stringify({ layout: route.layout, base: route.base })
131+
path.join(CLIENT_ENTRYPOINT_PREFIX, route.renderable, 'hydrate.jsx') + '?' + querystring.stringify(queryParams)
128132
)
129133
}
130134

@@ -189,7 +193,6 @@ export class ReactRenderer implements Renderer {
189193
// push the props for the entrypoint to use when hydrating client side
190194
bus.push('tail', `<script type="module">window.__FSTR_PROPS=${JSON.stringify(render.props)};</script>`)
191195

192-
const entrypointName = this.buildVirtualClientEntrypointModuleID(render)
193196
// if we're in development, we just source the entrypoint directly from vite and let the browser do its thing importing all the referenced stuff
194197
if (this.plugin.devMode) {
195198
bus.push(
@@ -200,6 +203,7 @@ export class ReactRenderer implements Renderer {
200203
)}"></script>`
201204
)
202205
} else {
206+
const entrypointName = this.buildVirtualClientEntrypointModuleID(render)
203207
const manifestEntryName = normalizePath(path.relative(this.viteConfig.root, entrypointName))
204208
this.plugin.pushImportTagsFromManifest(bus, manifestEntryName)
205209
}
@@ -309,13 +313,16 @@ export class ReactRenderer implements Renderer {
309313
const entrypoint = id.replace(CLIENT_ENTRYPOINT_PREFIX, '/@fs/').replace(/\/hydrate\.jsx\?.+$/, '')
310314
const layout = url.searchParams.get('layout')!
311315
const base = url.searchParams.get('base')!
316+
const route = url.searchParams.get('route')!
317+
const component = url.searchParams.get('component')!
318+
const queryParams = { base, lazy: true, ...(component && { route, component }) }
312319

313320
return `
314321
// client side hydration entrypoint for a particular route generated by fastify-renderer
315322
import React from 'react'
316323
import ReactDOM from 'react-dom'
317324
import { routes } from ${JSON.stringify(
318-
ReactRenderer.ROUTE_TABLE_ID + '?' + querystring.stringify({ base, lazy: true })
325+
ReactRenderer.ROUTE_TABLE_ID + '?' + querystring.stringify(queryParams)
319326
)}
320327
import { Root } from ${JSON.stringify(this.clientModulePath)}
321328
import Layout from ${JSON.stringify(layout)}
@@ -411,9 +418,19 @@ export class ReactRenderer implements Renderer {
411418
const url = new URL('fstr://' + id)
412419
const lazy = !!url.searchParams.get('lazy')!
413420
const base = url.searchParams.get('base')!
421+
const route = url.searchParams.get('route')!
422+
const component = url.searchParams.get('component')!
414423
// We filter out the routes the imperatively renderable routes, which don't have a url property
415424
// There is no point in having them included in the route table
416425
const applicableRoutes = this.routes.filter((route) => route.base == base && route.url !== undefined)
426+
if (route) {
427+
const imperativeRoute = Object.assign(
428+
{},
429+
this.routes.find((route) => (route.renderable = component)),
430+
{ url: route }
431+
)
432+
applicableRoutes.push(imperativeRoute)
433+
}
417434
applicableRoutes.sort((a, b) => routeSortScore(a.url!) - routeSortScore(b.url!))
418435

419436
const pathsToModules = applicableRoutes.map((route) => [

0 commit comments

Comments
 (0)