Skip to content

Commit

Permalink
refactor: undo changes to cache entries
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Aug 20, 2024
1 parent 3ed55a3 commit e44ee41
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 49 deletions.
57 changes: 29 additions & 28 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2335,17 +2336,12 @@ export default abstract class Server<
* a render that has been postponed.
*/
postponed: string | undefined

/**
* Whether or not this render is a fallback render.
*/
isFallback: boolean | undefined
}
type Renderer = (
context: RendererContext
) => Promise<ResponseCacheEntry | null>

const doRender: Renderer = async ({ postponed, isFallback }) => {
const doRender: Renderer = async ({ postponed }) => {
// In development, we always want to generate dynamic HTML.
let supportsDynamicResponse: boolean =
// If we're in development, we always support dynamic HTML, unless it's
Expand Down Expand Up @@ -2510,7 +2506,6 @@ export default abstract class Server<
body: Buffer.from(await blob.arrayBuffer()),
headers,
},
isFallback: false,
revalidate,
}

Expand Down Expand Up @@ -2668,11 +2663,7 @@ export default abstract class Server<

// Handle `isNotFound`.
if ('isNotFound' in metadata && metadata.isNotFound) {
return {
value: null,
revalidate: metadata.revalidate,
isFallback: false,
}
return { value: null, revalidate: metadata.revalidate }
}

// Handle `isRedirect`.
Expand All @@ -2683,7 +2674,6 @@ export default abstract class Server<
props: metadata.pageData ?? metadata.flightData,
} satisfies CachedRedirectValue,
revalidate: metadata.revalidate,
isFallback: false,
}
}

Expand All @@ -2704,7 +2694,6 @@ export default abstract class Server<
status: res.statusCode,
} satisfies CachedAppPageValue,
revalidate: metadata.revalidate,
isFallback,
}
}

Expand All @@ -2717,7 +2706,6 @@ export default abstract class Server<
status: isAppPath ? res.statusCode : undefined,
} satisfies CachedPageValue,
revalidate: metadata.revalidate,
isFallback,
}
}

Expand Down Expand Up @@ -2844,16 +2832,26 @@ export default abstract class Server<
: pathname
: null

return this.responseCache.get(
const fallbackResponse = await this.responseCache.get(
key,
async () => {
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 toResponseCacheEntry(previousFallbackCacheEntry)
}

query.__nextFallback = 'true'

return doRender({
// We pass `undefined` as rendering a fallback isn't resumed here.
postponed: undefined,
isFallback: true,
})
// We pass `undefined` as rendering a fallback isn't resumed here.
return doRender({ postponed: undefined })
},
{
routeKind: isAppPath ? RouteKind.APP_PAGE : RouteKind.PAGES,
Expand All @@ -2862,6 +2860,14 @@ export default abstract class Server<
isFallback: true,
}
)

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
}
}

Expand All @@ -2872,7 +2878,6 @@ export default abstract class Server<
!isOnDemandRevalidate && !isRevalidating && minimalPostponed
? minimalPostponed
: undefined,
isFallback: false,
}

// When we're in minimal mode, if we're trying to debug the static shell,
Expand All @@ -2883,7 +2888,6 @@ export default abstract class Server<
) {
return {
revalidate: 1,
isFallback: false,
value: {
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(''),
Expand Down Expand Up @@ -3263,10 +3267,7 @@ export default abstract class Server<
// Perform the render again, but this time, provide the postponed state.
// We don't await because we want the result to start streaming now, and
// we've already chained the transformer's readable to the render result.
doRender({
postponed: cachedData.postponed,
isFallback: false,
})
doRender({ postponed: cachedData.postponed })
.then(async (result) => {
if (!result) {
throw new Error('Invariant: expected a result to be returned')
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ export class ImageOptimizerCache {
revalidateAfter:
Math.max(maxAge, this.nextConfig.images.minimumCacheTTL) * 1000 +
Date.now(),
isFallback: false,
curRevalidate: maxAge,
isStale: now > expireAt,
}
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ export class IncrementalCache implements IncrementalCacheType {

return {
isStale: isStale,
isFallback: false,
value: {
kind: CachedRouteKind.FETCH,
data,
Expand Down Expand Up @@ -464,7 +463,6 @@ export class IncrementalCache implements IncrementalCacheType {
curRevalidate,
revalidateAfter,
value: cacheData.value,
isFallback: ctx.isFallback,
}
}

Expand All @@ -482,7 +480,6 @@ export class IncrementalCache implements IncrementalCacheType {
value: null,
curRevalidate,
revalidateAfter,
isFallback: ctx.isFallback,
}
this.set(cacheKey, entry.value, ctx)
}
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,6 @@ export default class NextNodeServer extends BaseServer<
etag,
extension: getExtension(contentType) as string,
},
isFallback: false,
revalidate: maxAge,
}
},
Expand Down
14 changes: 2 additions & 12 deletions packages/next/src/server/response-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ export default class ResponseCache implements ResponseCacheBase {
})
: null

// Ensure that cached responses that are fallbacks are marked as such.
if (cachedResponse) {
cachedResponse.isFallback = isFallback
}

if (cachedResponse && !isOnDemandRevalidate) {
if (cachedResponse.value?.kind === CachedRouteKind.FETCH) {
throw new Error(
Expand Down Expand Up @@ -158,13 +153,8 @@ export default class ResponseCache implements ResponseCacheBase {
}

// We want to persist the result only if it has a revalidate value
// defined and it's not the fallback result. The fallback result has
// it's own wrapping cache.
if (
typeof resolveValue.revalidate !== 'undefined' &&
(!resolveValue.isFallback ||
(resolveValue.isFallback && isFallback))
) {
// defined.
if (typeof resolveValue.revalidate !== 'undefined') {
if (this.minimalMode) {
this.previousCacheItem = {
key: cacheKey,
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/server/response-cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export type IncrementalCacheEntry = {
revalidateAfter: Revalidate
// -1 here dictates a blocking revalidate should be used
isStale?: boolean | -1
isFallback: boolean | undefined
value: IncrementalCacheValue | null
}

Expand All @@ -158,7 +157,6 @@ export type ResponseCacheEntry = {
value: ResponseCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
isFallback: boolean | undefined
}

/**
Expand All @@ -178,7 +176,6 @@ export type IncrementalCacheItem = {
value: IncrementalCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
isFallback?: boolean
} | null

export const enum IncrementalCacheKind {
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/response-cache/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export async function toResponseCacheEntry(
return {
isMiss: response.isMiss,
isStale: response.isStale,
isFallback: response.isFallback,
revalidate: response.revalidate,
value:
response.value?.kind === CachedRouteKind.PAGES
Expand Down

0 comments on commit e44ee41

Please # to comment.