Skip to content

Commit

Permalink
__rwjs__router
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed May 11, 2024
1 parent 1131b9c commit 9e65cc9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/router/src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function normalizePage(
LazyComponent: specOrPage,
}

console.log('normalizePage spec', specOrPage)
console.log('normalizePage spec', spec)

return spec
}
Expand Down
20 changes: 17 additions & 3 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cache, use, useEffect, useState } from 'react'
import React, { cache, use, useEffect, useState } from 'react'
import type { ReactElement } from 'react'

import type { Options } from 'react-server-dom-webpack/client'
Expand All @@ -23,15 +23,29 @@ const BASE_PATH = '/rw-rsc/'
export function renderFromRscServer<TProps>(rscId: string) {
console.log('serve rscId (renderFromRscServer)', rscId)

console.log('window', typeof window)

// TODO (RSC): Remove this when we have a babel plugin to call another
// function during SSR
if (typeof window === 'undefined') {
// Temporarily skip rendering this component during SSR
return null
}

if (rscId === 'NavigationLayout') {
console.log('renderFromRscServer - NavigationLayout')

// @ts-expect-error I don't care about types, this is just for debugging
return function HardcodedNavigationLayout(props) {
return React.createElement('div', {}, [
'Hardcoded NavigationLayout',
React.createElement(
'pre',
{},
React.createElement('code', {}, JSON.stringify(props, null, 2)),
),
])
}
}

type SetRerender = (
rerender: (next: [Thenable<ReactElement>, string]) => void,
) => () => void
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/lib/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function getEntries() {

entries['NavigationLayout'] =
getPaths().web.layouts + '/NavigationLayout/NavigationLayout.tsx'
entries['__rwjs__Router'] = getPaths().web.src + '/ServerRoutes.tsx'

// Add the ServerEntry entry, noting we use the "__rwjs__" prefix to avoid
// any potential conflicts with user-defined entries
Expand Down
12 changes: 12 additions & 0 deletions packages/vite/src/rsc/rscWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,18 @@ async function renderRsc(input: RenderInput): Promise<PipeableStream> {
// continue for mutation mode
}

if (input.rscId?.startsWith('__rwjs__router')) {
console.log('render Router rscId', input.rscId)
const router = await getFunctionComponent('__rwjs__Router')
console.log('render Router router', router)

return renderToPipeableStream(
createElement(router, { location: { pathname: '/about' } }),
// createElement('div', {}, 'AboutPage'),
bundlerConfig,
).pipe(transformRsfId(config.root))
}

if (input.rscId && input.props) {
const component = await getFunctionComponent(input.rscId)

Expand Down

0 comments on commit 9e65cc9

Please sign in to comment.