Unverified Commit d64bc4c6 authored by Leah's avatar Leah Committed by GitHub

fix `react-server-dom-webpack` cache invalidation (#55287)

### What?
Webpack wrapped the external import in a new module, making `require.cache` invalidation impossible.

This also adds a basic fallback manifest for turbopack.

Closes WEB-1522
parent fe797c10
...@@ -28,10 +28,11 @@ ...@@ -28,10 +28,11 @@
# Tooling & Telemetry # Tooling & Telemetry
/packages/next/src/build/ @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling /packages/next/src/build/ @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/packages/next/src/telemetry/ @timneutkens @ijjk @shuding @padmaia /packages/next/src/server/lib/router-utils/setup-dev.ts @timneutkens @ijjk @shuding @huozhi @feedthejim @ztanner @wyattjoh @vercel/web-tooling
/packages/next-swc/ @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling /packages/next/src/telemetry/ @timneutkens @ijjk @shuding @padmaia
Cargo.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling /packages/next-swc/ @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
Cargo.lock @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling Cargo.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.cargo/config.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling Cargo.lock @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.config/nextest.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling /.cargo/config.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
/.config/nextest.toml @timneutkens @ijjk @shuding @huozhi @vercel/web-tooling
...@@ -21,9 +21,10 @@ export function useFlightResponse( ...@@ -21,9 +21,10 @@ export function useFlightResponse(
return flightResponseRef.current return flightResponseRef.current
} }
// react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly // react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly
const { const { createFromReadableStream } = process.env.NEXT_MINIMAL
createFromReadableStream, ? // @ts-ignore
} = require(`react-server-dom-webpack/client.edge`) __non_webpack_require__(`react-server-dom-webpack/client.edge`)
: require(`react-server-dom-webpack/client.edge`)
const [renderStream, forwardStream] = req.tee() const [renderStream, forwardStream] = req.tee()
const res = createFromReadableStream(renderStream, { const res = createFromReadableStream(renderStream, {
......
...@@ -103,7 +103,10 @@ import { ...@@ -103,7 +103,10 @@ import {
TurboPackConnectedAction, TurboPackConnectedAction,
} from '../../dev/hot-reloader-types' } from '../../dev/hot-reloader-types'
import { debounce } from '../../utils' import { debounce } from '../../utils'
import { deleteCache } from '../../../build/webpack/plugins/nextjs-require-cache-hot-reloader' import {
deleteAppClientCache,
deleteCache,
} from '../../../build/webpack/plugins/nextjs-require-cache-hot-reloader'
import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route' import { normalizeMetadataRoute } from '../../../lib/metadata/get-metadata-route'
const wsServer = new ws.Server({ noServer: true }) const wsServer = new ws.Server({ noServer: true })
...@@ -311,20 +314,18 @@ async function startWatcher(opts: SetupOpts) { ...@@ -311,20 +314,18 @@ async function startWatcher(opts: SetupOpts) {
async function processResult( async function processResult(
result: TurbopackResult<WrittenEndpoint> result: TurbopackResult<WrittenEndpoint>
): Promise<TurbopackResult<WrittenEndpoint>> { ): Promise<TurbopackResult<WrittenEndpoint>> {
for (const file of result.serverPaths const hasAppPaths = result.serverPaths.some((p) =>
.map((p) => path.join(distDir, p)) p.startsWith('server/app')
.concat([ )
// We need to clear the chunk cache in react
require.resolve( if (hasAppPaths) {
'next/dist/compiled/react-server-dom-webpack/cjs/react-server-dom-webpack-client.edge.development.js' deleteAppClientCache()
), }
// And this redirecting module as well
require.resolve( for (const file of result.serverPaths.map((p) => path.join(distDir, p))) {
'next/dist/compiled/react-server-dom-webpack/client.edge.js'
),
])) {
deleteCache(file) deleteCache(file)
} }
return result return result
} }
...@@ -337,6 +338,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -337,6 +338,7 @@ async function startWatcher(opts: SetupOpts) {
moduleTrace?: Array<{ moduleName: string }> moduleTrace?: Array<{ moduleName: string }>
stack?: string stack?: string
} }
const errors = new Map<string, HmrError>() const errors = new Map<string, HmrError>()
for (const [, issueMap] of issues) { for (const [, issueMap] of issues) {
for (const [key, issue] of issueMap) { for (const [key, issue] of issueMap) {
...@@ -380,8 +382,6 @@ async function startWatcher(opts: SetupOpts) { ...@@ -380,8 +382,6 @@ async function startWatcher(opts: SetupOpts) {
sendHmrDebounce() sendHmrDebounce()
} }
const clearCache = (filePath: string) => deleteCache(filePath)
async function loadPartialManifest<T>( async function loadPartialManifest<T>(
name: string, name: string,
pageName: string, pageName: string,
...@@ -482,6 +482,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -482,6 +482,7 @@ async function startWatcher(opts: SetupOpts) {
if (payload) sendHmr('endpoint-change', page, payload) if (payload) sendHmr('endpoint-change', page, payload)
} }
} }
function clearChangeSubscription(page: string) { function clearChangeSubscription(page: string) {
const subscription = changeSubscriptions.get(page) const subscription = changeSubscriptions.get(page)
if (subscription) { if (subscription) {
...@@ -583,6 +584,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -583,6 +584,7 @@ async function startWatcher(opts: SetupOpts) {
currentEntriesHandlingResolve = undefined currentEntriesHandlingResolve = undefined
} }
} }
handleEntries().catch((err) => { handleEntries().catch((err) => {
console.error(err) console.error(err)
process.exit(1) process.exit(1)
...@@ -665,7 +667,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -665,7 +667,7 @@ async function startWatcher(opts: SetupOpts) {
async function writeBuildManifest(): Promise<void> { async function writeBuildManifest(): Promise<void> {
const buildManifest = mergeBuildManifests(buildManifests.values()) const buildManifest = mergeBuildManifests(buildManifests.values())
const buildManifestPath = path.join(distDir, BUILD_MANIFEST) const buildManifestPath = path.join(distDir, BUILD_MANIFEST)
await clearCache(buildManifestPath) deleteCache(buildManifestPath)
await writeFile( await writeFile(
buildManifestPath, buildManifestPath,
JSON.stringify(buildManifest, null, 2), JSON.stringify(buildManifest, null, 2),
...@@ -696,12 +698,30 @@ async function startWatcher(opts: SetupOpts) { ...@@ -696,12 +698,30 @@ async function startWatcher(opts: SetupOpts) {
) )
} }
async function writeFallbackBuildManifest(): Promise<void> {
const fallbackBuildManifest = mergeBuildManifests(
[buildManifests.get('_app'), buildManifests.get('_error')].filter(
Boolean
) as BuildManifest[]
)
const fallbackBuildManifestPath = path.join(
distDir,
`fallback-${BUILD_MANIFEST}`
)
deleteCache(fallbackBuildManifestPath)
await writeFile(
fallbackBuildManifestPath,
JSON.stringify(fallbackBuildManifest, null, 2),
'utf-8'
)
}
async function writeAppBuildManifest(): Promise<void> { async function writeAppBuildManifest(): Promise<void> {
const appBuildManifest = mergeAppBuildManifests( const appBuildManifest = mergeAppBuildManifests(
appBuildManifests.values() appBuildManifests.values()
) )
const appBuildManifestPath = path.join(distDir, APP_BUILD_MANIFEST) const appBuildManifestPath = path.join(distDir, APP_BUILD_MANIFEST)
await clearCache(appBuildManifestPath) deleteCache(appBuildManifestPath)
await writeFile( await writeFile(
appBuildManifestPath, appBuildManifestPath,
JSON.stringify(appBuildManifest, null, 2), JSON.stringify(appBuildManifest, null, 2),
...@@ -712,7 +732,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -712,7 +732,7 @@ async function startWatcher(opts: SetupOpts) {
async function writePagesManifest(): Promise<void> { async function writePagesManifest(): Promise<void> {
const pagesManifest = mergePagesManifests(pagesManifests.values()) const pagesManifest = mergePagesManifests(pagesManifests.values())
const pagesManifestPath = path.join(distDir, 'server', PAGES_MANIFEST) const pagesManifestPath = path.join(distDir, 'server', PAGES_MANIFEST)
await clearCache(pagesManifestPath) deleteCache(pagesManifestPath)
await writeFile( await writeFile(
pagesManifestPath, pagesManifestPath,
JSON.stringify(pagesManifest, null, 2), JSON.stringify(pagesManifest, null, 2),
...@@ -727,7 +747,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -727,7 +747,7 @@ async function startWatcher(opts: SetupOpts) {
'server', 'server',
APP_PATHS_MANIFEST APP_PATHS_MANIFEST
) )
await clearCache(appPathsManifestPath) deleteCache(appPathsManifestPath)
await writeFile( await writeFile(
appPathsManifestPath, appPathsManifestPath,
JSON.stringify(appPathsManifest, null, 2), JSON.stringify(appPathsManifest, null, 2),
...@@ -743,7 +763,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -743,7 +763,7 @@ async function startWatcher(opts: SetupOpts) {
distDir, distDir,
'server/middleware-manifest.json' 'server/middleware-manifest.json'
) )
await clearCache(middlewareManifestPath) deleteCache(middlewareManifestPath)
await writeFile( await writeFile(
middlewareManifestPath, middlewareManifestPath,
JSON.stringify(middlewareManifest, null, 2), JSON.stringify(middlewareManifest, null, 2),
...@@ -759,7 +779,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -759,7 +779,7 @@ async function startWatcher(opts: SetupOpts) {
'server', 'server',
NEXT_FONT_MANIFEST + '.json' NEXT_FONT_MANIFEST + '.json'
) )
await clearCache(fontManifestPath) deleteCache(fontManifestPath)
await writeFile( await writeFile(
fontManifestPath, fontManifestPath,
JSON.stringify( JSON.stringify(
...@@ -780,7 +800,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -780,7 +800,7 @@ async function startWatcher(opts: SetupOpts) {
distDir, distDir,
'react-loadable-manifest.json' 'react-loadable-manifest.json'
) )
await clearCache(loadableManifestPath) deleteCache(loadableManifestPath)
await writeFile( await writeFile(
loadableManifestPath, loadableManifestPath,
JSON.stringify({}, null, 2), JSON.stringify({}, null, 2),
...@@ -847,6 +867,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -847,6 +867,7 @@ async function startWatcher(opts: SetupOpts) {
await currentEntriesHandling await currentEntriesHandling
await writeBuildManifest() await writeBuildManifest()
await writeAppBuildManifest() await writeAppBuildManifest()
await writeFallbackBuildManifest()
await writePagesManifest() await writePagesManifest()
await writeAppPathsManifest() await writeAppPathsManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
...@@ -1008,6 +1029,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1008,6 +1029,7 @@ async function startWatcher(opts: SetupOpts) {
await loadPagesManifest('_error') await loadPagesManifest('_error')
await writeBuildManifest() await writeBuildManifest()
await writeFallbackBuildManifest()
await writePagesManifest() await writePagesManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeOtherManifests() await writeOtherManifests()
...@@ -1118,6 +1140,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1118,6 +1140,7 @@ async function startWatcher(opts: SetupOpts) {
} }
await writeBuildManifest() await writeBuildManifest()
await writeFallbackBuildManifest()
await writePagesManifest() await writePagesManifest()
await writeMiddlewareManifest() await writeMiddlewareManifest()
await writeOtherManifests() await writeOtherManifests()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment