Skip to content

Commit

Permalink
Plumb expire time through
Browse files Browse the repository at this point in the history
Test with use cache.
  • Loading branch information
sebmarkbage committed Oct 14, 2024
1 parent 1794f59 commit 379bc29
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 18 deletions.
7 changes: 5 additions & 2 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
RSC_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX,
RSC_SEGMENT_SUFFIX,
INFINITE_CACHE,
} from '../../lib/constants'
import { hasNextSupport } from '../../server/ci-info'
import { lazyRenderAppPage } from '../../server/route-modules/app-page/module.render'
Expand Down Expand Up @@ -91,6 +92,7 @@ export async function exportAppPage(
const {
flightData,
revalidate = false,
expire = INFINITE_CACHE,
postponed,
fetchTags,
fetchMetrics,
Expand Down Expand Up @@ -118,7 +120,7 @@ export async function exportAppPage(
})
}

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

// If page data isn't available, it means that the page couldn't be rendered
Expand Down Expand Up @@ -239,6 +241,7 @@ export async function exportAppPage(
hasEmptyPrelude: Boolean(postponed) && html === '',
hasPostponed: Boolean(postponed),
revalidate,
expire,
fetchMetrics,
}
} catch (err) {
Expand Down Expand Up @@ -266,7 +269,7 @@ export async function exportAppPage(
})
}

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

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

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

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

const blob = await response.blob()
Expand All @@ -131,6 +131,12 @@ export async function exportAppRoute(
? false
: (context.renderOpts as any).collectedRevalidate

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

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

Expand Down Expand Up @@ -161,13 +167,14 @@ export async function exportAppRoute(

return {
revalidate: revalidate,
expire: expire,
metadata: meta,
}
} catch (err) {
if (!isDynamicUsageError(err)) {
throw err
}

return { revalidate: 0 }
return { revalidate: 0, expire: 0 }
}
}
2 changes: 2 additions & 0 deletions packages/next/src/export/routes/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
} from '../../server/lib/mock-request'
import { isInAmpMode } from '../../shared/lib/amp-mode'
import {
INFINITE_CACHE,
NEXT_DATA_SUFFIX,
SERVER_PROPS_EXPORT_ERROR,
} from '../../lib/constants'
Expand Down Expand Up @@ -217,6 +218,7 @@ export async function exportPagesPage(
return {
ampValidations,
revalidate: metadata.revalidate ?? false,
expire: INFINITE_CACHE, // Pages/ doesn't have an expire config.
ssgNotFound,
}
}
3 changes: 2 additions & 1 deletion packages/next/src/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { LoadComponentsReturnType } from '../server/load-components'
import type { OutgoingHttpHeaders } from 'http'
import type AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import type { Revalidate } from '../server/lib/revalidate'
import type { ExpireTime, Revalidate } from '../server/lib/revalidate'
import type { NextEnabledDirectories } from '../server/base-server'
import type {
SerializableTurborepoAccessTraceResult,
Expand Down Expand Up @@ -84,6 +84,7 @@ export type ExportRouteResult =
| {
ampValidations?: AmpValidation[]
revalidate: Revalidate
expire: ExpireTime
metadata?: {
status?: number
headers?: OutgoingHttpHeaders
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ async function exportPage(
files,
ampValidations: result.ampValidations,
revalidate: result.revalidate,
expire: result.expire,
metadata: result.metadata,
ssgNotFound: result.ssgNotFound,
hasEmptyPrelude: result.hasEmptyPrelude,
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1125,12 +1125,17 @@ async function renderToHTMLOrFlightImpl(
// the page.
if (workStore.forceStatic === false || response.collectedRevalidate === 0) {
metadata.revalidate = 0
metadata.expire = 0
} else {
// Copy the revalidation value onto the render result metadata.
metadata.revalidate =
response.collectedRevalidate >= INFINITE_CACHE
? false
: response.collectedRevalidate
metadata.expire =
response.collectedExpire >= INFINITE_CACHE
? undefined
: response.collectedExpire
}

// provide bailout info for debugging
Expand Down
46 changes: 40 additions & 6 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ export class WrappedBuildError extends Error {
type ResponsePayload = {
type: 'html' | 'json' | 'rsc'
body: RenderResult
revalidate?: Revalidate
revalidate: undefined | Revalidate
expire: undefined | ExpireTime
}

export type NextEnabledDirectories = {
Expand Down Expand Up @@ -1718,7 +1719,7 @@ export default abstract class Server<
const { req, res } = ctx
const originalStatus = res.statusCode
const { body, type } = payload
let { revalidate } = payload
let { revalidate, expire } = payload
if (!res.sent) {
const { generateEtags, poweredByHeader, dev } = this.renderOpts

Expand All @@ -1734,7 +1735,7 @@ export default abstract class Server<
generateEtags,
poweredByHeader,
revalidate,
expireTime: this.nextConfig.expireTime,
expireTime: expire ?? this.nextConfig.expireTime,
})
res.statusCode = originalStatus
}
Expand Down Expand Up @@ -2186,6 +2187,8 @@ export default abstract class Server<
type: 'html',
// TODO: Static pages should be serialized as RenderResult
body: RenderResult.fromStatic(components.Component),
revalidate: undefined,
expire: undefined,
}
}

Expand Down Expand Up @@ -2563,6 +2566,13 @@ export default abstract class Server<
? false
: (context.renderOpts as any).collectedRevalidate

const expire =
typeof (context.renderOpts as any).collectedExpire ===
'undefined' ||
(context.renderOpts as any).collectedExpire >= INFINITE_CACHE
? undefined
: (context.renderOpts as any).collectedExpire

// Create the cache entry for the response.
const cacheEntry: ResponseCacheEntry = {
value: {
Expand All @@ -2572,6 +2582,7 @@ export default abstract class Server<
headers,
},
revalidate,
expire,
isFallback: false,
}

Expand Down Expand Up @@ -2733,6 +2744,7 @@ export default abstract class Server<
return {
value: null,
revalidate: metadata.revalidate,
expire: metadata.expire,
isFallback: false,
} satisfies ResponseCacheEntry
}
Expand All @@ -2745,6 +2757,7 @@ export default abstract class Server<
props: metadata.pageData ?? metadata.flightData,
} satisfies CachedRedirectValue,
revalidate: metadata.revalidate,
expire: metadata.expire,
isFallback: false,
} satisfies ResponseCacheEntry
}
Expand All @@ -2767,6 +2780,7 @@ export default abstract class Server<
segmentData: undefined,
} satisfies CachedAppPageValue,
revalidate: metadata.revalidate,
expire: metadata.expire,
isFallback: !!fallbackRouteParams,
} satisfies ResponseCacheEntry
}
Expand All @@ -2780,6 +2794,7 @@ export default abstract class Server<
status: isAppPath ? res.statusCode : undefined,
} satisfies CachedPageValue,
revalidate: metadata.revalidate,
expire: metadata.expire,
isFallback: query.__nextFallback === 'true',
}
}
Expand Down Expand Up @@ -3000,6 +3015,7 @@ export default abstract class Server<
) {
return {
revalidate: 1,
expire: INFINITE_CACHE,
isFallback: false,
value: {
kind: CachedRouteKind.PAGES,
Expand Down Expand Up @@ -3032,6 +3048,7 @@ export default abstract class Server<
return {
...result,
revalidate: result.revalidate,
expire: result.expire,
}
}

Expand Down Expand Up @@ -3122,6 +3139,7 @@ export default abstract class Server<
// TODO: Eventually this should use revalidate time of the
// individual segment, not the whole page.
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
}
}
Expand All @@ -3133,6 +3151,7 @@ export default abstract class Server<
type: 'rsc',
body: RenderResult.fromStatic(''),
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
} else {
// Segment prefetches should never reach the application layer. If
Expand All @@ -3141,6 +3160,8 @@ export default abstract class Server<
return {
type: 'rsc',
body: RenderResult.fromStatic(''),
revalidate: undefined,
expire: undefined,
}
}
}
Expand Down Expand Up @@ -3343,7 +3364,7 @@ export default abstract class Server<
'Cache-Control',
formatRevalidate({
revalidate: cacheEntry.revalidate,
expireTime: this.nextConfig.expireTime,
expireTime: cacheEntry.expire ?? this.nextConfig.expireTime,
})
)
}
Expand All @@ -3366,7 +3387,7 @@ export default abstract class Server<
'Cache-Control',
formatRevalidate({
revalidate: cacheEntry.revalidate,
expireTime: this.nextConfig.expireTime,
expireTime: cacheEntry.expire ?? this.nextConfig.expireTime,
})
)
}
Expand All @@ -3379,6 +3400,7 @@ export default abstract class Server<
JSON.stringify(cachedData.props)
),
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
} else {
await handleRedirect(cachedData.props)
Expand Down Expand Up @@ -3475,6 +3497,7 @@ export default abstract class Server<
// postponed state.
// TODO: distinguish `force-static` from pages with no postponed state (static)
revalidate: isDynamicRSCRequest ? 0 : cacheEntry.revalidate,
expire: isDynamicRSCRequest ? 0 : cacheEntry.expire,
}
}

