Unverified Commit 51e091d5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix prerender tags when notFound is called (#74577)

This ensures we don't lose tags added to a page before `notFound()` is
called as otherwise a fetch on the page that has been revalidated won't
be able to be applied correctly if dropped from a `notFound()` call.
This was broken recently in the refactors of handling rendering in
app-render as it worked correctly in `v14`.

x-ref: [slack
thread](https://vercel.slack.com/archives/C04V3E1UYNQ/p1736200963684869)

Closes https://linear.app/vercel/issue/ENET-1381
Closes https://linear.app/vercel/issue/NEXT-3946
Closes https://github.com/vercel/next.js/issues/73973
Closes https://github.com/vercel/next.js/issues/66540
parent 5a8d1171
...@@ -3833,7 +3833,7 @@ async function prerenderToStream( ...@@ -3833,7 +3833,7 @@ async function prerenderToStream(
revalidate: INFINITE_CACHE, revalidate: INFINITE_CACHE,
expire: INFINITE_CACHE, expire: INFINITE_CACHE,
stale: INFINITE_CACHE, stale: INFINITE_CACHE,
tags: [...implicitTags], tags: [...(prerenderStore?.tags || implicitTags)],
}) })
const errorRSCPayload = await workUnitAsyncStorage.run( const errorRSCPayload = await workUnitAsyncStorage.run(
prerenderLegacyStore, prerenderLegacyStore,
......
...@@ -970,6 +970,12 @@ describe('app-dir static/dynamic handling', () => { ...@@ -970,6 +970,12 @@ describe('app-dir static/dynamic handling', () => {
"partial-gen-params-no-additional-slug/fr/second.rsc", "partial-gen-params-no-additional-slug/fr/second.rsc",
"partial-gen-params/[lang]/[slug]/page.js", "partial-gen-params/[lang]/[slug]/page.js",
"partial-gen-params/[lang]/[slug]/page_client-reference-manifest.js", "partial-gen-params/[lang]/[slug]/page_client-reference-manifest.js",
"prerendered-not-found/[slug]/page.js",
"prerendered-not-found/[slug]/page_client-reference-manifest.js",
"prerendered-not-found/first.html",
"prerendered-not-found/first.rsc",
"prerendered-not-found/second.html",
"prerendered-not-found/second.rsc",
"react-fetch-deduping-edge/page.js", "react-fetch-deduping-edge/page.js",
"react-fetch-deduping-edge/page_client-reference-manifest.js", "react-fetch-deduping-edge/page_client-reference-manifest.js",
"react-fetch-deduping-node/page.js", "react-fetch-deduping-node/page.js",
...@@ -1954,6 +1960,54 @@ describe('app-dir static/dynamic handling', () => { ...@@ -1954,6 +1960,54 @@ describe('app-dir static/dynamic handling', () => {
"initialRevalidateSeconds": false, "initialRevalidateSeconds": false,
"srcRoute": "/partial-gen-params-no-additional-slug/[lang]/[slug]", "srcRoute": "/partial-gen-params-no-additional-slug/[lang]/[slug]",
}, },
"/prerendered-not-found/first": {
"allowHeader": [
"host",
"x-matched-path",
"x-prerender-revalidate",
"x-prerender-revalidate-if-generated",
"x-next-revalidated-tags",
"x-next-revalidate-tag-token",
],
"dataRoute": "/prerendered-not-found/first.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"initialRevalidateSeconds": false,
"srcRoute": "/prerendered-not-found/[slug]",
},
"/prerendered-not-found/second": {
"allowHeader": [
"host",
"x-matched-path",
"x-prerender-revalidate",
"x-prerender-revalidate-if-generated",
"x-next-revalidated-tags",
"x-next-revalidate-tag-token",
],
"dataRoute": "/prerendered-not-found/second.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"initialRevalidateSeconds": false,
"srcRoute": "/prerendered-not-found/[slug]",
},
"/route-handler/no-store-force-static": { "/route-handler/no-store-force-static": {
"allowHeader": [ "allowHeader": [
"host", "host",
...@@ -2627,6 +2681,31 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2627,6 +2681,31 @@ describe('app-dir static/dynamic handling', () => {
"fallback": false, "fallback": false,
"routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$", "routeRegex": "^\\/partial\\-gen\\-params\\-no\\-additional\\-slug\\/([^\\/]+?)\\/([^\\/]+?)(?:\\/)?$",
}, },
"/prerendered-not-found/[slug]": {
"allowHeader": [
"host",
"x-matched-path",
"x-prerender-revalidate",
"x-prerender-revalidate-if-generated",
"x-next-revalidated-tags",
"x-next-revalidate-tag-token",
],
"dataRoute": "/prerendered-not-found/[slug].rsc",
"dataRouteRegex": "^\\/prerendered\\-not\\-found\\/([^\\/]+?)\\.rsc$",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"fallback": null,
"routeRegex": "^\\/prerendered\\-not\\-found\\/([^\\/]+?)(?:\\/)?$",
},
"/ssg-draft-mode/[[...route]]": { "/ssg-draft-mode/[[...route]]": {
"allowHeader": [ "allowHeader": [
"host", "host",
...@@ -2727,6 +2806,25 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2727,6 +2806,25 @@ describe('app-dir static/dynamic handling', () => {
) )
}) })
it('should have correct cache tags for prerendered path', async () => {
const firstMeta = await next.readJSON(
'.next/server/app/prerendered-not-found/first.meta'
)
const secondMeta = await next.readJSON(
'.next/server/app/prerendered-not-found/second.meta'
)
expect(firstMeta.status).toBe(404)
expect(firstMeta.headers['x-next-cache-tags']).toBe(
'_N_T_/layout,_N_T_/prerendered-not-found/layout,_N_T_/prerendered-not-found/[slug]/layout,_N_T_/prerendered-not-found/[slug]/page,_N_T_/prerendered-not-found/first,explicit-tag'
)
expect(secondMeta.status).not.toBe(404)
expect(secondMeta.headers['x-next-cache-tags']).toBe(
'_N_T_/layout,_N_T_/prerendered-not-found/layout,_N_T_/prerendered-not-found/[slug]/layout,_N_T_/prerendered-not-found/[slug]/page,_N_T_/prerendered-not-found/second,explicit-tag'
)
})
// build cache not leveraged for custom cache handler so not seeded // build cache not leveraged for custom cache handler so not seeded
if (!process.env.CUSTOM_CACHE_HANDLER) { if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should correctly error and not update cache for ISR', async () => { it('should correctly error and not update cache for ISR', async () => {
......
import React from 'react'
import { notFound } from 'next/navigation'
export function generateStaticParams() {
return [
{
slug: 'first',
},
{
slug: 'second',
},
]
}
export default async function Page(props: {
params: Promise<{ slug: string }>
}) {
const params = await props.params
await fetch('https://next-data-api.vercel.app/api/random', {
next: {
tags: ['explicit-tag'],
},
})
if (
params.slug !== 'second' &&
process.env.NEXT_PHASE === 'phase-production-build'
) {
notFound()
}
return (
<>
<p>/prerendered-not-found/{params.slug}</p>
<p>{Date.now()}</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