Unverified Commit e4425697 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update manual revalidate response cache handling (#36462)

This updates to ensure we only resolve a manual revalidate request when the cache has been fully set instead of eagerly like we do for generating pages in `fallback` mode. This also ensures the response cache keys manual revalidate requests separate from normal requests to ensure one doesn't block the other. This should resolve the below flakey tests.  

x-ref: https://github.com/vercel/next.js/runs/6163218261?check_suite_focus=true
x-ref: https://github.com/vercel/next.js/runs/6157328196?check_suite_focus=true
x-ref: https://github.com/vercel/next.js/runs/6151165153?check_suite_focus=true
parent 7e7d7bbc
......@@ -101,7 +101,14 @@ export default class ResponseCache {
isManualRevalidate?: boolean
}
): Promise<ResponseCacheEntry | null> {
const pendingResponse = key ? this.pendingResponses.get(key) : null
// ensure manual revalidate doesn't block normal requests
const pendingResponseKey = key
? `${key}-${context.isManualRevalidate ? '1' : '0'}`
: null
const pendingResponse = pendingResponseKey
? this.pendingResponses.get(pendingResponseKey)
: null
if (pendingResponse) {
return pendingResponse
}
......@@ -114,15 +121,18 @@ export default class ResponseCache {
rejecter = reject
}
)
if (key) {
this.pendingResponses.set(key, promise)
if (pendingResponseKey) {
this.pendingResponses.set(pendingResponseKey, promise)
}
let resolved = false
const resolve = (cacheEntry: ResponseCacheEntry | null) => {
if (key) {
if (pendingResponseKey) {
// Ensure all reads from the cache get the latest value.
this.pendingResponses.set(key, Promise.resolve(cacheEntry))
this.pendingResponses.set(
pendingResponseKey,
Promise.resolve(cacheEntry)
)
}
if (!resolved) {
resolved = true
......@@ -133,13 +143,13 @@ export default class ResponseCache {
// we keep the previous cache entry around to leverage
// when the incremental cache is disabled in minimal mode
if (
key &&
pendingResponseKey &&
this.minimalMode &&
this.previousCacheItem?.key === key &&
this.previousCacheItem?.key === pendingResponseKey &&
this.previousCacheItem.expiresAt > Date.now()
) {
resolve(this.previousCacheItem.entry)
this.pendingResponses.delete(key)
this.pendingResponses.delete(pendingResponseKey)
return promise
}
......@@ -173,19 +183,23 @@ export default class ResponseCache {
}
const cacheEntry = await responseGenerator(resolved, !!cachedResponse)
resolve(
const resolveValue =
cacheEntry === null
? null
: {
...cacheEntry,
isMiss: !cachedResponse,
}
)
// for manual revalidate wait to resolve until cache is set
if (!context.isManualRevalidate) {
resolve(resolveValue)
}
if (key && cacheEntry && typeof cacheEntry.revalidate !== 'undefined') {
if (this.minimalMode) {
this.previousCacheItem = {
key,
key: pendingResponseKey || key,
entry: cacheEntry,
expiresAt: Date.now() + 1000,
}
......@@ -205,6 +219,10 @@ export default class ResponseCache {
} else {
this.previousCacheItem = undefined
}
if (context.isManualRevalidate) {
resolve(resolveValue)
}
} catch (err) {
// when a getStaticProps path is erroring we automatically re-set the
// existing cache under a new expiration to prevent non-stop retrying
......@@ -223,8 +241,8 @@ export default class ResponseCache {
rejecter(err as Error)
}
} finally {
if (key) {
this.pendingResponses.delete(key)
if (pendingResponseKey) {
this.pendingResponses.delete(pendingResponseKey)
}
}
})()
......
import fs from 'fs-extra'
import cheerio from 'cheerio'
import { join, sep } from 'path'
import escapeRegex from 'escape-string-regexp'
......@@ -49,6 +50,39 @@ describe('Prerender', () => {
})
afterAll(() => next.destroy())
async function waitForCacheWrite(
prerenderPath = '',
timeBeforeRevalidateMilliseconds,
retries = 30
) {
for (let i = 0; i < retries; i++) {
const lastRetry = i === retries - 1
const jsonPath = join(
next.testDir,
'.next',
'server',
'pages',
`${prerenderPath}.html`
)
try {
const jsonStats = await fs.stat(jsonPath)
const jsonLastModified = jsonStats.mtime.getTime()
if (timeBeforeRevalidateMilliseconds <= jsonLastModified) {
break
}
throw new Error(
`revalidate cache not past ${timeBeforeRevalidateMilliseconds} received time ${jsonLastModified}`
)
} catch (err) {
if (lastRetry) {
throw err
}
}
await waitFor(500)
}
}
function isCachingHeader(cacheControl) {
return !cacheControl || !/no-store/.test(cacheControl)
}
......@@ -2014,30 +2048,30 @@ describe('Prerender', () => {
})
it('should handle manual revalidate for fallback: blocking', async () => {
const beforeRevalidate = Date.now()
const res = await fetchViaHTTP(
next.url,
'/blocking-fallback/test-manual-1'
)
await waitForCacheWrite(
'/blocking-fallback/test-manual-1',
beforeRevalidate
)
const html = await res.text()
const $ = cheerio.load(html)
const initialTime = $('#time').text()
expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/)
expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/)
expect($('p').text()).toMatch(/Post:.*?test-manual-1/)
let $2
await check(async () => {
const res2 = await fetchViaHTTP(
next.url,
'/blocking-fallback/test-manual-1'
)
const html2 = await res2.text()
$2 = cheerio.load(html2)
return res2.headers.get('x-nextjs-cache')
}, /(HIT|STALE)/)
const res2 = await fetchViaHTTP(
next.url,
'/blocking-fallback/test-manual-1'
)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect(res2.headers.get('x-nextjs-cache')).toMatch(/(HIT|STALE)/)
expect(initialTime).toBe($2('#time').text())
const res3 = await fetchViaHTTP(
......@@ -2091,15 +2125,19 @@ describe('Prerender', () => {
})
it('should manual revalidate for fallback: blocking with onlyGenerated if generated', async () => {
const beforeRevalidate = Date.now()
const res = await fetchViaHTTP(
next.url,
'/blocking-fallback/test-if-generated-2'
)
await waitForCacheWrite(
'/blocking-fallback/test-if-generated-2',
beforeRevalidate
)
const html = await res.text()
const $ = cheerio.load(html)
const initialTime = $('#time').text()
expect(res.headers.get('x-nextjs-cache')).toMatch(/MISS/)
expect($('p').text()).toMatch(/Post:.*?test-if-generated-2/)
const res2 = await fetchViaHTTP(
......
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