Unverified Commit 93249ee0 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix Pages router 404 runtime rendering with Adapter (#95264)

The problem is that the adapter expected a `output.pages` for the `/404`
route so that it can add these files to other Pages handlers so that
they can render a 404 at runtime (getStaticProps with
revaliate/getServerSideProps):


https://github.com/nextjs/adapter-vercel/blob/650effd340b4f676a9bca6e0cdaba9bf7d34595a/packages/adapter/src/outputs.ts#L421-L438

https://github.com/nextjs/adapter-vercel/blob/650effd340b4f676a9bca6e0cdaba9bf7d34595a/packages/adapter/src/node-handler.ts#L331-L340

But this 404 Pages handler was not actually emitted if it was
prerendered, where it only emitted the `/404.html` static file. But
that's not what the adapter needs.

Tests:
1. Run as an e2e test
2. Add a variant with dynamically rendered 404 pages

Full e2e test run:
~~https://github.com/vercel/next.js/actions/runs/29081105397~~
https://github.com/vercel/next.js/actions/runs/29087851475

Closes NEXT-4947
parent db1f3a64
......@@ -848,8 +848,12 @@ export async function handleBuildComplete({
})
}
}
// if was a static file output don't create page output as well
continue
if (page !== '/404') {
// If it was a static file output don't create page output as well.
// However, don't skip the 404 output to be able to add it to other Pages routes so that
// they can render 404s at runtime.
continue
}
}
const { assets, assetsHashes } = await handleTraceFiles(
......
import { nextTestSetup } from 'e2e-utils'
import { createRouterAct } from 'router-act'
import { nextTestSetup } from 'e2e-utils'
describe('optimistic routing - parallel slot catch-all regression', () => {
const { next, isNextDev } = nextTestSetup({
......
export default function Page(props) {
return (
<>
<h1>Custom 404 page</h1>
</>
)
}
......@@ -317,7 +317,7 @@ const runTests = (
const res = await fetchViaHTTP(next.url, '/not-found', { hiding: true })
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
expect(await res.text()).toContain('Custom 404 page')
})
it('should render 404 correctly when notFound is returned client-transition (non-dynamic)', async () => {
......@@ -329,7 +329,7 @@ const runTests = (
await browser.waitForElementByCss('h1')
expect(await browser.elementByCss('html').text()).toContain(
'This page could not be found'
'Custom 404 page'
)
expect(await browser.eval('window.beforeNav')).toBe(1)
})
......@@ -346,7 +346,7 @@ const runTests = (
})
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
expect(await res.text()).toContain('Custom 404 page')
})
it('should render 404 correctly when notFound is returned client-transition (dynamic)', async () => {
......@@ -358,7 +358,7 @@ const runTests = (
await browser.waitForElementByCss('h1')
expect(await browser.elementByCss('html').text()).toContain(
'This page could not be found'
'Custom 404 page'
)
expect(await browser.eval('window.beforeNav')).toBe(1)
})
......@@ -382,7 +382,7 @@ const runTests = (
} else {
expect(res.status).toBe(500)
expect(html).toContain('custom pages/500')
expect(html).not.toContain('This page could not be found')
expect(html).not.toContain('Custom 404 page')
}
})
......
import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('SSG notFound revalidate', () => {
describe.each(['static', 'dynamic'])('%s 404', (type) => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: { 'fs-extra': 'latest' },
overrideFiles:
type === 'dynamic'
? {
'pages/404.js': `export default function Page(props) {
return (
<>
<p id="not-found">404 page</p>
<p id="props">{JSON.stringify({notFound: true})}</p>
</>
)
}
`,
}
: undefined,
})
it('should correctly render 404', async () => {
{
let res = await next.fetch('/always-not-found/first')
let $ = await next.render$('/always-not-found/first')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
}
{
let res = await next.fetch('/always-not-found/second')
let $ = await next.render$('/always-not-found/second')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
}
{
let res = await next.fetch('/always-not-found')
let $ = await next.render$('/always-not-found')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
}
})
// TODO these currently rely on local FS modifications, but should ideally also run when deployed
if (isNextStart) {
it('should revalidate page when notFound returned during build', async () => {
let res = await next.fetch('/initial-not-found/first')
let $ = await next.render$('/initial-not-found/first')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
res = await next.fetch('/initial-not-found/second')
$ = await next.render$('/initial-not-found/second')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
res = await next.fetch('/initial-not-found')
$ = await next.render$('/initial-not-found')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
await next.patchFile('data.txt', '200')
try {
await retry(async () => {
const r = await next.fetch('/initial-not-found/first')
const $r = await next.render$('/initial-not-found/first')
expect(r.status).toBe(200)
expect($r('#data').text()).toBe('200')
})
await retry(async () => {
const r = await next.fetch('/initial-not-found/second')
const $r = await next.render$('/initial-not-found/second')
expect(r.status).toBe(200)
expect($r('#data').text()).toBe('200')
})
await retry(async () => {
const r = await next.fetch('/initial-not-found')
const $r = await next.render$('/initial-not-found')
expect(r.status).toBe(200)
expect($r('#data').text()).toBe('200')
})
} finally {
await next.patchFile('data.txt', '404')
}
})
it('should revalidate after notFound is returned for fallback: blocking', async () => {
let res = await next.fetch('/fallback-blocking/hello')
let $ = await next.render$('/fallback-blocking/hello')
expect(res.headers.get('cache-control')).toBe(
isNextDev
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
await retry(async () => {
res = await next.fetch('/fallback-blocking/hello')
$ = await next.render$('/fallback-blocking/hello')
expect(res.headers.get('cache-control')).toBe(
isNextDev
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
const p = JSON.parse($('#props').text())
expect(p.found).toBe(true)
expect(p.params).toEqual({ slug: 'hello' })
expect(isNaN(p.random)).toBe(false)
})
const props = JSON.parse($('#props').text())
await retry(async () => {
const r = await next.fetch('/fallback-blocking/hello')
const $r = await next.render$('/fallback-blocking/hello')
const p = JSON.parse($r('#props').text())
expect(r.headers.get('cache-control')).toBe(
isNextDev
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
)
expect(r.status).toBe(200)
expect(p.found).toBe(true)
expect(p.params).toEqual({ slug: 'hello' })
expect(isNaN(p.random)).toBe(false)
expect(p.random).not.toBe(props.random)
})
})
it('should revalidate after notFound is returned for fallback: true', async () => {
const browser = await next.browser('/fallback-true/world')
await browser.waitForElementByCss('#not-found')
await retry(async () => {
const res = await next.fetch('/fallback-true/world')
expect(res.headers.get('cache-control')).toBe(
isNextDev
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(404)
const $ = await next.render$('/fallback-true/world')
expect(JSON.parse($('#props').text()).notFound).toBe(true)
})
await retry(async () => {
const res = await next.fetch('/fallback-true/world')
const $ = await next.render$('/fallback-true/world')
const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
isNextDev
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
expect(props.params).toEqual({ slug: 'world' })
expect(isNaN(props.random)).toBe(false)
})
const $ = await next.render$('/fallback-true/world')
const props = JSON.parse($('#props').text())
await retry(async () => {
const r = await next.fetch('/fallback-true/world')
const $r = await next.render$('/fallback-true/world')
const props3 = JSON.parse($r('#props').text())
expect(r.headers.get('cache-control')).toBe(
isNextDev
? 'no-cache, must-revalidate'
: 's-maxage=1, stale-while-revalidate=31535999'
)
expect(r.status).toBe(200)
expect(props3.found).toBe(true)
expect(props3.params).toEqual({ slug: 'world' })
expect(isNaN(props3.random)).toBe(false)
expect(props3.random).not.toBe(props.random)
})
})
}
})
})
export async function getStaticProps() {
const data = '404'
return {
props: { data },
notFound: data.trim() === '404',
revalidate: 1,
}
}
export async function getStaticPaths() {
return {
paths: [{ params: { slug: 'first' } }],
fallback: 'blocking',
}
}
export default function Page({ data }) {
return <p id="data">{data}</p>
}
export async function getStaticProps() {
const data = '404'
return {
props: { data },
notFound: data.trim() === '404',
revalidate: 1,
}
}
export default function Page({ data }) {
return <p id="data">{data}</p>
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('SSG notFound revalidate', () => {
const { next } = nextTestSetup({
files: __dirname,
dependencies: { 'fs-extra': 'latest' },
})
it('should revalidate page when notFound returned during build', async () => {
let res = await next.fetch('/initial-not-found/first')
let $ = await next.render$('/initial-not-found/first')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
res = await next.fetch('/initial-not-found/second')
$ = await next.render$('/initial-not-found/second')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
res = await next.fetch('/initial-not-found')
$ = await next.render$('/initial-not-found')
expect(res.status).toBe(404)
expect($('#not-found').text()).toBe('404 page')
await next.patchFile('data.txt', '200')
try {
await retry(async () => {
const r = await next.fetch('/initial-not-found/first')
const $r = await next.render$('/initial-not-found/first')
expect(r.status).toBe(200)
expect($r('#data').text()).toBe('200')
})
await retry(async () => {
const r = await next.fetch('/initial-not-found/second')
const $r = await next.render$('/initial-not-found/second')
expect(r.status).toBe(200)
expect($r('#data').text()).toBe('200')
})
await retry(async () => {
const r = await next.fetch('/initial-not-found')
const $r = await next.render$('/initial-not-found')
expect(r.status).toBe(200)
expect($r('#data').text()).toBe('200')
})
} finally {
await next.patchFile('data.txt', '404')
}
})
it('should revalidate after notFound is returned for fallback: blocking', async () => {
let res = await next.fetch('/fallback-blocking/hello')
let $ = await next.render$('/fallback-blocking/hello')
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(404)
expect(JSON.parse($('#props').text()).notFound).toBe(true)
await retry(async () => {
res = await next.fetch('/fallback-blocking/hello')
$ = await next.render$('/fallback-blocking/hello')
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
const p = JSON.parse($('#props').text())
expect(p.found).toBe(true)
expect(p.params).toEqual({ slug: 'hello' })
expect(isNaN(p.random)).toBe(false)
})
const props = JSON.parse($('#props').text())
await retry(async () => {
const r = await next.fetch('/fallback-blocking/hello')
const $r = await next.render$('/fallback-blocking/hello')
const p = JSON.parse($r('#props').text())
expect(r.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
)
expect(r.status).toBe(200)
expect(p.found).toBe(true)
expect(p.params).toEqual({ slug: 'hello' })
expect(isNaN(p.random)).toBe(false)
expect(p.random).not.toBe(props.random)
})
})
it('should revalidate after notFound is returned for fallback: true', async () => {
const browser = await next.browser('/fallback-true/world')
await browser.waitForElementByCss('#not-found')
await retry(async () => {
const res = await next.fetch('/fallback-true/world')
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(404)
const $ = await next.render$('/fallback-true/world')
expect(JSON.parse($('#props').text()).notFound).toBe(true)
})
await retry(async () => {
const res = await next.fetch('/fallback-true/world')
const $ = await next.render$('/fallback-true/world')
const props = JSON.parse($('#props').text())
expect(res.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
)
expect(res.status).toBe(200)
expect(props.found).toBe(true)
expect(props.params).toEqual({ slug: 'world' })
expect(isNaN(props.random)).toBe(false)
})
const $ = await next.render$('/fallback-true/world')
const props = JSON.parse($('#props').text())
await retry(async () => {
const r = await next.fetch('/fallback-true/world')
const $r = await next.render$('/fallback-true/world')
const props3 = JSON.parse($r('#props').text())
expect(r.headers.get('cache-control')).toBe(
's-maxage=1, stale-while-revalidate=31535999'
)
expect(r.status).toBe(200)
expect(props3.found).toBe(true)
expect(props3.params).toEqual({ slug: 'world' })
expect(isNaN(props3.random)).toBe(false)
expect(props3.random).not.toBe(props.random)
})
})
})
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