Skip to content

Commit

Permalink
Ignore error pages for cache revalidate (#72412) (#72484)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Nov 7, 2024
1 parent 2755430 commit 91cbf9d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1733,9 +1733,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
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
Expand Down
12 changes: 12 additions & 0 deletions test/production/empty-ssg-fallback/empty-ssg-fallback.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
15 changes: 15 additions & 0 deletions test/production/empty-ssg-fallback/pages/[...slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const getStaticPaths = async () => {
return {
paths: [],
fallback: 'blocking',
}
}
export const getStaticProps = async () => {
return {
notFound: true,
}
}

export default function Page() {
return <p>slug</p>
}
12 changes: 12 additions & 0 deletions test/production/empty-ssg-fallback/pages/_error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GetServerSidePropsContext } from 'next'

function Error({ statusCode }: { statusCode: number }) {
return <p>{statusCode ? `${statusCode} error` : '500 error'}</p>
}

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

export default Error

0 comments on commit 91cbf9d

Please sign in to comment.