Unverified Commit 00ed2ba7 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix revalidate: false detection in app (#49473)

When revalidate isn't defined in the tree at all and a fetch without
cache/revalidate fields is done we are incorrectly marking the initial
revalidate period with a time based value when it should be `false`.
This causes pages that should be fully static to revalidate
unexpectedly.

x-ref: [twitter
thread](https://twitter.com/diegohaz/status/1655638433795014657)
x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1683566860136879)
parent 3a5501e6
...@@ -289,7 +289,11 @@ export default async function build( ...@@ -289,7 +289,11 @@ export default async function build(
hasAppDir = Boolean(appDir) hasAppDir = Boolean(appDir)
if (isAppDirEnabled && hasAppDir) { if (isAppDirEnabled && hasAppDir) {
if (!process.env.__NEXT_TEST_MODE && ciEnvironment.hasNextSupport) { if (
(!process.env.__NEXT_TEST_MODE ||
process.env.__NEXT_TEST_MODE === 'e2e') &&
ciEnvironment.hasNextSupport
) {
const requireHook = require.resolve('../server/require-hook') const requireHook = require.resolve('../server/require-hook')
const contents = await promises.readFile(requireHook, 'utf8') const contents = await promises.readFile(requireHook, 'utf8')
await promises.writeFile( await promises.writeFile(
......
...@@ -173,14 +173,10 @@ export function patchFetch({ ...@@ -173,14 +173,10 @@ export function patchFetch({
if (['no-cache', 'no-store'].includes(_cache || '')) { if (['no-cache', 'no-store'].includes(_cache || '')) {
curRevalidate = 0 curRevalidate = 0
} }
if (typeof curRevalidate === 'number') { if (typeof curRevalidate === 'number' || curRevalidate === false) {
revalidate = curRevalidate revalidate = curRevalidate
} }
if (curRevalidate === false) {
revalidate = CACHE_ONE_YEAR
}
const _headers = getRequestMeta('headers') const _headers = getRequestMeta('headers')
const initHeaders: Headers = const initHeaders: Headers =
typeof _headers?.get === 'function' typeof _headers?.get === 'function'
...@@ -229,7 +225,7 @@ export function patchFetch({ ...@@ -229,7 +225,7 @@ export function patchFetch({
revalidate = revalidate =
typeof staticGenerationStore.revalidate === 'boolean' || typeof staticGenerationStore.revalidate === 'boolean' ||
typeof staticGenerationStore.revalidate === 'undefined' typeof staticGenerationStore.revalidate === 'undefined'
? CACHE_ONE_YEAR ? false
: staticGenerationStore.revalidate : staticGenerationStore.revalidate
} }
} }
...@@ -240,18 +236,19 @@ export function patchFetch({ ...@@ -240,18 +236,19 @@ export function patchFetch({
!autoNoCache && !autoNoCache &&
(typeof staticGenerationStore.revalidate === 'undefined' || (typeof staticGenerationStore.revalidate === 'undefined' ||
(typeof revalidate === 'number' && (typeof revalidate === 'number' &&
typeof staticGenerationStore.revalidate === 'number' && (staticGenerationStore.revalidate === false ||
revalidate < staticGenerationStore.revalidate)) (typeof staticGenerationStore.revalidate === 'number' &&
revalidate < staticGenerationStore.revalidate))))
) { ) {
staticGenerationStore.revalidate = revalidate staticGenerationStore.revalidate = revalidate
} }
const isCacheableRevalidate =
(typeof revalidate === 'number' && revalidate > 0) ||
revalidate === false
let cacheKey: string | undefined let cacheKey: string | undefined
if ( if (staticGenerationStore.incrementalCache && isCacheableRevalidate) {
staticGenerationStore.incrementalCache &&
typeof revalidate === 'number' &&
revalidate > 0
) {
try { try {
cacheKey = cacheKey =
await staticGenerationStore.incrementalCache.fetchCacheKey( await staticGenerationStore.incrementalCache.fetchCacheKey(
...@@ -304,6 +301,8 @@ export function patchFetch({ ...@@ -304,6 +301,8 @@ export function patchFetch({
const fetchIdx = staticGenerationStore.nextFetchId ?? 1 const fetchIdx = staticGenerationStore.nextFetchId ?? 1
staticGenerationStore.nextFetchId = fetchIdx + 1 staticGenerationStore.nextFetchId = fetchIdx + 1
const normalizedRevalidate = !revalidate ? CACHE_ONE_YEAR : revalidate
const doOriginalFetch = async (isStale?: boolean) => { const doOriginalFetch = async (isStale?: boolean) => {
// add metadata to init without editing the original // add metadata to init without editing the original
const clonedInit = { const clonedInit = {
...@@ -325,8 +324,7 @@ export function patchFetch({ ...@@ -325,8 +324,7 @@ export function patchFetch({
res.status === 200 && res.status === 200 &&
staticGenerationStore.incrementalCache && staticGenerationStore.incrementalCache &&
cacheKey && cacheKey &&
typeof revalidate === 'number' && isCacheableRevalidate
revalidate > 0
) { ) {
const bodyBuffer = Buffer.from(await res.arrayBuffer()) const bodyBuffer = Buffer.from(await res.arrayBuffer())
...@@ -341,9 +339,9 @@ export function patchFetch({ ...@@ -341,9 +339,9 @@ export function patchFetch({
status: res.status, status: res.status,
tags, tags,
}, },
revalidate, revalidate: normalizedRevalidate,
}, },
revalidate, normalizedRevalidate,
true, true,
fetchUrl, fetchUrl,
fetchIdx fetchIdx
...@@ -367,7 +365,7 @@ export function patchFetch({ ...@@ -367,7 +365,7 @@ export function patchFetch({
: await staticGenerationStore.incrementalCache.get( : await staticGenerationStore.incrementalCache.get(
cacheKey, cacheKey,
true, true,
revalidate, normalizedRevalidate,
fetchUrl, fetchUrl,
fetchIdx fetchIdx
) )
......
...@@ -23,12 +23,14 @@ createNextDescribe( ...@@ -23,12 +23,14 @@ createNextDescribe(
}, },
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => { ({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
let prerenderManifest let prerenderManifest
let buildCliOutputIndex = 0
beforeAll(async () => { beforeAll(async () => {
if (isNextStart) { if (isNextStart) {
prerenderManifest = JSON.parse( prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json') await next.readFile('.next/prerender-manifest.json')
) )
buildCliOutputIndex = next.cliOutput.length
} }
}) })
...@@ -171,6 +173,31 @@ createNextDescribe( ...@@ -171,6 +173,31 @@ createNextDescribe(
// On-Demand Revalidate has not effect in dev since app routes // On-Demand Revalidate has not effect in dev since app routes
// aren't considered static until prerendering // aren't considered static until prerendering
if (!(global as any).isNextDev && !process.env.CUSTOM_CACHE_HANDLER) { if (!(global as any).isNextDev && !process.env.CUSTOM_CACHE_HANDLER) {
it('should not revalidate / when revalidate is not used', async () => {
let prevData
for (let i = 0; i < 5; i++) {
const res = await next.fetch('/')
const html = await res.text()
const $ = cheerio.load(html)
const data = $('#page-data').text()
expect(res.status).toBe(200)
if (prevData) {
expect(prevData).toBe(data)
prevData = data
}
await waitFor(500)
}
if (isNextStart) {
expect(next.cliOutput.substring(buildCliOutputIndex)).not.toContain(
'rendering index'
)
}
})
it.each([ it.each([
{ {
type: 'edge route handler', type: 'edge route handler',
...@@ -399,6 +426,9 @@ createNextDescribe( ...@@ -399,6 +426,9 @@ createNextDescribe(
'hooks/use-search-params/with-suspense.html', 'hooks/use-search-params/with-suspense.html',
'hooks/use-search-params/with-suspense.rsc', 'hooks/use-search-params/with-suspense.rsc',
'hooks/use-search-params/with-suspense/page.js', 'hooks/use-search-params/with-suspense/page.js',
'index.html',
'index.rsc',
'page.js',
'partial-gen-params-no-additional-lang/[lang]/[slug]/page.js', 'partial-gen-params-no-additional-lang/[lang]/[slug]/page.js',
'partial-gen-params-no-additional-lang/en/RAND.html', 'partial-gen-params-no-additional-lang/en/RAND.html',
'partial-gen-params-no-additional-lang/en/RAND.rsc', 'partial-gen-params-no-additional-lang/en/RAND.rsc',
...@@ -507,6 +537,11 @@ createNextDescribe( ...@@ -507,6 +537,11 @@ createNextDescribe(
expect(curManifest.version).toBe(4) expect(curManifest.version).toBe(4)
expect(curManifest.routes).toEqual({ expect(curManifest.routes).toEqual({
'/': {
initialRevalidateSeconds: false,
srcRoute: '/',
dataRoute: '/index.rsc',
},
'/blog/tim': { '/blog/tim': {
initialRevalidateSeconds: 10, initialRevalidateSeconds: 10,
srcRoute: '/blog/[author]', srcRoute: '/blog/[author]',
...@@ -659,7 +694,7 @@ createNextDescribe( ...@@ -659,7 +694,7 @@ createNextDescribe(
'x-next-cache-tags': 'x-next-cache-tags':
'thankyounext,/route-handler/revalidate-360-isr/route', 'thankyounext,/route-handler/revalidate-360-isr/route',
}, },
initialRevalidateSeconds: false, initialRevalidateSeconds: 10,
srcRoute: '/route-handler/revalidate-360-isr', srcRoute: '/route-handler/revalidate-360-isr',
}, },
'/variable-config-revalidate/revalidate-3': { '/variable-config-revalidate/revalidate-3': {
......
export default async function Page() {
console.log('rendering index')
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page'
).then((res) => res.text())
return (
<>
<p id="page">/variable-revalidate/revalidate-360</p>
<p id="page-data">{data}</p>
<p id="now">{Date.now()}</p>
</>
)
}
...@@ -85,7 +85,7 @@ export class NextDeployInstance extends NextInstance { ...@@ -85,7 +85,7 @@ export class NextDeployInstance extends NextInstance {
[ [
'deploy', 'deploy',
'--build-env', '--build-env',
'NEXT_PRIVATE_TEST_MODE=1', 'NEXT_PRIVATE_TEST_MODE=e2e',
'--build-env', '--build-env',
'NEXT_TELEMETRY_DISABLED=1', 'NEXT_TELEMETRY_DISABLED=1',
...additionalEnv, ...additionalEnv,
......
...@@ -47,7 +47,7 @@ export class NextDevInstance extends NextInstance { ...@@ -47,7 +47,7 @@ export class NextDevInstance extends NextInstance {
...this.env, ...this.env,
NODE_ENV: '' as any, NODE_ENV: '' as any,
PORT: this.forcedPort || '0', PORT: this.forcedPort || '0',
__NEXT_TEST_MODE: '1', __NEXT_TEST_MODE: 'e2e',
__NEXT_TEST_WITH_DEVTOOL: '1', __NEXT_TEST_WITH_DEVTOOL: '1',
}, },
}) })
......
...@@ -50,7 +50,7 @@ export class NextStartInstance extends NextInstance { ...@@ -50,7 +50,7 @@ export class NextStartInstance extends NextInstance {
...this.env, ...this.env,
NODE_ENV: '' as any, NODE_ENV: '' as any,
PORT: this.forcedPort || '0', PORT: this.forcedPort || '0',
__NEXT_TEST_MODE: '1', __NEXT_TEST_MODE: 'e2e',
}, },
} }
let buildArgs = ['yarn', 'next', 'build'] let buildArgs = ['yarn', 'next', 'build']
......
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