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

Update app cache handling in development (#45248)

parent 2725a379
......@@ -257,13 +257,10 @@ function createErrorHandler(
}
}
let isFetchPatched = false
// we patch fetch to collect cache information used for
// determining if a page is static or not
function patchFetch(ComponentMod: any) {
if (isFetchPatched) return
isFetchPatched = true
if ((globalThis.fetch as any).patched) return
const { DynamicServerError } =
ComponentMod.serverHooks as typeof import('../client/components/hooks-server-context')
......@@ -448,6 +445,7 @@ function patchFetch(ComponentMod: any) {
return doOriginalFetch()
}
;(globalThis.fetch as any).patched = true
}
interface FlightResponseRef {
......
......@@ -100,7 +100,7 @@ export class IncrementalCache {
this.cacheHandler = new (cacheHandlerMod as typeof CacheHandler)({
dev,
fs,
flushToDisk,
flushToDisk: flushToDisk && !dev,
serverDistDir,
maxMemoryCacheSize,
_appDir: !!appDir,
......@@ -110,11 +110,12 @@ export class IncrementalCache {
private calculateRevalidate(
pathname: string,
fromTime: number
fromTime: number,
dev?: boolean
): number | false {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
if (this.dev) return new Date().getTime() - 1000
if (dev) return new Date().getTime() - 1000
// if an entry isn't present in routes we fallback to a default
// of revalidating after 1 second
......@@ -174,7 +175,12 @@ export class IncrementalCache {
): Promise<IncrementalCacheEntry | null> {
// we don't leverage the prerender cache in dev mode
// so that getStaticProps is always called for easier debugging
if (this.dev) return null
if (
this.dev &&
(!fetchCache || this.requestHeaders['cache-control'] === 'no-cache')
) {
return null
}
pathname = this._getPathname(pathname, fetchCache)
let entry: IncrementalCacheEntry | null = null
......@@ -205,7 +211,8 @@ export class IncrementalCache {
this.prerenderManifest.routes[toRoute(pathname)]?.initialRevalidateSeconds
const revalidateAfter = this.calculateRevalidate(
pathname,
cacheData?.lastModified || Date.now()
cacheData?.lastModified || Date.now(),
this.dev && !fetchCache
)
const isStale =
revalidateAfter !== false && revalidateAfter < Date.now()
......@@ -248,7 +255,7 @@ export class IncrementalCache {
revalidateSeconds?: number | false,
fetchCache?: boolean
) {
if (this.dev) return
if (this.dev && !fetchCache) return
pathname = this._getPathname(pathname, fetchCache)
try {
......
......@@ -3,7 +3,7 @@ import cheerio from 'cheerio'
import { promisify } from 'util'
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import { check, normalizeRegEx, waitFor } from 'next-test-utils'
import { check, fetchViaHTTP, normalizeRegEx, waitFor } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
const glob = promisify(globOrig)
......@@ -289,7 +289,38 @@ createNextDescribe(
})
}
if (!isDev) {
if (isDev) {
it('should bypass fetch cache with cache-control: no-cache', async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/revalidate-3'
)
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const layoutData = $('#layout-data').text()
const pageData = $('#page-data').text()
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/revalidate-3',
undefined,
{
headers: {
'cache-control': 'no-cache',
},
}
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#layout-data').text()).not.toBe(layoutData)
expect($2('#page-data').text()).not.toBe(pageData)
})
} else {
it('should properly error when static page switches to dynamic at runtime', async () => {
const res = await next.fetch(
'/static-to-dynamic-error/static-bailout-1'
......@@ -324,6 +355,34 @@ createNextDescribe(
})
}
it('should honor fetch cache correctly', async () => {
await fetchViaHTTP(next.url, '/variable-revalidate/revalidate-3')
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/revalidate-3'
)
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const layoutData = $('#layout-data').text()
const pageData = $('#page-data').text()
for (let i = 0; i < 3; i++) {
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/revalidate-3'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#layout-data').text()).toBe(layoutData)
expect($2('#page-data').text()).toBe(pageData)
}
})
it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => {
const browserOnIndexPage = await next.browser('/ssg-preview')
......
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