Skip to content

Commit 567ea0a

Browse files
committed
fix: mark prerendered content as less stale now that expire setting is enforced for marking for blocking re-render
1 parent 9d38cd4 commit 567ea0a

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

src/build/content/prerendered.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const routeToFilePath = (path: string) => {
5959
const buildPagesCacheValue = async (
6060
path: string,
6161
initialRevalidateSeconds: number | false | undefined,
62+
initialExpireSeconds: number | undefined,
6263
shouldUseEnumKind: boolean,
6364
shouldSkipJson = false,
6465
): Promise<NetlifyCachedPageValue> => ({
@@ -68,6 +69,12 @@ const buildPagesCacheValue = async (
6869
headers: undefined,
6970
status: undefined,
7071
revalidate: initialRevalidateSeconds,
72+
cacheControl: initialRevalidateSeconds
73+
? {
74+
revalidate: initialRevalidateSeconds,
75+
expire: initialExpireSeconds,
76+
}
77+
: undefined,
7178
})
7279

7380
const RSC_SEGMENTS_DIR_SUFFIX = '.segments'
@@ -157,10 +164,20 @@ const buildRouteCacheValue = async (
157164

158165
const buildFetchCacheValue = async (
159166
path: string,
160-
): Promise<CachedFetchValueForMultipleVersions> => ({
161-
kind: 'FETCH',
162-
...JSON.parse(await readFile(path, 'utf-8')),
163-
})
167+
): Promise<{ value: CachedFetchValueForMultipleVersions; lastModified: number }> => {
168+
const data = JSON.parse(await readFile(path, 'utf-8')) as Omit<
169+
CachedFetchValueForMultipleVersions,
170+
'kind'
171+
>
172+
173+
return {
174+
value: {
175+
kind: 'FETCH',
176+
...data,
177+
},
178+
lastModified: Date.now() - (data?.revalidate ?? 31536000000),
179+
}
180+
}
164181

165182
/**
166183
* Upload prerendered content to the blob store
@@ -201,7 +218,7 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
201218
([route, meta]): Promise<void> =>
202219
limitConcurrentPrerenderContentHandling(async () => {
203220
const lastModified = meta.initialRevalidateSeconds
204-
? Date.now() - 31536000000
221+
? Date.now() - meta.initialRevalidateSeconds * 1000
205222
: Date.now()
206223
const key = routeToFilePath(route)
207224
let value: NetlifyIncrementalCacheValue
@@ -218,6 +235,7 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
218235
value = await buildPagesCacheValue(
219236
join(ctx.publishDir, 'server/pages', key),
220237
meta.initialRevalidateSeconds,
238+
meta.initialExpireSeconds,
221239
shouldUseEnumKind,
222240
)
223241
break
@@ -256,6 +274,7 @@ export const copyPrerenderedContent = async (ctx: PluginContext): Promise<void>
256274
const value = await buildPagesCacheValue(
257275
join(ctx.publishDir, 'server/pages', key),
258276
undefined,
277+
undefined,
259278
shouldUseEnumKind,
260279
true, // there is no corresponding json file for fallback, so we are skipping it for this entry
261280
)
@@ -310,9 +329,8 @@ export const copyFetchContent = async (ctx: PluginContext): Promise<void> => {
310329

311330
await Promise.all(
312331
paths.map(async (key): Promise<void> => {
313-
const lastModified = Date.now() - 31536000000
314332
const path = join(ctx.publishDir, 'cache/fetch-cache', key)
315-
const value = await buildFetchCacheValue(path)
333+
const { value, lastModified } = await buildFetchCacheValue(path)
316334
await writeCacheEntry(key, value, lastModified, ctx)
317335
}),
318336
)

0 commit comments

Comments
 (0)