Unverified Commit cc484b99 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

refactor: update fallbacks to use response cache (#68603)

This replaces the custom behaviour of `getFallback` in the server with
the existing ResponseCache. This sets us up for #68958 which has
fallbacks that should be revalidatable.
parent 28a5eaa8
......@@ -165,6 +165,7 @@ import { getRevalidateReason } from './instrumentation/utils'
import { RouteKind } from './route-kind'
import type { RouteModule } from './route-modules/route-module'
import { FallbackMode, parseFallbackField } from '../lib/fallback'
import { toResponseCacheEntry } from './response-cache/utils'
export type FindComponentsResult = {
components: LoadComponentsReturnType
......@@ -378,7 +379,6 @@ export default abstract class Server<
req: ServerRequest,
parsedUrl: NextUrlWithParsedQuery
): void
protected abstract getFallback(page: string): Promise<string>
protected abstract hasPage(pathname: string): Promise<boolean>
protected abstract sendRenderResult(
......@@ -2318,7 +2318,7 @@ export default abstract class Server<
} catch {}
// use existing incrementalCache instance if available
const incrementalCache =
const incrementalCache: import('./lib/incremental-cache').IncrementalCache =
(globalThis as any).__incrementalCache ||
(await this.getIncrementalCache({
requestHeaders: Object.assign({}, req.headers),
......@@ -2709,11 +2709,11 @@ export default abstract class Server<
}
}
const responseGenerator: ResponseGenerator = async (
const responseGenerator: ResponseGenerator = async ({
hasResolved,
previousCacheEntry,
isRevalidating
): Promise<ResponseCacheEntry | null> => {
isRevalidating,
}): Promise<ResponseCacheEntry | null> => {
const isProduction = !this.renderOpts.dev
const didRespond = hasResolved || res.sent
......@@ -2823,37 +2823,51 @@ export default abstract class Server<
throw new NoFallbackError()
}
if (!isNextDataRequest) {
// Production already emitted the fallback as static HTML.
if (isProduction) {
const html = await this.getFallback(
locale ? `/${locale}${pathname}` : pathname
)
if (!isNextDataRequest && !isRSCRequest) {
const key = isProduction
? isAppPath
? pathname
: locale
? `/${locale}${pathname}`
: pathname
: null
const fallbackResponse = await this.responseCache.get(
key,
async ({ previousCacheEntry: previousFallbackCacheEntry }) => {
// For the pages router, fallbacks cannot be revalidated or
// generated in production. In the case of a missing fallback,
// we return null, but if it's being revalidated, we just return
// the previous fallback cache entry. This preserves the previous
// behavior.
if (isProduction) {
if (!previousFallbackCacheEntry) {
return null
}
return {
value: {
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(html),
pageData: {},
status: undefined,
headers: undefined,
},
}
}
// We need to generate the fallback on-demand for development.
else {
query.__nextFallback = 'true'
// We pass `undefined` as there cannot be a postponed state in
// development.
const result = await doRender({ postponed: undefined })
if (!result) {
return null
return toResponseCacheEntry(previousFallbackCacheEntry)
}
query.__nextFallback = 'true'
// We pass `undefined` as rendering a fallback isn't resumed here.
return doRender({ postponed: undefined })
},
{
routeKind: isAppPath ? RouteKind.APP_PAGE : RouteKind.PAGES,
incrementalCache,
isRoutePPREnabled,
isFallback: true,
}
// Prevent caching this result
delete result.revalidate
return result
}
)
if (!fallbackResponse) return null
// Remove the revalidate from the response to prevent it from being
// used in the surrounding cache.
delete fallbackResponse.revalidate
return fallbackResponse
}
}
......@@ -2910,6 +2924,7 @@ export default abstract class Server<
isOnDemandRevalidate,
isPrefetch: req.headers.purpose === 'prefetch',
isRoutePPREnabled,
isFallback: false,
}
)
......
......@@ -174,7 +174,7 @@ export default class FileSystemCache implements CacheHandler {
public async get(...args: Parameters<CacheHandler['get']>) {
const [key, ctx] = args
const { tags, softTags, kind, isRoutePPREnabled } = ctx
const { tags, softTags, kind, isRoutePPREnabled, isFallback } = ctx
let data = memoryCache?.get(key)
......@@ -252,24 +252,27 @@ export default class FileSystemCache implements CacheHandler {
}
} else if (kind === IncrementalCacheKind.APP_PAGE) {
let meta: RouteMetadata | undefined
let rscData: Buffer | undefined
// We try to load the metadata file, but if it fails, we don't
// error.
try {
meta = JSON.parse(
await this.fs.readFile(
filePath.replace(/\.html$/, NEXT_META_SUFFIX),
'utf8'
// error. We also don't load it if this is a fallback.
if (!isFallback) {
try {
meta = JSON.parse(
await this.fs.readFile(
filePath.replace(/\.html$/, NEXT_META_SUFFIX),
'utf8'
)
)
)
} catch {}
} catch {}
const rscData = await this.fs.readFile(
this.getFilePath(
`${key}${isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`,
IncrementalCacheKind.APP_PAGE
rscData = await this.fs.readFile(
this.getFilePath(
`${key}${isRoutePPREnabled ? RSC_PREFETCH_SUFFIX : RSC_SUFFIX}`,
IncrementalCacheKind.APP_PAGE
)
)
)
}
data = {
lastModified: mtime.getTime(),
......@@ -284,16 +287,19 @@ export default class FileSystemCache implements CacheHandler {
}
} else if (kind === IncrementalCacheKind.PAGES) {
let meta: RouteMetadata | undefined
let pageData: string | object = {}
const pageData = JSON.parse(
await this.fs.readFile(
this.getFilePath(
`${key}${NEXT_DATA_SUFFIX}`,
IncrementalCacheKind.PAGES
),
'utf8'
if (!isFallback) {
pageData = JSON.parse(
await this.fs.readFile(
this.getFilePath(
`${key}${NEXT_DATA_SUFFIX}`,
IncrementalCacheKind.PAGES
),
'utf8'
)
)
)
}
data = {
lastModified: mtime.getTime(),
......@@ -376,6 +382,7 @@ export default class FileSystemCache implements CacheHandler {
public async set(...args: Parameters<CacheHandler['set']>) {
const [key, data, ctx] = args
const { isFallback } = ctx
memoryCache?.set(key, {
value: data,
lastModified: Date.now(),
......@@ -417,25 +424,30 @@ export default class FileSystemCache implements CacheHandler {
await this.fs.mkdir(path.dirname(htmlPath))
await this.fs.writeFile(htmlPath, data.html)
await this.fs.writeFile(
this.getFilePath(
`${key}${
// Fallbacks don't generate a data file.
if (!isFallback) {
await this.fs.writeFile(
this.getFilePath(
`${key}${
isAppPath
? ctx.isRoutePPREnabled
? RSC_PREFETCH_SUFFIX
: RSC_SUFFIX
: NEXT_DATA_SUFFIX
}`,
isAppPath
? ctx.isRoutePPREnabled
? RSC_PREFETCH_SUFFIX
: RSC_SUFFIX
: NEXT_DATA_SUFFIX
}`,
isAppPath ? IncrementalCacheKind.APP_PAGE : IncrementalCacheKind.PAGES
),
isAppPath ? data.rscData : JSON.stringify(data.pageData)
)
? IncrementalCacheKind.APP_PAGE
: IncrementalCacheKind.PAGES
),
isAppPath ? data.rscData : JSON.stringify(data.pageData)
)
}
if (data.headers || data.status || (isAppPath && data.postponed)) {
if (data?.kind === CachedRouteKind.APP_PAGE) {
const meta: RouteMetadata = {
headers: data.headers,
status: data.status,
postponed: isAppPath ? data.postponed : undefined,
postponed: data.postponed,
}
await this.fs.writeFile(
......
......@@ -190,16 +190,17 @@ export class IncrementalCache implements IncrementalCacheType {
private calculateRevalidate(
pathname: string,
fromTime: number,
dev?: boolean
dev: boolean,
isFallback: boolean | undefined
): Revalidate {
// in development we don't have a prerender-manifest
// and default to always revalidating to allow easier debugging
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.
// of revalidating after 1 second unless it's a fallback request.
const initialRevalidateSeconds =
this.revalidateTimings.get(toRoute(pathname)) ?? 1
this.revalidateTimings.get(toRoute(pathname)) ?? (isFallback ? false : 1)
const revalidateAfter =
typeof initialRevalidateSeconds === 'number'
......@@ -384,6 +385,7 @@ export class IncrementalCache implements IncrementalCacheType {
tags?: string[]
softTags?: string[]
isRoutePPREnabled?: boolean
isFallback: boolean | undefined
}
): Promise<IncrementalCacheEntry | null> {
// we don't leverage the prerender cache in dev mode
......@@ -446,7 +448,8 @@ export class IncrementalCache implements IncrementalCacheType {
revalidateAfter = this.calculateRevalidate(
cacheKey,
cacheData?.lastModified || Date.now(),
this.dev && ctx.kind !== IncrementalCacheKind.FETCH
this.dev ? ctx.kind !== IncrementalCacheKind.FETCH : false,
ctx.isFallback
)
isStale =
revalidateAfter !== false && revalidateAfter < Date.now()
......@@ -494,6 +497,7 @@ export class IncrementalCache implements IncrementalCacheType {
fetchIdx?: number
tags?: string[]
isRoutePPREnabled?: boolean
isFallback?: boolean
}
) {
if (this.disableForTestmode || (this.dev && !ctx.fetchCache)) return
......
......@@ -676,6 +676,7 @@ export function createPatchedFetcher(
fetchIdx,
tags,
softTags: implicitTags,
isFallback: false,
})
if (entry) {
......
......@@ -797,15 +797,6 @@ export default class NextNodeServer extends BaseServer<
) as NextFontManifest
}
protected getFallback(page: string): Promise<string> {
page = normalizePagePath(page)
const cacheFs = this.getCacheFilesystem()
return cacheFs.readFile(
join(this.serverDistDir, 'pages', `${page}.html`),
'utf8'
)
}
protected handleNextImageRequest: NodeRouteHandler = async (
req,
res,
......@@ -887,6 +878,7 @@ export default class NextNodeServer extends BaseServer<
{
routeKind: RouteKind.IMAGE,
incrementalCache: imageOptimizerCache,
isFallback: false,
}
)
......
......@@ -58,13 +58,21 @@ export default class ResponseCache implements ResponseCacheBase {
isPrefetch?: boolean
incrementalCache: IncrementalCache
isRoutePPREnabled?: boolean
isFallback: boolean
}
): Promise<ResponseCacheEntry | null> {
// If there is no key for the cache, we can't possibly look this up in the
// cache so just return the result of the response generator.
if (!key) return responseGenerator(false, null)
if (!key) {
return responseGenerator({ hasResolved: false, previousCacheEntry: null })
}
const { incrementalCache, isOnDemandRevalidate = false } = context
const {
incrementalCache,
isOnDemandRevalidate = false,
isFallback = false,
isRoutePPREnabled = false,
} = context
const response = await this.batcher.batch(
{ key, isOnDemandRevalidate },
......@@ -89,6 +97,7 @@ export default class ResponseCache implements ResponseCacheBase {
? await incrementalCache.get(key, {
kind,
isRoutePPREnabled: context.isRoutePPREnabled,
isFallback,
})
: null
......@@ -112,11 +121,11 @@ export default class ResponseCache implements ResponseCacheBase {
}
}
const cacheEntry = await responseGenerator(
resolved,
cachedResponse,
true
)
const cacheEntry = await responseGenerator({
hasResolved: resolved,
previousCacheEntry: cachedResponse,
isRevalidating: true,
})
// If the cache entry couldn't be generated, we don't want to cache
// the result.
......@@ -143,6 +152,8 @@ export default class ResponseCache implements ResponseCacheBase {
resolved = true
}
// We want to persist the result only if it has a revalidate value
// defined.
if (typeof resolveValue.revalidate !== 'undefined') {
if (this.minimalMode) {
this.previousCacheItem = {
......@@ -153,7 +164,8 @@ export default class ResponseCache implements ResponseCacheBase {
} else {
await incrementalCache.set(key, resolveValue.value, {
revalidate: resolveValue.revalidate,
isRoutePPREnabled: context.isRoutePPREnabled,
isRoutePPREnabled,
isFallback,
})
}
}
......@@ -168,7 +180,8 @@ export default class ResponseCache implements ResponseCacheBase {
Math.max(cachedResponse.revalidate || 3, 3),
30
),
isRoutePPREnabled: context.isRoutePPREnabled,
isRoutePPREnabled,
isFallback,
})
}
......
......@@ -18,6 +18,14 @@ export interface ResponseCacheBase {
*/
routeKind: RouteKind
/**
* True if this is a fallback request.
*/
isFallback: boolean
/**
* True if the route is enabled for PPR.
*/
isRoutePPREnabled?: boolean
}
): Promise<ResponseCacheEntry | null>
......@@ -155,11 +163,11 @@ export type ResponseCacheEntry = {
* @param hasResolved whether the responseGenerator has resolved it's promise
* @param previousCacheEntry the previous cache entry if it exists or the current
*/
export type ResponseGenerator = (
hasResolved: boolean,
previousCacheEntry?: IncrementalCacheItem,
export type ResponseGenerator = (state: {
hasResolved: boolean
previousCacheEntry?: IncrementalCacheItem
isRevalidating?: boolean
) => Promise<ResponseCacheEntry | null>
}) => Promise<ResponseCacheEntry | null>
export type IncrementalCacheItem = {
revalidateAfter?: number | false
......@@ -184,7 +192,15 @@ export interface IncrementalCache {
ctx: {
kind: IncrementalCacheKind
/**
* True if the route is enabled for PPR.
*/
isRoutePPREnabled?: boolean
/**
* True if this is a fallback request.
*/
isFallback: boolean
}
) => Promise<IncrementalCacheItem>
set: (
......@@ -192,7 +208,16 @@ export interface IncrementalCache {
data: IncrementalCacheValue | null,
ctx: {
revalidate: Revalidate
/**
* True if the route is enabled for PPR.
*/
isRoutePPREnabled?: boolean
/**
* True if this is a fallback request.
*/
isFallback: boolean
}
) => Promise<void>
}
......@@ -51,7 +51,7 @@ export default class WebResponseCache {
this.pendingResponses.set(pendingResponseKey, promise)
}
let resolved = false
let hasResolved = false
const resolve = (cacheEntry: ResponseCacheEntry | null) => {
if (pendingResponseKey) {
// Ensure all reads from the cache get the latest value.
......@@ -60,8 +60,8 @@ export default class WebResponseCache {
Promise.resolve(cacheEntry)
)
}
if (!resolved) {
resolved = true
if (!hasResolved) {
hasResolved = true
resolver(cacheEntry)
}
}
......@@ -84,7 +84,7 @@ export default class WebResponseCache {
// same promise until we've fully finished our work.
;(async () => {
try {
const cacheEntry = await responseGenerator(resolved)
const cacheEntry = await responseGenerator({ hasResolved })
const resolveValue =
cacheEntry === null
? null
......@@ -114,7 +114,7 @@ export default class WebResponseCache {
} catch (err) {
// while revalidating in the background we can't reject as
// we already resolved the cache entry so log the error here
if (resolved) {
if (hasResolved) {
console.error(err)
} else {
rejecter(err as Error)
......
......@@ -355,10 +355,6 @@ export default class NextWebServer extends BaseServer<
return false
}
protected async getFallback() {
return ''
}
protected getFontManifest() {
return undefined
}
......
......@@ -196,6 +196,7 @@ export function unstable_cache<T extends Callback>(
softTags: implicitTags,
fetchIdx,
fetchUrl,
isFallback: false,
})
if (cacheEntry && cacheEntry.value) {
......@@ -309,6 +310,7 @@ export function unstable_cache<T extends Callback>(
fetchIdx,
fetchUrl,
softTags: implicitTags,
isFallback: false,
})
if (cacheEntry && cacheEntry.value) {
......
......@@ -186,7 +186,7 @@ describe('ppr-full', () => {
}
})
if (dynamic === true) {
if (dynamic === true && !isNextDev) {
it('should cache the static part', async () => {
const delay = 500
......
......@@ -40,6 +40,7 @@ describe('FileSystemCache', () => {
(
await fsCache.get('icon.png', {
kind: IncrementalCacheKind.APP_ROUTE,
isFallback: undefined,
})
)?.value
).toEqual({
......@@ -88,6 +89,7 @@ describe('FileSystemCache (isrMemory 0)', () => {
const res = await fsCache.get('fetch-cache', {
tags: ['server-time'],
kind: IncrementalCacheKind.FETCH,
isFallback: undefined,
})
expect(res?.value).toEqual({
......@@ -117,6 +119,7 @@ describe('FileSystemCache (isrMemory 0)', () => {
const res = await fsCache.get('unstable-cache', {
tags: ['server-time'],
kind: IncrementalCacheKind.FETCH,
isFallback: undefined,
})
expect(res?.value).toEqual({
......
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