Skip to content

Commit f6e9bd9

Browse files
authored
Ignore error pages for cache revalidate (#72412)
1 parent c3c5bf8 commit f6e9bd9

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

packages/next/src/server/base-server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,9 +1931,11 @@ export default abstract class Server<
19311931
if (pathname === UNDERSCORE_NOT_FOUND_ROUTE) {
19321932
pathname = '/404'
19331933
}
1934-
const is404Page = pathname === '/404'
1935-
1936-
const is500Page = pathname === '/500'
1934+
const isErrorPathname = pathname === '/_error'
1935+
const is404Page =
1936+
pathname === '/404' || (isErrorPathname && res.statusCode === 404)
1937+
const is500Page =
1938+
pathname === '/500' || (isErrorPathname && res.statusCode === 500)
19371939
const isAppPath = components.isAppPath === true
19381940

19391941
const hasServerProps = !!components.getServerSideProps
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('empty-ssg-fallback', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should not cache 404 error page', async () => {
9+
const res = await next.fetch('/any-non-existed')
10+
expect(res.status).toBe(404)
11+
})
12+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const getStaticPaths = async () => {
2+
return {
3+
paths: [],
4+
fallback: 'blocking',
5+
}
6+
}
7+
export const getStaticProps = async () => {
8+
return {
9+
notFound: true,
10+
}
11+
}
12+
13+
export default function Page() {
14+
return <p>slug</p>
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { GetServerSidePropsContext } from 'next'
2+
3+
function Error({ statusCode }: { statusCode: number }) {
4+
return <p>{statusCode ? `${statusCode} error` : '500 error'}</p>
5+
}
6+
7+
export async function getServerSideProps({ res }: GetServerSidePropsContext) {
8+
const statusCode = res ? res.statusCode : 404
9+
return { props: { statusCode } }
10+
}
11+
12+
export default Error

0 commit comments

Comments
 (0)