From 95b72cc9de5af55e98a27331ca5d9449040fd85b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 6 Nov 2024 21:48:32 +0100 Subject: [PATCH] Ignore error pages for cache revalidate (#72412) --- packages/next/src/server/base-server.ts | 8 +++++--- .../empty-ssg-fallback/empty-ssg-fallback.test.ts | 12 ++++++++++++ .../empty-ssg-fallback/pages/[...slug].tsx | 15 +++++++++++++++ .../empty-ssg-fallback/pages/_error.tsx | 12 ++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/production/empty-ssg-fallback/empty-ssg-fallback.test.ts create mode 100644 test/production/empty-ssg-fallback/pages/[...slug].tsx create mode 100644 test/production/empty-ssg-fallback/pages/_error.tsx diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index de89f6b35e0ad..903b7ca47afa2 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -1733,9 +1733,11 @@ export default abstract class Server { if (pathname === UNDERSCORE_NOT_FOUND_ROUTE) { pathname = '/404' } - const is404Page = pathname === '/404' - - const is500Page = pathname === '/500' + const isErrorPathname = pathname === '/_error' + const is404Page = + pathname === '/404' || (isErrorPathname && res.statusCode === 404) + const is500Page = + pathname === '/500' || (isErrorPathname && res.statusCode === 500) const isAppPath = components.isAppPath === true const hasServerProps = !!components.getServerSideProps diff --git a/test/production/empty-ssg-fallback/empty-ssg-fallback.test.ts b/test/production/empty-ssg-fallback/empty-ssg-fallback.test.ts new file mode 100644 index 0000000000000..07b69202fc3e4 --- /dev/null +++ b/test/production/empty-ssg-fallback/empty-ssg-fallback.test.ts @@ -0,0 +1,12 @@ +import { nextTestSetup } from 'e2e-utils' + +describe('empty-ssg-fallback', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) + + it('should not cache 404 error page', async () => { + const res = await next.fetch('/any-non-existed') + expect(res.status).toBe(404) + }) +}) diff --git a/test/production/empty-ssg-fallback/pages/[...slug].tsx b/test/production/empty-ssg-fallback/pages/[...slug].tsx new file mode 100644 index 0000000000000..2518ee8b11245 --- /dev/null +++ b/test/production/empty-ssg-fallback/pages/[...slug].tsx @@ -0,0 +1,15 @@ +export const getStaticPaths = async () => { + return { + paths: [], + fallback: 'blocking', + } +} +export const getStaticProps = async () => { + return { + notFound: true, + } +} + +export default function Page() { + return

slug

+} diff --git a/test/production/empty-ssg-fallback/pages/_error.tsx b/test/production/empty-ssg-fallback/pages/_error.tsx new file mode 100644 index 0000000000000..5456542a50dda --- /dev/null +++ b/test/production/empty-ssg-fallback/pages/_error.tsx @@ -0,0 +1,12 @@ +import { GetServerSidePropsContext } from 'next' + +function Error({ statusCode }: { statusCode: number }) { + return

{statusCode ? `${statusCode} error` : '500 error'}

+} + +export async function getServerSideProps({ res }: GetServerSidePropsContext) { + const statusCode = res ? res.statusCode : 404 + return { props: { statusCode } } +} + +export default Error