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

Use proxy to ensure Flight is referencing to the latest module during development #43823

Merged
merged 3 commits into from Dec 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/next/client/app-index.tsx
Expand Up @@ -30,10 +30,23 @@ __webpack_require__.u = (chunkId: any) => {
// Ignore the module ID transform in client.
// eslint-disable-next-line no-undef
// @ts-expect-error TODO: fix type
self.__next_require__ = (id: string) => {
const modId = id.replace(/\?.+$/, '')
return __webpack_require__(modId)
}
self.__next_require__ =
process.env.NODE_ENV !== 'production'
? (id: string) => {
const mod = __webpack_require__(id)
if (typeof mod === 'object') {
// Return a proxy to flight client to make sure it's always getting
// the latest module, instead of being cached.
return new Proxy(mod, {
get(_target, prop) {
return __webpack_require__(id)[prop]
},
})
}

return mod
}
: __webpack_require__

// eslint-disable-next-line no-undef
;(self as any).__next_chunk_load__ = (chunk: string) => {
Expand Down
26 changes: 1 addition & 25 deletions packages/next/server/app-render.tsx
Expand Up @@ -1601,35 +1601,11 @@ export async function renderToHTMLOrFlight(
).slice(1),
]

const serverComponentManifestWithHMR = dev
? new Proxy(serverComponentManifest, {
get: (target, prop) => {
if (
typeof prop === 'string' &&
!prop.startsWith('_') &&
target[prop]
) {
// Attach TS (timestamp) query param to IDs to get rid of flight client's module cache on HMR.
const namedExports: any = {}
const ts = Date.now()
for (let key in target[prop]) {
namedExports[key] = {
...target[prop][key],
id: `${target[prop][key].id}?ts=${ts}`,
}
}
return namedExports
}
return target[prop]
},
})
: serverComponentManifest

// For app dir, use the bundled version of Fizz renderer (renderToReadableStream)
// which contains the subset React.
const readable = ComponentMod.renderToReadableStream(
flightData,
serverComponentManifestWithHMR,
serverComponentManifest,
{
context: serverContexts,
onError: flightDataRendererErrorHandler,
Expand Down