Unverified Commit db1f3a64 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix duplicate static files in adapter (#95681)

When using `nextConfig.i18n`, then static files for `404.html` and
`500.html` were added multiple times to the list.

These duplicate entries in `outputs.staticFiles` ended up causing race
conditions in the Vercel adapter because the same output file path was
written concurrently by multiple file operations.
parent 4736452e
...@@ -776,6 +776,8 @@ export async function handleBuildComplete({ ...@@ -776,6 +776,8 @@ export async function handleBuildComplete({
const rscFallbackPath = path.join(distDir, 'server', 'rsc-fallback.json') const rscFallbackPath = path.join(distDir, 'server', 'rsc-fallback.json')
const emittedStaticFilePathnames = new Set<string>()
if (appPageKeys && appPageKeys.length > 0 && pageKeys.length > 0) { if (appPageKeys && appPageKeys.length > 0 && pageKeys.length > 0) {
await fs.writeFile(rscFallbackPath, '{}') await fs.writeFile(rscFallbackPath, '{}')
} }
...@@ -812,6 +814,7 @@ export async function handleBuildComplete({ ...@@ -812,6 +814,7 @@ export async function handleBuildComplete({
} satisfies AdapterOutput['STATIC_FILE'] } satisfies AdapterOutput['STATIC_FILE']
outputs.staticFiles.push(localeOutput) outputs.staticFiles.push(localeOutput)
emittedStaticFilePathnames.add(localeOutput.pathname)
if (appPageKeys && appPageKeys.length > 0) { if (appPageKeys && appPageKeys.length > 0) {
outputs.staticFiles.push({ outputs.staticFiles.push({
...@@ -833,6 +836,7 @@ export async function handleBuildComplete({ ...@@ -833,6 +836,7 @@ export async function handleBuildComplete({
} satisfies AdapterOutput['STATIC_FILE'] } satisfies AdapterOutput['STATIC_FILE']
outputs.staticFiles.push(staticOutput) outputs.staticFiles.push(staticOutput)
emittedStaticFilePathnames.add(staticOutput.pathname)
if (appPageKeys && appPageKeys.length > 0) { if (appPageKeys && appPageKeys.length > 0) {
outputs.staticFiles.push({ outputs.staticFiles.push({
...@@ -1826,10 +1830,16 @@ export async function handleBuildComplete({ ...@@ -1826,10 +1830,16 @@ export async function handleBuildComplete({
if (!prerenderManifest.routes[errorDocPath]) { if (!prerenderManifest.routes[errorDocPath]) {
for (const currentDocPath of [ for (const currentDocPath of [
errorDocPath, errorDocPath,
...(config.i18n?.locales?.map((locale) => ...(config.i18n?.locales
path.posix.join('/', locale, errorDoc) ?.filter((locale) => locale !== config.i18n?.defaultLocale)
) || []), .map((locale) => path.posix.join('/', locale, errorDoc)) || []),
]) { ]) {
// skip if this static file was already emitted for an
// auto-static-optimized page above to avoid duplicate entries
if (emittedStaticFilePathnames.has(currentDocPath)) {
continue
}
const currentFilePath = path.join( const currentFilePath = path.join(
pagesDistDir, pagesDistDir,
`${currentDocPath}.html` `${currentDocPath}.html`
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import type { NextAdapter } from 'next' import type { NextAdapter } from 'next'
describe('adapter config with i18n API routes', () => { describe('adapter config with i18n routes', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
...@@ -33,4 +33,20 @@ describe('adapter config with i18n API routes', () => { ...@@ -33,4 +33,20 @@ describe('adapter config with i18n API routes', () => {
'/$nextLocale/blog/[slug]?nxtPslug=$nxtPslug' '/$nextLocale/blog/[slug]?nxtPslug=$nxtPslug'
) )
}) })
it('does not emit outputs multiple times for a given pathname', async () => {
const { outputs }: Parameters<NextAdapter['onBuildComplete']>[0] =
await next.readJSON('build-complete.json')
const pathnameSet = (f) => new Set(f.map((o) => o.pathname))
expect(pathnameSet(outputs.pages).size).toBe(outputs.pages.length)
expect(pathnameSet(outputs.appPages).size).toBe(outputs.appPages.length)
expect(pathnameSet(outputs.pagesApi).size).toBe(outputs.pagesApi.length)
expect(pathnameSet(outputs.appRoutes).size).toBe(outputs.appRoutes.length)
expect(pathnameSet(outputs.prerenders).size).toBe(outputs.prerenders.length)
expect(pathnameSet(outputs.staticFiles).size).toBe(
outputs.staticFiles.length
)
})
}) })
...@@ -3,7 +3,7 @@ import fs from 'fs/promises' ...@@ -3,7 +3,7 @@ import fs from 'fs/promises'
// @ts-check // @ts-check
/** @type {import('next').NextAdapter} */ /** @type {import('next').NextAdapter} */
const adapter = { const adapter = {
name: 'i18n-api-test-adapter', name: 'i18n-test-adapter',
async onBuildComplete(ctx) { async onBuildComplete(ctx) {
await fs.writeFile('build-complete.json', JSON.stringify(ctx, null, 2)) await fs.writeFile('build-complete.json', JSON.stringify(ctx, null, 2))
}, },
......
export default function () {
return <p>Custom 404</p>
}
export default function () {
return <p>Custom 500</p>
}
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