Stale Cached Page List On First Client Navigation After Rewrite In Pages Router Dev
In Next.js Pages Router development with Turbopack, when a new page is materialized on demand and a client-side navigation is rewritten via proxy.ts to that new page, the client uses a stale build manifest. This causes route-loader.loadRoute to fail with 'Expected component ... to be defined' even though the server can already serve the rewritten page. A hard refresh works because the fresh manifest is loaded.
The client build manifest (route map) is cached after the initial page load. When a new route is created later (e.g., via proxy.ts rewrite), the cached manifest is not invalidated. During client-side navigation, the data JSON returns a 'page' field pointing to the new route (e.g., /docs/_handlers/example). The route-loader looks up that route in the stale manifest, finds no entry, and throws the error.
1. Clone the reproduction repository: https://github.com/gurkerl83/next-pages-client-stale-manifest-repro
2. Install dependencies with `pnpm install`.
3. Start the dev server with `pnpm dev`.
4. Open `/docs/start` in the browser.
5. Confirm the handler route is shown as `missing`.
6. Click `Materialize example`.
7. Click `Go to example` to trigger client-side navigation.
8. Observe the request is rewritten by proxy.ts from `/docs/example` to `/docs/_handlers/example`.
9. Observe the browser throws `Expected component 'RewrittenOnlyComponent' to be defined`.
10. Hard-refresh `/docs/example` and observe the page renders correctly.
Fixing Code Block
--- a/packages/next/src/client/route-loader.ts
+++ b/packages/next/src/client/route-loader.ts
@@ -123,6 +123,10 @@ export class RouteLoader {
return this.clientBuildManifest
}
+ private clearClientBuildManifest() {
+ this.clientBuildManifest = undefined
+ }
+
async loadRoute(
route: string,
prefetch?: boolean,
@@ -132,21 +136,28 @@ export class RouteLoader {
if (this.routeInfo[route]) {
return this.routeInfo[route]
}
- const clientBuildManifest = await this.getClientBuildManifest()
- const pageFiles = clientBuildManifest[route]
+ let clientBuildManifest = await this.getClientBuildManifest()
+ let pageFiles = clientBuildManifest[route]
+
+ // In development, new routes can be added after the initial manifest
+ // load (e.g. by proxy rewrites that materialize handler pages on demand).
+ // If the route is missing, force a one-time refresh of the manifest.
+ if (!pageFiles && process.env.NODE_ENV !== 'production') {
+ this.clearClientBuildManifest()
+ clientBuildManifest = await this.getClientBuildManifest()
+ pageFiles = clientBuildManifest[route]
+ }
if (!pageFiles) {
throw new Error(`Expected component '${route}' to be defined`)
}
The patch adds a method to clear the cached client build manifest and modifies loadRoute to retry manifest fetch once when a route is missing in development. This allows the client to discover newly materialized routes created by dev-time rewrites without requiring a full page refresh. In production, the logic is skipped to avoid unnecessary network requests.
Edge Case Audit
This fix only affects development mode and adds one extra manifest fetch when a route is not initially found. Potential risks: (1) concurrent loadRoute calls for the same missing route may trigger multiple manifest fetches, but this is benign. (2) If the manifest fetch fails during the retry, the original error is still thrown. Rollback is straightforward: revert the patch or remove the clearClientBuildManifest call.