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 16, 2024
1 parent 5aae257 commit 9d40f89
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 44 deletions.
36 changes: 13 additions & 23 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2330,17 +2330,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 @@ -2504,7 +2499,6 @@ export default abstract class Server<
body: Buffer.from(await blob.arrayBuffer()),
headers,
},
isFallback: false,
revalidate,
}

Expand Down Expand Up @@ -2665,7 +2659,6 @@ export default abstract class Server<
return {
value: null,
revalidate: metadata.revalidate,
isFallback: false,
}
}

Expand All @@ -2677,7 +2670,6 @@ export default abstract class Server<
props: metadata.pageData ?? metadata.flightData,
} satisfies CachedRedirectValue,
revalidate: metadata.revalidate,
isFallback: false,
}
}

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

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

Expand Down Expand Up @@ -2838,16 +2828,13 @@ export default abstract class Server<
: pathname
: null

return this.responseCache.get(
const response = await this.responseCache.get(
key,
async () => {
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 @@ -2856,6 +2843,14 @@ export default abstract class Server<
isFallback: true,
}
)

if (!response) return null

// Remove the revalidate from the response to prevent it from being
// used in the surrounding cache.
delete response.revalidate

return response
}
}

Expand All @@ -2866,7 +2861,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 @@ -2877,7 +2871,6 @@ export default abstract class Server<
) {
return {
revalidate: 1,
isFallback: false,
value: {
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(''),
Expand Down Expand Up @@ -3257,10 +3250,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 @@ -871,7 +871,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 9d40f89

Please # to comment.