-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcache.cts
382 lines (323 loc) · 12.6 KB
/
cache.cts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Netlify Cache Handler
// (CJS format because Next.js doesn't support ESM yet)
//
import { Buffer } from 'node:buffer'
import { join } from 'node:path'
import { join as posixJoin } from 'node:path/posix'
import { Store } from '@netlify/blobs'
import { purgeCache } from '@netlify/functions'
import { type Span } from '@opentelemetry/api'
import type { PrerenderManifest } from 'next/dist/build/index.js'
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
import { loadManifest } from 'next/dist/server/load-manifest.js'
import { normalizePagePath } from 'next/dist/shared/lib/page-path/normalize-page-path.js'
import type {
CacheHandler,
CacheHandlerContext,
IncrementalCache,
NetlifyCachedPageValue,
NetlifyCachedRouteValue,
NetlifyCacheHandlerValue,
NetlifyIncrementalCacheValue,
} from '../../shared/cache-types.cjs'
import { getRegionalBlobStore } from '../regional-blob-store.cjs'
import { getLogger, getRequestContext } from './request-context.cjs'
import { getTracer } from './tracer.cjs'
type TagManifest = { revalidatedAt: number }
type TagManifestBlobCache = Record<string, Promise<TagManifest>>
export class NetlifyCacheHandler implements CacheHandler {
options: CacheHandlerContext
revalidatedTags: string[]
blobStore: Store
tracer = getTracer()
tagManifestsFetchedFromBlobStoreInCurrentRequest: TagManifestBlobCache
constructor(options: CacheHandlerContext) {
this.options = options
this.revalidatedTags = options.revalidatedTags
this.blobStore = getRegionalBlobStore({ consistency: 'strong' })
this.tagManifestsFetchedFromBlobStoreInCurrentRequest = {}
}
private async encodeBlobKey(key: string) {
const { encodeBlobKey } = await import('../../shared/blobkey.js')
return await encodeBlobKey(key)
}
private captureResponseCacheLastModified(
cacheValue: NetlifyCacheHandlerValue,
key: string,
getCacheKeySpan: Span,
) {
if (cacheValue.value?.kind === 'FETCH') {
return
}
const requestContext = getRequestContext()
if (!requestContext) {
// we will not be able to use request context for date header calculation
// we will fallback to using blobs
getCacheKeySpan.recordException(
new Error('CacheHandler was called without a request context'),
)
getCacheKeySpan.setAttributes({
severity: 'alert',
warning: true,
})
return
}
if (requestContext.responseCacheKey && requestContext.responseCacheKey !== key) {
// if there are multiple response-cache keys, we don't know which one we should use
// so as a safety measure we will not use any of them and let blobs be used
// to calculate the date header
requestContext.responseCacheGetLastModified = undefined
getCacheKeySpan.recordException(
new Error(
`Multiple response cache keys used in single request: ["${requestContext.responseCacheKey}, "${key}"]`,
),
)
getCacheKeySpan.setAttributes({
severity: 'alert',
warning: true,
})
return
}
requestContext.responseCacheKey = key
if (cacheValue.lastModified) {
// we store it to use it later when calculating date header
requestContext.responseCacheGetLastModified = cacheValue.lastModified
}
}
private captureRouteRevalidateAndRemoveFromObject(
cacheValue: NetlifyCachedRouteValue,
): Omit<NetlifyCachedRouteValue, 'revalidate'> {
const { revalidate, ...restOfRouteValue } = cacheValue
const requestContext = getRequestContext()
if (requestContext) {
requestContext.routeHandlerRevalidate = revalidate
}
return restOfRouteValue
}
private injectEntryToPrerenderManifest(
key: string,
revalidate: NetlifyCachedPageValue['revalidate'],
) {
if (this.options.serverDistDir && (typeof revalidate === 'number' || revalidate === false)) {
const prerenderManifest = loadManifest(
join(this.options.serverDistDir, '..', 'prerender-manifest.json'),
) as PrerenderManifest
prerenderManifest.routes[key] = {
experimentalPPR: undefined,
dataRoute: posixJoin('/_next/data', `${normalizePagePath(key)}.json`),
srcRoute: null, // FIXME: provide actual source route, however, when dynamically appending it doesn't really matter
initialRevalidateSeconds: revalidate,
// Pages routes do not have a prefetch data route.
prefetchDataRoute: undefined,
}
}
}
async get(...args: Parameters<CacheHandler['get']>): ReturnType<CacheHandler['get']> {
return this.tracer.withActiveSpan('get cache key', async (span) => {
const [key, ctx = {}] = args
getLogger().debug(`[NetlifyCacheHandler.get]: ${key}`)
const blobKey = await this.encodeBlobKey(key)
span.setAttributes({ key, blobKey })
const blob = (await this.tracer.withActiveSpan('blobStore.get', async (blobGetSpan) => {
blobGetSpan.setAttributes({ key, blobKey })
return await this.blobStore.get(blobKey, {
type: 'json',
})
})) as NetlifyCacheHandlerValue | null
// if blob is null then we don't have a cache entry
if (!blob) {
span.addEvent('Cache miss', { key, blobKey })
return null
}
const staleByTags = await this.checkCacheEntryStaleByTags(blob, ctx.tags, ctx.softTags)
if (staleByTags) {
span.addEvent('Stale', { staleByTags })
return null
}
this.captureResponseCacheLastModified(blob, key, span)
switch (blob.value?.kind) {
case 'FETCH':
span.addEvent('FETCH', { lastModified: blob.lastModified, revalidate: ctx.revalidate })
return {
lastModified: blob.lastModified,
value: blob.value,
}
case 'ROUTE': {
span.addEvent('ROUTE', { lastModified: blob.lastModified, status: blob.value.status })
const valueWithoutRevalidate = this.captureRouteRevalidateAndRemoveFromObject(blob.value)
return {
lastModified: blob.lastModified,
value: {
...valueWithoutRevalidate,
body: Buffer.from(valueWithoutRevalidate.body as unknown as string, 'base64'),
},
}
}
case 'PAGE': {
span.addEvent('PAGE', { lastModified: blob.lastModified })
const { revalidate, ...restOfPageValue } = blob.value
this.injectEntryToPrerenderManifest(key, revalidate)
return {
lastModified: blob.lastModified,
value: restOfPageValue,
}
}
default:
span.recordException(new Error(`Unknown cache entry kind: ${blob.value?.kind}`))
}
return null
})
}
private transformToStorableObject(
data: Parameters<IncrementalCache['set']>[1],
context: Parameters<IncrementalCache['set']>[2],
): NetlifyIncrementalCacheValue | null {
if (data?.kind === 'ROUTE') {
return {
...data,
revalidate: context.revalidate,
body: data.body.toString('base64'),
}
}
if (data?.kind === 'PAGE') {
return {
...data,
revalidate: context.revalidate,
}
}
return data
}
async set(...args: Parameters<IncrementalCache['set']>) {
return this.tracer.withActiveSpan('set cache key', async (span) => {
const [key, data, context] = args
const blobKey = await this.encodeBlobKey(key)
const lastModified = Date.now()
span.setAttributes({ key, lastModified, blobKey })
getLogger().debug(`[NetlifyCacheHandler.set]: ${key}`)
const value = this.transformToStorableObject(data, context)
await this.blobStore.setJSON(blobKey, {
lastModified,
value,
})
if (data?.kind === 'PAGE') {
const requestContext = getRequestContext()
if (requestContext?.didPagesRouterOnDemandRevalidate) {
const tag = `_N_T_${key === '/index' ? '/' : key}`
getLogger().debug(`Purging CDN cache for: [${tag}]`)
requestContext.trackBackgroundWork(
purgeCache({ tags: [tag] }).catch((error) => {
// TODO: add reporting here
getLogger()
.withError(error)
.error(`[NetlifyCacheHandler]: Purging the cache for tag ${tag} failed`)
}),
)
}
}
})
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async revalidateTag(tagOrTags: string | string[], ...args: any) {
const revalidateTagPromise = this.doRevalidateTag(tagOrTags, ...args)
const requestContext = getRequestContext()
if (requestContext) {
requestContext.trackBackgroundWork(revalidateTagPromise)
}
return revalidateTagPromise
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async doRevalidateTag(tagOrTags: string | string[], ...args: any) {
getLogger().withFields({ tagOrTags, args }).debug('NetlifyCacheHandler.revalidateTag')
const tags = Array.isArray(tagOrTags) ? tagOrTags : [tagOrTags]
const data: TagManifest = {
revalidatedAt: Date.now(),
}
await Promise.all(
tags.map(async (tag) => {
try {
await this.blobStore.setJSON(await this.encodeBlobKey(tag), data)
} catch (error) {
getLogger().withError(error).log(`Failed to update tag manifest for ${tag}`)
}
}),
)
await purgeCache({ tags }).catch((error) => {
// TODO: add reporting here
getLogger()
.withError(error)
.error(`[NetlifyCacheHandler]: Purging the cache for tags ${tags.join(', ')} failed`)
})
}
resetRequestCache() {
this.tagManifestsFetchedFromBlobStoreInCurrentRequest = {}
}
/**
* Checks if a cache entry is stale through on demand revalidated tags
*/
private async checkCacheEntryStaleByTags(
cacheEntry: NetlifyCacheHandlerValue,
tags: string[] = [],
softTags: string[] = [],
) {
let cacheTags: string[] = []
if (cacheEntry.value?.kind === 'FETCH') {
cacheTags = [...tags, ...softTags]
} else if (cacheEntry.value?.kind === 'PAGE' || cacheEntry.value?.kind === 'ROUTE') {
cacheTags = (cacheEntry.value.headers?.[NEXT_CACHE_TAGS_HEADER] as string)?.split(',') || []
} else {
return false
}
// 1. Check if revalidateTags array passed from Next.js contains any of cacheEntry tags
if (this.revalidatedTags && this.revalidatedTags.length !== 0) {
// TODO: test for this case
for (const tag of this.revalidatedTags) {
if (cacheTags.includes(tag)) {
return true
}
}
}
// 2. If any in-memory tags don't indicate that any of tags was invalidated
// we will check blob store, but memoize results for duration of current request
// so that we only check blob store once per tag within a single request
// full-route cache and fetch caches share a lot of tags so this might save
// some roundtrips to the blob store.
// Additionally, we will resolve the promise as soon as we find first
// stale tag, so that we don't wait for all of them to resolve (but keep all
// running in case future `CacheHandler.get` calls would be able to use results).
// "Worst case" scenario is none of tag was invalidated in which case we need to wait
// for all blob store checks to finish before we can be certain that no tag is stale.
return new Promise<boolean>((resolve, reject) => {
const tagManifestPromises: Promise<boolean>[] = []
for (const tag of cacheTags) {
let tagManifestPromise: Promise<TagManifest> =
this.tagManifestsFetchedFromBlobStoreInCurrentRequest[tag]
if (!tagManifestPromise) {
tagManifestPromise = this.encodeBlobKey(tag).then((blobKey) => {
return this.tracer.withActiveSpan(`get tag manifest`, async (span) => {
span.setAttributes({ tag, blobKey })
return this.blobStore.get(blobKey, { type: 'json' })
})
})
this.tagManifestsFetchedFromBlobStoreInCurrentRequest[tag] = tagManifestPromise
}
tagManifestPromises.push(
tagManifestPromise.then((tagManifest) => {
const isStale = tagManifest?.revalidatedAt >= (cacheEntry.lastModified || Date.now())
if (isStale) {
resolve(true)
return true
}
return false
}),
)
}
// make sure we resolve promise after all blobs are checked (if we didn't resolve as stale yet)
Promise.all(tagManifestPromises)
.then((tagManifestAreStale) => {
resolve(tagManifestAreStale.some((tagIsStale) => tagIsStale))
})
.catch(reject)
})
}
}
export default NetlifyCacheHandler