From ab2d6864c5cc3e3b7fdddc1922b30857f2f3e3a0 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:35:06 +0900 Subject: [PATCH] fix(plugin-utils): count every Next route file in the compiled set computeCompiledFiles, the Next plugin's base CSS completion set, only started from App Router page files and added layouts and other segment files when a page sat below them. A Pages Router project therefore got an empty set, and the coordinator served its base CSS on the idle heuristic, which depends on traffic timing. Route handlers, a parallel-route default and a root not-found without a page were left out of App Router projects too. The compiled set now starts from every App Router route file (page, route, layout, template, default, loading, error, not-found, forbidden, unauthorized, global-error, global-not-found) and every Pages Router module under pages/, _app and _document included. computeFileRoutes, which drives atom hoisting, still counts only App Router pages. Refs #694 Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_next_route_entries.json | 1 + .../plugin-utils/src/import-graph.test.ts | 49 +++++++++++++++++++ packages/plugin-utils/src/import-graph.ts | 31 +++++++----- 3 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 .changepacks/changepack_log_next_route_entries.json diff --git a/.changepacks/changepack_log_next_route_entries.json b/.changepacks/changepack_log_next_route_entries.json new file mode 100644 index 000000000..ce1422851 --- /dev/null +++ b/.changepacks/changepack_log_next_route_entries.json @@ -0,0 +1 @@ +{"changes":{"packages/next-plugin/package.json":"Patch","packages/plugin-utils/package.json":"Patch"},"note":"Count Pages Router modules and every App Router route file as Next route entries, so Pages Router projects get a deterministic CSS completion set","date":"2026-11-15T00:00:00.000Z"} diff --git a/packages/plugin-utils/src/import-graph.test.ts b/packages/plugin-utils/src/import-graph.test.ts index 7f0069c24..d1143afd8 100644 --- a/packages/plugin-utils/src/import-graph.test.ts +++ b/packages/plugin-utils/src/import-graph.test.ts @@ -853,6 +853,55 @@ describe('computeCompiledFiles', () => { expect(compiled).not.toContain('src/orphan.tsx') }) + it('treats every Pages Router module as a route entry', () => { + writeFixture('src/pages/_app.tsx', "import '../shell'\n") + writeFixture('src/pages/_document.tsx', 'export default 1\n') + writeFixture('src/pages/index.tsx', 'export default 1\n') + writeFixture('src/pages/blog/[slug].tsx', "import '../../post'\n") + writeFixture('src/pages/api/hello.ts', 'export default 1\n') + writeFixture('src/shell.tsx', 'export const S = 1\n') + writeFixture('src/post.tsx', 'export const P = 1\n') + writeFixture('src/orphan.tsx', 'export const O = 1\n') + + expect(computeCompiledFiles({ cwd, srcDir })).toEqual([ + 'src/pages/_app.tsx', + 'src/pages/_document.tsx', + 'src/pages/api/hello.ts', + 'src/pages/blog/[slug].tsx', + 'src/pages/index.tsx', + 'src/post.tsx', + 'src/shell.tsx', + ]) + }) + + it('finds a Pages Router at the project root', () => { + writeFixture('pages/about.jsx', "import './about-body'\n") + writeFixture('pages/about-body.jsx', 'export const B = 1\n') + + expect(computeCompiledFiles({ cwd, srcDir: cwd })).toEqual([ + 'pages/about-body.jsx', + 'pages/about.jsx', + ]) + }) + + it('includes App Router files no page sits below', () => { + writeFixture('src/app/api/route.ts', "import '../../handler'\n") + writeFixture('src/app/@modal/default.tsx', "import '../../modal'\n") + writeFixture('src/app/not-found.tsx', 'export default 1\n') + writeFixture('src/app/global-error.tsx', 'export default 1\n') + writeFixture('src/handler.ts', 'export const H = 1\n') + writeFixture('src/modal.tsx', 'export const M = 1\n') + + expect(computeCompiledFiles({ cwd, srcDir })).toEqual([ + 'src/app/@modal/default.tsx', + 'src/app/api/route.ts', + 'src/app/global-error.tsx', + 'src/app/not-found.tsx', + 'src/handler.ts', + 'src/modal.tsx', + ]) + }) + it('returns an empty list when no leaf route exists', () => { writeFixture('src/index.tsx', "import './a'\n") writeFixture('src/a.tsx', 'export const A = 1\n') diff --git a/packages/plugin-utils/src/import-graph.ts b/packages/plugin-utils/src/import-graph.ts index 9bf15167e..fb147bdea 100644 --- a/packages/plugin-utils/src/import-graph.ts +++ b/packages/plugin-utils/src/import-graph.ts @@ -78,6 +78,19 @@ const testFileRegex = /\.(?:test|spec)\.[mc]?[jt]sx?$/ const routeFileRegex = /(^|\/)(page|layout|template|default|loading|error|not-found|global-error)\.(tsx|ts|jsx|js)$/ const leafRouteFileRegex = /(^|\/)page\.(tsx|ts|jsx|js)$/ +// Every App Router file Next compiles as part of a route tree: pages, route +// handlers, and each segment file, including the ones (parallel-route +// `default`, root `not-found`) no page sits below. +const appRouterFileRegex = + /(^|\/)(page|route|layout|template|default|loading|error|not-found|forbidden|unauthorized|global-error|global-not-found)\.(tsx|ts|jsx|js)$/ +// Every module in the Pages Router directory is a route, `_app`, `_document` +// and `_error` included. +const pagesRouterFileRegex = /^pages\/.+\.(tsx|ts|jsx|js)$/ + +/** Whether Next compiles this `srcDir`-relative file as a route entry. */ +function isCompiledRouteEntry(relPath: string): boolean { + return appRouterFileRegex.test(relPath) || pagesRouterFileRegex.test(relPath) +} let cachedOxcParser: false | OxcParser | undefined @@ -292,8 +305,10 @@ export interface ComputeCompiledFilesOptions { /** * Every source file the bundler will compile for the app's routes: the closure - * of all leaf routes (plus their ancestor route shells) over BOTH static and - * dynamic `import()` edges. + * of every route entry over BOTH static and dynamic `import()` edges. Route + * entries are all App Router route files (pages, route handlers, layouts and + * the other segment files) and every Pages Router module (`pages/**`, + * `_app`/`_document` included). * * This is deliberately WIDER than `computeFileRoutes`, whose closure stops at * static edges because a lazily-loaded file belongs to its own chunk for @@ -305,7 +320,7 @@ export interface ComputeCompiledFilesOptions { * * Keys are POSIX paths relative to `cwd`, matching the extraction filename the * loader posts. Returns a sorted array (deterministic across runs). Empty when - * no leaf route is detected, which keeps callers on their idle fallback rather + * no route entry is detected, which keeps callers on their idle fallback rather * than blocking on a set that can never complete. */ export function computeCompiledFiles( @@ -323,16 +338,10 @@ export function computeCompiledFiles( allImports.set(file, edges) } - const routeShellFilesByDir = getRouteShellFilesByDir(files, srcDir) const compiled = new Set() for (const file of files) { - if (!leafRouteFileRegex.test(toPosixRelative(srcDir, file))) continue - for (const reached of getLeafRouteClosure( - file, - srcDir, - allImports, - routeShellFilesByDir, - )) { + if (!isCompiledRouteEntry(toPosixRelative(srcDir, file))) continue + for (const reached of getStaticClosure(file, allImports)) { compiled.add(reached) } }