Unverified Commit c7a5a095 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Revert "Revert "refactor: write pages router export files directly to server/pages/"" (#93409)

Re-apply #92735

Before re-landing we need to investigate why the pages router middleware
i18n test fails when deployed.

Update: Investigation/fix:
https://github.com/vercel/next.js/pull/92735#issuecomment-4242978559

Reverts vercel/next.js#92733

---

Cursor plan based on the investigation Claude did:

# Fix i18n export orphan HTML files in server/pages/

## Problem

When i18n is configured, the export path map in `build/index.ts` creates
locale-prefixed entries for every page (e.g., `/en/home/a`,
`/fr/home/a`), but only deletes the non-locale entry from `defaultMap`
for SSG pages. For non-SSG static pages, the non-locale entry (e.g.,
`/home/a`) remains, causing the export worker to render and write orphan
HTML files directly to `server/pages/`.

These orphan files interfere with routing on deployment platforms (e.g.,
a `[teamId]/[slug].html` orphan matches rewritten paths that should
404).

## Root Cause

In [packages/next/src/build/index.ts](packages/next/src/build/index.ts)
at line 2988:

```2988:2991:packages/next/src/build/index.ts
                  if (isSsg) {
                    // remove non-locale prefixed variant from defaultMap
                    delete defaultMap[page]
                  }
```

The `if (isSsg)` guard means non-SSG pages keep their non-locale entry,
which gets rendered and written to `server/pages/` as an orphan file.

## Fix

**Single-line change** in
[packages/next/src/build/index.ts](packages/next/src/build/index.ts):
Remove the `if (isSsg)` guard so `delete defaultMap[page]` always runs
when i18n is enabled.

The locale-prefixed entries are already created in the loop above (line
2981), and they capture the original `page` value via
`defaultMap[page]?.page || page`. The non-locale entry is not needed
after that -- in the old staging-directory flow (`moveExportedPage`), it
was rendered but never moved to the final output directory.

**Safety verification:**

- `updatePagesManifestForExportedPage` (line 3754) is called based on
`combinedPages`, not the export map -- it still correctly replaces
non-locale manifest entries with locale-prefixed ones
- `pageDuration` tracking (line 3745) may return `undefined` for the
non-locale path, but this is typed `number | undefined` and used with
`|| 0` fallback at line 375 of `build/utils.ts` -- purely cosmetic
- SSG pages already have this exact behavior (delete from defaultMap),
so this change makes non-SSG consistent with SSG
- The `ssgPageDurations` invariant check (line 3737-3738) only runs for
SSG pages, which are unaffected

## Test

Add assertions to the existing i18n integration test at
[test/integration/i18n-support/test/index.test.ts](test/integration/i18n-support/test/index.test.ts)
to verify that non-locale-prefixed HTML orphans do not exist in
`server/pages/` after build. The test already has production mode
assertions checking locale-prefixed file existence and manifest
correctness (lines 81-99).
parent 0f35dab6
......@@ -2999,10 +2999,8 @@ export default async function build(
}
}
if (isSsg) {
// remove non-locale prefixed variant from defaultMap
delete defaultMap[page]
}
// remove non-locale prefixed variant from defaultMap
delete defaultMap[page]
}
}
......@@ -3606,126 +3604,62 @@ export default async function build(
}
})
const moveExportedPage = async (
originPage: string,
// Update pages manifest entries for exported pages.
// The export worker writes pages directly to .next/server/pages/,
// so no file moving is needed — only the manifest must be updated.
const updatePagesManifestForExportedPage = (
page: string,
file: string,
isSsg: boolean,
ext: 'html' | 'json',
additionalSsgFile = false
isSsg: boolean
) => {
return staticGenerationSpan
.traceChild('move-exported-page')
.traceAsyncFn(async () => {
file = `${file}.${ext}`
const orig = path.join(outdir, file)
const pagePath = getPagePath(
originPage,
distDir,
undefined,
false
)
const relativeDest = path
.relative(
path.join(distDir, SERVER_DIRECTORY),
path.join(
path.join(
pagePath,
// strip leading / and then recurse number of nested dirs
// to place from base folder
originPage
.slice(1)
.split('/')
.map(() => '..')
.join('/')
),
file
)
)
.replace(/\\/g, '/')
if (
!isSsg &&
!(
// don't add static status page to manifest if it's
// the default generated version e.g. no pages/500
(
STATIC_STATUS_PAGES.includes(page) &&
!usedStaticStatusPages.includes(page)
)
)
) {
pagesManifest[page] = relativeDest
}
const dest = path.join(distDir, SERVER_DIRECTORY, relativeDest)
const isNotFound =
prerenderManifest.notFoundRoutes.includes(page)
// for SSG files with i18n the non-prerendered variants are
// output with the locale prefixed so don't attempt moving
// without the prefix
if ((!i18n || additionalSsgFile) && !isNotFound) {
await fs.mkdir(path.dirname(dest), { recursive: true })
await fs.rename(orig, dest)
} else if (i18n && !isSsg) {
// this will be updated with the locale prefixed variant
// since all files are output with the locale prefix
delete pagesManifest[page]
}
if (i18n) {
if (additionalSsgFile) return
const localeExt = page === '/' ? path.extname(file) : ''
const relativeDestNoPages = relativeDest.slice(
'pages/'.length
)
for (const locale of i18n.locales) {
const curPath = `/${locale}${page === '/' ? '' : page}`
if (
isSsg &&
prerenderManifest.notFoundRoutes.includes(curPath)
) {
continue
}
const updatedRelativeDest = path
.join(
'pages',
locale + localeExt,
// if it's the top-most index page we want it to be locale.EXT
// instead of locale/index.html
page === '/' ? '' : relativeDestNoPages
)
.replace(/\\/g, '/')
const updatedOrig = path.join(
outdir,
locale + localeExt,
page === '/' ? '' : file
)
const updatedDest = path.join(
distDir,
SERVER_DIRECTORY,
updatedRelativeDest
)
if (!isSsg) {
pagesManifest[curPath] = updatedRelativeDest
}
await fs.mkdir(path.dirname(updatedDest), {
recursive: true,
})
await fs.rename(updatedOrig, updatedDest)
}
}
})
const isUnusedStaticStatusPage =
STATIC_STATUS_PAGES.includes(page) &&
!usedStaticStatusPages.includes(page)
// SSG pages don't get manifest entries
if (isSsg) return
if (i18n) {
// Replace non-locale entry with per-locale entries
delete pagesManifest[page]
for (const locale of i18n.locales) {
const curPath = `/${locale}${page === '/' ? '' : page}`
if (prerenderManifest.notFoundRoutes.includes(curPath)) continue
const relativeDest =
page === '/'
? `pages/${locale}.html`
: `pages${normalizePagePath(curPath)}.html`
pagesManifest[curPath] = relativeDest
}
} else if (!isUnusedStaticStatusPage) {
const file = normalizePagePath(page)
pagesManifest[page] = `pages${file}.html`
}
}
// The export worker writes files directly to server/pages/,
// so we must delete files for notFound routes that shouldn't be served.
const deleteNotFoundPageFiles = (normalizedPath: string) =>
Promise.all([
fs.rm(
path.join(
distDir,
SERVER_DIRECTORY,
'pages',
`${normalizedPath}.html`
),
{ force: true }
),
fs.rm(
path.join(
distDir,
SERVER_DIRECTORY,
'pages',
`${normalizedPath}.json`
),
{ force: true }
),
])
async function moveExportedAppNotFoundTo404() {
return staticGenerationSpan
.traceChild('move-exported-app-not-found-')
......@@ -3810,19 +3744,19 @@ export default async function build(
if (hasStaticApp404) {
await moveExportedAppNotFoundTo404()
} else {
// Only move /404 to /404 when there is no custom 404 as in that case we don't know about the 404 page
// Update manifest for default 404 page
if (
!hasPages404 &&
!hasApp404 &&
useStaticPages404 &&
!appDirOnly
) {
await moveExportedPage('/_error', '/404', '/404', false, 'html')
updatePagesManifestForExportedPage('/404', false)
}
}
if (useDefaultStatic500 && !appDirOnly) {
await moveExportedPage('/_error', '/500', '/500', false, 'html')
updatePagesManifestForExportedPage('/500', false)
}
// If there's app router and no pages router, use app router built-in 500.html
......@@ -3865,20 +3799,22 @@ export default async function build(
const hasHtmlOutput = !(isSsg && isDynamic && !isStaticSsgFallback)
if (hasHtmlOutput) {
await moveExportedPage(page, page, file, isSsg, 'html')
updatePagesManifestForExportedPage(page, isSsg)
}
if (isSsg) {
// For a non-dynamic SSG page, we must copy its data file
// from export, we already moved the HTML file above
if (!isDynamic) {
await moveExportedPage(page, page, file, isSsg, 'json')
if (i18n) {
// TODO: do we want to show all locale variants in build output
for (const locale of i18n.locales) {
const localePage = `/${locale}${page === '/' ? '' : page}`
if (prerenderManifest.notFoundRoutes.includes(localePage)) {
await deleteNotFoundPageFiles(
normalizePagePath(localePage)
)
}
const cacheControl = getCacheControl(localePage)
prerenderManifest.routes[localePage] = {
......@@ -3897,6 +3833,10 @@ export default async function build(
}
}
} else {
if (prerenderManifest.notFoundRoutes.includes(page)) {
await deleteNotFoundPageFiles(file)
}
const cacheControl = getCacheControl(page)
prerenderManifest.routes[page] = {
......@@ -3919,28 +3859,17 @@ export default async function build(
pageInfo.initialCacheControl = getCacheControl(page)
}
} else {
// For a dynamic SSG page, we did not copy its data exports and only
// copy the fallback HTML file (if present).
// We must also copy specific versions of this page as defined by
// `getStaticPaths` (additionalSsgPaths).
// For a dynamic SSG page, the export worker already wrote
// the HTML/JSON files directly to their final location.
// We only need to update the prerender manifest.
for (const route of additionalPaths.get(page) ?? []) {
const pageFile = normalizePagePath(route.pathname)
await moveExportedPage(
page,
route.pathname,
pageFile,
isSsg,
'html',
true
)
await moveExportedPage(
page,
route.pathname,
pageFile,
isSsg,
'json',
true
)
if (
prerenderManifest.notFoundRoutes.includes(route.pathname)
) {
await deleteNotFoundPageFiles(
normalizePagePath(route.pathname)
)
}
const cacheControl = getCacheControl(route.pathname)
......@@ -3968,8 +3897,6 @@ export default async function build(
}
}
// remove temporary export folder
await fs.rm(outdir, { recursive: true, force: true })
await writeManifest(pagesManifestPath, pagesManifest)
})
......
......@@ -345,8 +345,10 @@ async function exportAppImpl(
)
}
await fs.rm(outDir, { recursive: true, force: true })
await fs.mkdir(join(outDir, '_next', buildId), { recursive: true })
if (!options.buildExport) {
await fs.rm(outDir, { recursive: true, force: true })
await fs.mkdir(join(outDir, '_next', buildId), { recursive: true })
}
await fs.writeFile(
join(distDir, EXPORT_DETAIL),
......@@ -653,7 +655,7 @@ async function exportAppImpl(
}
const pagesDataDir = options.buildExport
? outDir
? join(distDir, 'server', 'pages')
: join(outDir, '_next/data', buildId)
const publicDir = join(dir, CLIENT_PUBLIC_FILES_PATH)
......@@ -720,7 +722,9 @@ async function exportAppImpl(
options,
dir,
distDir,
outDir,
outDir: options.buildExport
? join(distDir, 'server', 'pages')
: outDir,
nextConfig,
cacheHandler: nextConfig.cacheHandler,
cacheMaxMemorySize: nextConfig.cacheMaxMemorySize,
......@@ -937,19 +941,25 @@ async function exportAppImpl(
// realizing the implications.
const route = normalizePagePath(unnormalizedRoute)
const pagePath = getPagePath(pageName, distDir, undefined, isAppPath)
const distPagesDir = join(
pagePath,
// strip leading / and then recurse number of nested dirs
// to place from base folder
pageName
.slice(1)
.split('/')
.map(() => '..')
.join('/')
)
const orig = join(distPagesDir, route)
let orig: string
if (isAppPath) {
const pagePath = getPagePath(pageName, distDir, undefined, isAppPath)
const distPagesDir = join(
pagePath,
// strip leading / and then recurse number of nested dirs
// to place from base folder
pageName
.slice(1)
.split('/')
.map(() => '..')
.join('/')
)
orig = join(distPagesDir, route)
} else {
// Pages router files are written directly to server/pages/
// by the export worker during build, so read from there.
orig = join(distDir, 'server', 'pages', route)
}
const handlerSrc = `${orig}.body`
const handlerDest = join(outDir, route)
......
......@@ -113,7 +113,7 @@ export async function exportPagesPage(
renderResult && !renderResult.isNull ? renderResult.toUnchunkedString() : ''
const metadata = renderResult?.metadata || {}
if (metadata.pageData) {
if (metadata.pageData && !ssgNotFound) {
const dataFile = join(
pagesDataDir,
htmlFilename.replace(/\.html$/, NEXT_DATA_SUFFIX)
......
......@@ -1635,7 +1635,7 @@ export function runTests(ctx: {
"/catchall-dash/[...hello-world]": "pages/catchall-dash/[...hello-world].html",
"/d/[id]": "pages/d/[id].html",
"/dash/[hello-world]": "pages/dash/[hello-world].html",
"/index/[...slug]": "pages/index/index/[...slug].html",
"/index/[...slug]": "pages/index/[...slug].html",
"/on-mount/[post]": "pages/on-mount/[post].html",
"/p1/p2/all-ssg/[...rest]": "pages/p1/p2/all-ssg/[...rest].js",
"/p1/p2/all-ssr/[...rest]": "pages/p1/p2/all-ssr/[...rest].js",
......
import { nextTestSetup, isNextDev } from 'e2e-utils'
;(isNextDev ? describe.skip : describe)('i18n-beforefiles-rewrite', () => {
const { next } = nextTestSetup({
files: __dirname,
})
const locales = ['en', 'fr']
const autoStaticRoutes = [
{ pathname: '', content: 'index' },
{ pathname: '/home/a', content: 'Home A' },
{ pathname: '/home/b', content: 'Home B' },
{ pathname: '/dynamic/static', content: 'static route' },
{ pathname: '/dynamic/first', content: 'dynamic route' },
{ pathname: '/team/slug', content: '/[teamId]/[slug]' },
]
it('should serve locale-prefixed auto-static pages', async () => {
for (const { pathname, content } of autoStaticRoutes) {
for (const locale of locales) {
const url = `/${locale}${pathname}`
const res = await next.fetch(url)
const html = await res.text()
expect({ url, status: res.status }).toEqual({ url, status: 200 })
expect(html).toContain(content)
}
}
})
it.each(['', '/en', '/fr'])(
'should 404 for %s/rewrite-before-files',
async (localePrefix) => {
// beforeFiles rewrites /rewrite-before-files to /somewhere.
// /somewhere does not match any page, so this should 404.
// Before the fix, orphan dynamic HTML output could incorrectly match.
const res = await next.fetch(`${localePrefix}/rewrite-before-files`)
expect(res.status).toBe(404)
}
)
})
import { NextResponse } from 'next/server'
export function middleware(request) {
return NextResponse.next()
}
module.exports = {
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
},
rewrites() {
return {
beforeFiles: [
{
source: '/rewrite-before-files',
destination: '/somewhere',
},
],
afterFiles: [],
}
},
}
export default function Page() {
return <p>/[teamId]/[slug]</p>
}
export default function Page() {
return <p>dynamic route</p>
}
export default function Page() {
return <p>static route</p>
}
export default function Page() {
return <p>Home A</p>
}
export default function Page() {
return <p>Home B</p>
}
export default function Page() {
return <p>index</p>
}
......@@ -77,6 +77,30 @@ describe('i18n Support', () => {
expect(content).toMatch(/Internal Server Error/i)
}
})
it('should have locale-prefixed pages-manifest entries for default 500', async () => {
const manifest = await fs.readJSON(
join(next.testDir, '.next/server/pages-manifest.json')
)
for (const locale of locales) {
expect(manifest[`/${locale}/500`]).toBe(`pages/${locale}/500.html`)
}
})
it('should not have non-locale-prefixed HTML files for auto-static pages', async () => {
const pagesDir = join(next.testDir, '.next/server/pages')
// auto-export is a non-SSG auto-static page; only locale-prefixed
// variants should exist in server/pages/ (e.g. en-US/auto-export.html).
// A bare auto-export.html would be an orphan that can interfere with routing.
expect(await fs.pathExists(join(pagesDir, 'auto-export.html'))).toBe(
false
)
for (const locale of locales) {
expect(
await fs.pathExists(join(pagesDir, locale, 'auto-export.html'))
).toBe(true)
}
})
}
describe('with localeDetection disabled', () => {
......
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