Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: types for collected cache info in route modules #71434

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/next/src/export/routes/app-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ export async function exportAppRoute(
await afterRunner.executeAfter()

const revalidate =
typeof (context.renderOpts as any).collectedRevalidate === 'undefined' ||
(context.renderOpts as any).collectedRevalidate >= INFINITE_CACHE
typeof context.renderOpts.collectedRevalidate === 'undefined' ||
context.renderOpts.collectedRevalidate >= INFINITE_CACHE
? false
: (context.renderOpts as any).collectedRevalidate
: context.renderOpts.collectedRevalidate

const headers = toNodeOutgoingHttpHeaders(response.headers)
const cacheTags = (context.renderOpts as any).collectedTags
const cacheTags = context.renderOpts.collectedTags

if (cacheTags) {
headers[NEXT_CACHE_TAGS_HEADER] = cacheTags
Expand Down
10 changes: 4 additions & 6 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,7 @@ export default abstract class Server<
context.renderOpts as any
).fetchMetrics

const cacheTags = (context.renderOpts as any).collectedTags
const cacheTags = context.renderOpts.collectedTags

// If the request is for a static response, we can cache it so long
// as it's not edge.
Expand All @@ -2525,12 +2525,10 @@ export default abstract class Server<
}

const revalidate =
typeof (context.renderOpts as any).collectedRevalidate ===
'undefined' ||
(context.renderOpts as any).collectedRevalidate >=
INFINITE_CACHE
typeof context.renderOpts.collectedRevalidate === 'undefined' ||
context.renderOpts.collectedRevalidate >= INFINITE_CACHE
? false
: (context.renderOpts as any).collectedRevalidate
: context.renderOpts.collectedRevalidate

// Create the cache entry for the response.
const cacheEntry: ResponseCacheEntry = {
Expand Down
20 changes: 13 additions & 7 deletions packages/next/src/server/route-modules/app-route/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,18 @@ export type AppRouteModule = typeof import('../../../build/templates/app-route')
*/
export interface AppRouteRouteHandlerContext extends RouteModuleHandleContext {
renderOpts: WorkStoreContext['renderOpts'] &
Pick<RenderOptsPartial, 'onInstrumentationRequestError'>
Pick<RenderOptsPartial, 'onInstrumentationRequestError'> &
CollectedCacheInfo
prerenderManifest: DeepReadonly<PrerenderManifest>
}

type CollectedCacheInfo = {
collectedTags?: string
collectedRevalidate?: number
collectedExpire?: number
collectedStale?: number
}

/**
* AppRouteHandlerFnContext is the context that is passed to the handler as the
* second argument.
Expand Down Expand Up @@ -590,12 +598,10 @@ export class AppRouteRouteModule extends RouteModule<
])

if (prerenderStore) {
;(context.renderOpts as any).collectedTags =
prerenderStore.tags?.join(',')
;(context.renderOpts as any).collectedRevalidate =
prerenderStore.revalidate
;(context.renderOpts as any).collectedExpire = prerenderStore.expire
;(context.renderOpts as any).collectedStale = prerenderStore.stale
context.renderOpts.collectedTags = prerenderStore.tags?.join(',')
context.renderOpts.collectedRevalidate = prerenderStore.revalidate
context.renderOpts.collectedExpire = prerenderStore.expire
context.renderOpts.collectedStale = prerenderStore.stale
}

// It's possible cookies were set in the handler, so we need
Expand Down
Loading