Unverified Commit fc134fc1 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix: ensure app router not found works when deployed with pages i18n config (#77905)

When using both pages & app routers, during `next dev` and `next start`,
if a function triggers a 404 error, we serve the app router not found
page rather than the pages router one.

When paired with the `i18n` configuration in `next.config`, the Next.js
builder ignores the app router 404 and bundles the localized pages
router 404 pages with the deployed functions. This leads to a 500 error,
and is fixed in https://github.com/vercel/vercel/pull/13222

However, to match the dev/start handling, we also need to update the
`pages-manifest` so the builder knows to resolve `/404.html` and not the
pages router variant when deployed.

Fixes NEXT-4045

Validation:
[vtest314-e2e-tests-bkc89ab40-ztanner.vercel.app/app-dir/foo](https://vtest314-e2e-tests-bkc89ab40-ztanner.vercel.app/app-dir/foo)
parent 2b6304ed
......@@ -3301,6 +3301,16 @@ export default async function build(
orig,
path.join(distDir, 'server', updatedRelativeDest)
)
// since the app router not found is prioritized over pages router,
// we have to ensure the app router entries are available for all locales
if (i18n) {
for (const locale of i18n.locales) {
const curPath = `/${locale}/404`
pagesManifest[curPath] = updatedRelativeDest
}
}
pagesManifest['/404'] = updatedRelativeDest
}
})
......
import { notFound } from 'next/navigation'
export async function generateStaticParams() {
return []
}
async function validateSlug(slug: string[]) {
try {
const isValidPath =
slug.length === 1 && (slug[0] === 'about' || slug[0] === 'contact')
if (!isValidPath) {
return false
}
return true
} catch (error) {
throw error
}
}
export default async function CatchAll({
params,
}: {
params: Promise<{ slug: string[] }>
}) {
const { slug } = await params
const slugArray = Array.isArray(slug) ? slug : [slug]
// Validate the slug
const isValid = await validateSlug(slugArray)
// If not valid, show 404
if (!isValid) {
notFound()
}
return (
<div>
<h1>Catch All</h1>
<p>This is a catch all page added to the APP router</p>
</div>
)
}
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import React from 'react'
const NotFound = () => (
<div>
<h1>APP ROUTER - 404 PAGE</h1>
<p>This page is using the APP ROUTER</p>
</div>
)
export default NotFound
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
i18n: {
locales: ['en-GB', 'en'],
defaultLocale: 'en',
localeDetection: false,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('not-found-with-pages', () => {
const { next, isNextStart } = nextTestSetup({
files: __dirname,
})
if (isNextStart) {
it('should write all locales to the pages manifest', async () => {
const pagesManifest = JSON.parse(
await next.readFile('.next/server/pages-manifest.json')
)
expect(pagesManifest['/404']).toBe('pages/404.html')
expect(pagesManifest['/en/404']).toBe('pages/404.html')
expect(pagesManifest['/en-GB/404']).toBe('pages/404.html')
})
}
it('should prefer the app router 404 over the pages router 404 when both are present', async () => {
const browser = await next.browser('/app-dir/foo')
expect(await browser.elementByCss('h1').text()).toBe(
'APP ROUTER - 404 PAGE'
)
await browser.loadPage(next.url)
expect(await browser.elementByCss('h1').text()).toBe(
'APP ROUTER - 404 PAGE'
)
})
})
import React from 'react'
import { GetStaticProps } from 'next'
interface NotFoundProps {
message: string
}
const NotFound = ({ message }: NotFoundProps) => (
<div>
<h1>PAGES ROUTER - 404 PAGE</h1>
<p>This page is using the PAGES ROUTER</p>
<p>{message}</p>
</div>
)
export const getStaticProps: GetStaticProps<NotFoundProps> = async () => {
return {
props: {
message: 'Custom message fetched at build time',
},
}
}
export default NotFound
import React from 'react'
export const getStaticProps = ({ params }: { params: { slug: string[] } }) => {
try {
const slugArray = Array.isArray(params.slug) ? params.slug : [params.slug]
const isValidPath =
slugArray.length === 1 &&
(slugArray[0] === 'about' || slugArray[0] === 'contact')
if (!isValidPath) {
return {
notFound: true,
}
}
return {
props: {
slug: params.slug,
},
}
} catch (error) {
throw error
}
}
export const getStaticPaths = async () => ({
paths: [],
fallback: 'blocking',
})
const CatchAll = () => (
<div>
<h1>Catch All</h1>
<p>This is a catch all page added to the pages router</p>
</div>
)
export default CatchAll
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