Skip to content

Commit

Permalink
chore(rsc/rsa): Use "model" naming (redwoodjs#11522)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Sep 10, 2024
1 parent 5300d8d commit 3b45827
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
25 changes: 11 additions & 14 deletions packages/router/src/rsc/RscRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,41 +108,38 @@ function rscFetchRoutes(serializedProps: string) {
// I'm not sure this recursive use of `options` is needed. I briefly
// tried without it, and things seemed to work. But keeping it for
// now, until we learn more.
const dataPromise = createFromFetch(responsePromise, options)
const modelPromise = createFromFetch(responsePromise, options)

// TODO (RSC): This is where we want to update the RSA cache, but first we
// need to normalize the data that comes back from the server. We need to
// always send an object with a `__rwjs__rsa_data` key and some key
// for the flight data
// rscCache.set(rscCacheKey, dataPromise)

const dataValue = await dataPromise
console.log('RscRoutes :: callServer dataValue', dataValue)
const model = await modelPromise

// TODO (RSC): Fix the types for `createFromFetch`
// @ts-expect-error The type is wrong for createFromFetch
const Routes = dataValue.Routes?.[0]
console.log('Routes', Routes)

rscCache.set(rscCacheKey, Promise.resolve(Routes))
rscCache.set(rscCacheKey, Promise.resolve(model.__rwjs__Routes?.[0]))

// TODO (RSC): Fix the types for `createFromFetch`
// @ts-expect-error The type is wrong for createFromFetch. It can really
// return anything, not just React.ReactElement. It all depends on what
// the server sends back.
return dataValue.__rwjs__rsa_data
return model.__rwjs__rsa_data
},
}

const componentPromise = createFromFetch<never, React.ReactElement>(
const modelPromise = createFromFetch<never, React.ReactElement>(
responsePromise,
options,
)

rscCache.set(rscCacheKey, componentPromise)
rscCache.set(rscCacheKey, modelPromise)

// TODO (RSC): Figure out if this is ever used, or if it's better to return
// the cache key
return componentPromise
return modelPromise
}

interface Props {
Expand Down Expand Up @@ -177,11 +174,11 @@ export const RscRoutes = ({ routesProps }: Props) => {
console.log('RscRoutes :: current props\n routesProps: ' + serializedProps)
console.log('RscRoutes :: rendering cache entry for\n' + currentRscCacheKey)

const component = rscCache.get(currentRscCacheKey)
const rscModelPromise = rscCache.get(currentRscCacheKey)

if (!component) {
if (!rscModelPromise) {
throw new Error('Missing RSC cache entry for ' + currentRscCacheKey)
}

return use(component)
return use(rscModelPromise)
}
9 changes: 7 additions & 2 deletions packages/vite/src/rsc/rscRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ function getBundlerConfig() {
return bundlerConfig
}

interface RscModel {
__rwjs__Routes: React.ReactElement
__rwjs__rsa_data?: unknown
}

async function renderRsc(input: RenderInput): Promise<ReadableStream> {
if (input.rsaId || !input.args) {
throw new Error(
Expand Down Expand Up @@ -183,8 +188,8 @@ async function executeRsa(input: RenderInput): Promise<ReadableStream> {

const serverRoutes = await getRoutesComponent()
console.log('rscRenderer.ts executeRsa serverRoutes', serverRoutes)
const model = {
Routes: createElement(serverRoutes, {
const model: RscModel = {
__rwjs__Routes: createElement(serverRoutes, {
location: { pathname: '/', search: '' },
}),
__rwjs__rsa_data: data,
Expand Down

0 comments on commit 3b45827

Please sign in to comment.