Expand All @@ -3484,6 +3507,7 @@ export default abstract class Server<
type: 'rsc',
body: RenderResult.fromStatic(cachedData.rscData),
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
}

Expand All @@ -3498,6 +3522,7 @@ export default abstract class Server<
type: 'html',
body,
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
}

Expand All @@ -3517,7 +3542,7 @@ export default abstract class Server<
})
)

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

// This request has postponed, so let's create a new transformer that the
Expand Down Expand Up @@ -3564,18 +3589,21 @@ export default abstract class Server<
// the response being sent to the client it's dynamic parts are streamed
// to the client on the same request.
revalidate: 0,
expire: undefined,
}
} else if (isNextDataRequest) {
return {
type: 'json',
body: RenderResult.fromStatic(JSON.stringify(cachedData.pageData)),
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
} else {
return {
type: 'html',
body: cachedData.html,
revalidate: cacheEntry.revalidate,
expire: cacheEntry.expire,
}
}
}
Expand Down Expand Up @@ -3893,6 +3921,8 @@ export default abstract class Server<
return {
type: 'html',
body: RenderResult.fromStatic(''),
revalidate: undefined,
expire: undefined,
}
}
const { res, query } = ctx
Expand Down Expand Up @@ -3999,6 +4029,8 @@ export default abstract class Server<
check()
</script>`
),
revalidate: undefined,
expire: undefined,
}
}

Expand Down Expand Up @@ -4077,6 +4109,8 @@ export default abstract class Server<
return {
type: 'html',
body: RenderResult.fromStatic('Internal Server Error'),
revalidate: undefined,
expire: undefined,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function formatRevalidate({
expireTime,
}: {
revalidate: Revalidate
expireTime?: ExpireTime
expireTime: undefined | ExpireTime
}): string {
const swrHeader =
typeof revalidate === 'number' && expireTime !== undefined
Expand Down
Loading

0 comments on commit 379bc29

Please sign in to comment.