Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rsc/rsa): Use "model" naming #11522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading