Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure unstable_revalidate does not error from notFound: true #34826

Merged
merged 2 commits into from Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/next/server/api-utils/node.ts
Expand Up @@ -315,7 +315,13 @@ async function unstable_revalidate(
},
})

if (!res.ok) {
// we use the cache header to determine successful revalidate as
// a non-200 status code can be returned from a successful revalidate
// e.g. notFound: true returns 404 status code but is successful
const cacheHeader =
res.headers.get('x-vercel-cache') || res.headers.get('x-nextjs-cache')

if (cacheHeader?.toUpperCase() !== 'REVALIDATED') {
throw new Error(`Invalid response ${res.status}`)
}
} catch (err) {
Expand Down
15 changes: 15 additions & 0 deletions packages/next/server/base-server.ts
Expand Up @@ -1528,6 +1528,21 @@ export default abstract class Server {
return null
}

if (isSSG) {
// set x-nextjs-cache header to match the header
// we set for the image-optimizer
res.setHeader(
'x-nextjs-cache',
isManualRevalidate
? 'REVALIDATED'
: cacheEntry.isMiss
? 'MISS'
: cacheEntry.isStale
? 'STALE'
: 'HIT'
ijjk marked this conversation as resolved.
Show resolved Hide resolved
)
}

const { revalidate, value: cachedData } = cacheEntry
const revalidateOptions: any =
typeof revalidate !== 'undefined' &&
Expand Down
43 changes: 43 additions & 0 deletions test/e2e/prerender.test.ts
Expand Up @@ -149,6 +149,11 @@ describe('Prerender', () => {
initialRevalidateSeconds: false,
srcRoute: '/api-docs/[...slug]',
},
'/blocking-fallback-once/404-on-manual-revalidate': {
dataRoute: `/_next/data/${next.buildId}/blocking-fallback-once/404-on-manual-revalidate.json`,
initialRevalidateSeconds: false,
srcRoute: '/blocking-fallback-once/[slug]',
},
'/blocking-fallback-some/a': {
dataRoute: `/_next/data/${next.buildId}/blocking-fallback-some/a.json`,
initialRevalidateSeconds: 1,
Expand Down Expand Up @@ -1978,6 +1983,44 @@ describe('Prerender', () => {
expect($4('#time').text()).not.toBe(initialTime)
})

it('should manual revalidate that returns notFound: true', async () => {
const html = await renderViaHTTP(
next.url,
'/blocking-fallback-once/404-on-manual-revalidate'
)
const $ = cheerio.load(html)
const initialTime = $('#time').text()

expect($('p').text()).toMatch(/Post:.*?404-on-manual-revalidate/)

const html2 = await renderViaHTTP(
next.url,
'/blocking-fallback-once/404-on-manual-revalidate'
)
const $2 = cheerio.load(html2)

expect(initialTime).toBe($2('#time').text())

const res = await fetchViaHTTP(
next.url,
'/api/manual-revalidate',
{
pathname: '/blocking-fallback-once/404-on-manual-revalidate',
},
{ redirect: 'manual' }
)
expect(res.status).toBe(200)
const revalidateData = await res.json()
expect(revalidateData.revalidated).toBe(true)

const res2 = await fetchViaHTTP(
next.url,
'/blocking-fallback-once/404-on-manual-revalidate'
)
expect(res2.status).toBe(404)
expect(await res2.text()).toContain('This page could not be found')
})

it('should handle manual revalidate for fallback: false', async () => {
const res = await fetchViaHTTP(
next.url,
Expand Down
14 changes: 13 additions & 1 deletion test/e2e/prerender/pages/blocking-fallback-once/[slug].js
Expand Up @@ -4,14 +4,26 @@ import { useRouter } from 'next/router'

export async function getStaticPaths() {
return {
paths: [],
paths: [
{
params: { slug: '404-on-manual-revalidate' },
},
],
fallback: 'blocking',
}
}

export async function getStaticProps({ params }) {
await new Promise((resolve) => setTimeout(resolve, 1000))

if (process.env.NEXT_PHASE !== 'phase-production-build') {
if (params.slug === '404-on-manual-revalidate') {
return {
notFound: true,
}
}
}

return {
props: {
params,
Expand Down