Skip to content

Commit

Permalink
Define expire as number | undefined in CacheControl
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable committed Feb 28, 2025
1 parent b08a160 commit 152a496
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export async function exportAppPage(
const { metadata } = result
const {
flightData,
cacheControl = { revalidate: false },
cacheControl = { revalidate: false, expire: undefined },
postponed,
fetchTags,
fetchMetrics,
Expand Down Expand Up @@ -298,7 +298,7 @@ export async function exportAppPage(
})
}

return { cacheControl: { revalidate: 0 }, fetchMetrics }
return { cacheControl: { revalidate: 0, expire: undefined }, fetchMetrics }
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/export/routes/app-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ export async function exportAppRoute(
// unless specifically opted into
experimental.dynamicIO !== true
) {
return { cacheControl: { revalidate: 0 } }
return { cacheControl: { revalidate: 0, expire: undefined } }
}

const response = await module.handle(request, context)

const isValidStatus = response.status < 400 || response.status === 404
if (!isValidStatus) {
return { cacheControl: { revalidate: 0 } }
return { cacheControl: { revalidate: 0, expire: undefined } }
}

const blob = await response.blob()
Expand Down Expand Up @@ -173,6 +173,6 @@ export async function exportAppRoute(
throw err
}

return { cacheControl: { revalidate: 0 } }
return { cacheControl: { revalidate: 0, expire: undefined } }
}
}
5 changes: 4 additions & 1 deletion packages/next/src/export/routes/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ export async function exportPagesPage(

return {
ampValidations,
cacheControl: metadata.cacheControl ?? { revalidate: false },
cacheControl: metadata.cacheControl ?? {
revalidate: false,
expire: undefined,
},
ssgNotFound,
}
}
2 changes: 1 addition & 1 deletion packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ async function renderToHTMLOrFlightImpl(
// If force static is specifically set to false, we should not revalidate
// the page.
if (workStore.forceStatic === false || response.collectedRevalidate === 0) {
metadata.cacheControl = { revalidate: 0 }
metadata.cacheControl = { revalidate: 0, expire: undefined }
} else {
// Copy the cache control value onto the render result metadata.
metadata.cacheControl = {
Expand Down
25 changes: 15 additions & 10 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,7 @@ export default abstract class Server<
typeof postponed !== 'undefined'
) {
return {
cacheControl: { revalidate: 1 },
cacheControl: { revalidate: 1, expire: undefined },
isFallback: false,
value: {
kind: CachedRouteKind.PAGES,
Expand Down Expand Up @@ -3311,7 +3311,7 @@ export default abstract class Server<
// If this is a resume request in minimal mode it is streamed with dynamic
// content and should not be cached.
if (minimalPostponed) {
cacheControl = { revalidate: 0 }
cacheControl = { revalidate: 0, expire: undefined }
}

// If this is in minimal mode and this is a flight request that isn't a
Expand All @@ -3323,18 +3323,18 @@ export default abstract class Server<
!isPrefetchRSCRequest &&
isRoutePPREnabled
) {
cacheControl = { revalidate: 0 }
cacheControl = { revalidate: 0, expire: undefined }
} else if (!this.renderOpts.dev || (hasServerProps && !isNextDataRequest)) {
// If this is a preview mode request, we shouldn't cache it
if (isPreviewMode) {
cacheControl = { revalidate: 0 }
cacheControl = { revalidate: 0, expire: undefined }
}

// If this isn't SSG, then we should set change the header only if it is
// not set already.
else if (!isSSG) {
if (!res.getHeader('Cache-Control')) {
cacheControl = { revalidate: 0 }
cacheControl = { revalidate: 0, expire: undefined }
}
}

Expand All @@ -3350,9 +3350,10 @@ export default abstract class Server<
cacheControl = {
revalidate:
typeof notFoundRevalidate === 'undefined' ? 0 : notFoundRevalidate,
expire: undefined,
}
} else if (is500Page) {
cacheControl = { revalidate: 0 }
cacheControl = { revalidate: 0, expire: undefined }
} else if (cacheEntry.cacheControl) {
// If the cache entry has a cache control with a revalidate value that's
// a number, use it.
Expand All @@ -3372,7 +3373,7 @@ export default abstract class Server<
// Otherwise if the revalidate value is false, then we should use the
// cache time of one year.
else {
cacheControl = { revalidate: CACHE_ONE_YEAR }
cacheControl = { revalidate: CACHE_ONE_YEAR, expire: undefined }
}
}
}
Expand Down Expand Up @@ -3563,7 +3564,7 @@ export default abstract class Server<
// postponed state.
// TODO: distinguish `force-static` from pages with no postponed state (static)
cacheControl: isDynamicRSCRequest
? { revalidate: 0 }
? { revalidate: 0, expire: undefined }
: cacheEntry.cacheControl,
}
}
Expand Down Expand Up @@ -3607,7 +3608,11 @@ export default abstract class Server<
})
)

return { type: 'html', body, cacheControl: { revalidate: 0 } }
return {
type: 'html',
body,
cacheControl: { revalidate: 0, expire: undefined },
}
}

// This request has postponed, so let's create a new transformer that the
Expand Down Expand Up @@ -3654,7 +3659,7 @@ export default abstract class Server<
// We don't want to cache the response if it has postponed data because
// the response being sent to the client it's dynamic parts are streamed
// to the client on the same request.
cacheControl: { revalidate: 0 },
cacheControl: { revalidate: 0, expire: undefined },
}
} else if (isNextDataRequest) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export class ImageOptimizerCache {
revalidateAfter:
Math.max(maxAge, this.nextConfig.images.minimumCacheTTL) * 1000 +
Date.now(),
cacheControl: { revalidate: maxAge },
cacheControl: { revalidate: maxAge, expire: undefined },
isStale: now > expireAt,
isFallback: false,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/cache-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Revalidate = number | false

export interface CacheControl {
revalidate: Revalidate
expire?: number
expire: number | undefined
}

export function getCacheControlHeader({
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ export default class NextNodeServer extends BaseServer<
upstreamEtag,
},
isFallback: false,
cacheControl: { revalidate: maxAge },
cacheControl: { revalidate: maxAge, expire: undefined },
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,8 @@ export async function renderToHTMLImpl(
'props' in data ? data.props : undefined
)

// pass up revalidate and props for export
metadata.cacheControl = { revalidate }
// pass up cache control and props for export
metadata.cacheControl = { revalidate, expire: undefined }
metadata.pageData = props

// this must come after revalidate is added to renderResultMeta
Expand Down Expand Up @@ -1123,7 +1123,7 @@ export async function renderToHTMLImpl(
})
)
canAccessRes = false
metadata.cacheControl = { revalidate: 0 }
metadata.cacheControl = { revalidate: 0, expire: undefined }
} catch (serverSidePropsError: any) {
// remove not found error code to prevent triggering legacy
// 404 rendering
Expand Down

0 comments on commit 152a496

Please sign in to comment.