-
Notifications
You must be signed in to change notification settings - Fork 27.2k
/
patch-fetch.ts
535 lines (484 loc) · 18 KB
/
patch-fetch.ts
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import type { StaticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage'
import type * as ServerHooks from '../../client/components/hooks-server-context'
import { AppRenderSpan } from './trace/constants'
import { getTracer, SpanKind } from './trace/tracer'
import { CACHE_ONE_YEAR } from '../../lib/constants'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
export function addImplicitTags(
staticGenerationStore: ReturnType<StaticGenerationAsyncStorage['getStore']>
) {
const newTags: string[] = []
const pathname = staticGenerationStore?.originalPathname
if (!pathname) {
return newTags
}
if (!Array.isArray(staticGenerationStore.tags)) {
staticGenerationStore.tags = []
}
if (!staticGenerationStore.tags.includes(pathname)) {
staticGenerationStore.tags.push(pathname)
}
newTags.push(pathname)
return newTags
}
function trackFetchMetric(
staticGenerationStore: ReturnType<StaticGenerationAsyncStorage['getStore']>,
ctx: {
url: string
status: number
method: string
cacheReason: string
cacheStatus: 'hit' | 'miss'
start: number
}
) {
if (!staticGenerationStore) return
if (!staticGenerationStore.fetchMetrics) {
staticGenerationStore.fetchMetrics = []
}
const dedupeFields = ['url', 'status', 'method']
// don't add metric if one already exists for the fetch
if (
staticGenerationStore.fetchMetrics.some((metric) => {
return dedupeFields.every(
(field) => (metric as any)[field] === (ctx as any)[field]
)
})
) {
return
}
staticGenerationStore.fetchMetrics.push({
url: ctx.url,
cacheStatus: ctx.cacheStatus,
status: ctx.status,
method: ctx.method,
start: ctx.start,
end: Date.now(),
idx: staticGenerationStore.nextFetchId || 0,
})
}
// we patch fetch to collect cache information used for
// determining if a page is static or not
export function patchFetch({
serverHooks,
staticGenerationAsyncStorage,
}: {
serverHooks: typeof ServerHooks
staticGenerationAsyncStorage: StaticGenerationAsyncStorage
}) {
if (!(globalThis as any)._nextOriginalFetch) {
;(globalThis as any)._nextOriginalFetch = globalThis.fetch
}
if ((globalThis.fetch as any).__nextPatched) return
const { DynamicServerError } = serverHooks
const originFetch: typeof fetch = (globalThis as any)._nextOriginalFetch
globalThis.fetch = async (
input: RequestInfo | URL,
init: RequestInit | undefined
) => {
let url: URL | undefined
try {
url = new URL(input instanceof Request ? input.url : input)
url.username = ''
url.password = ''
} catch {
// Error caused by malformed URL should be handled by native fetch
url = undefined
}
const fetchUrl = url?.href ?? ''
const fetchStart = Date.now()
const method = init?.method?.toUpperCase() || 'GET'
return await getTracer().trace(
AppRenderSpan.fetch,
{
kind: SpanKind.CLIENT,
spanName: ['fetch', method, fetchUrl].filter(Boolean).join(' '),
attributes: {
'http.url': fetchUrl,
'http.method': method,
'net.peer.name': url?.hostname,
'net.peer.port': url?.port || undefined,
},
},
async () => {
const staticGenerationStore = staticGenerationAsyncStorage.getStore()
const isRequestInput =
input &&
typeof input === 'object' &&
typeof (input as Request).method === 'string'
const getRequestMeta = (field: string) => {
let value = isRequestInput ? (input as any)[field] : null
return value || (init as any)?.[field]
}
// If the staticGenerationStore is not available, we can't do any
// special treatment of fetch, therefore fallback to the original
// fetch implementation.
if (!staticGenerationStore || (init?.next as any)?.internal) {
return originFetch(input, init)
}
let revalidate: number | undefined | false = undefined
const getNextField = (field: 'revalidate' | 'tags') => {
return typeof init?.next?.[field] !== 'undefined'
? init?.next?.[field]
: isRequestInput
? (input as any).next?.[field]
: undefined
}
// RequestInit doesn't keep extra fields e.g. next so it's
// only available if init is used separate
let curRevalidate = getNextField('revalidate')
const tags: string[] = getNextField('tags') || []
if (Array.isArray(tags)) {
if (!staticGenerationStore.tags) {
staticGenerationStore.tags = []
}
for (const tag of tags) {
if (!staticGenerationStore.tags.includes(tag)) {
staticGenerationStore.tags.push(tag)
}
}
}
const implicitTags = addImplicitTags(staticGenerationStore)
for (const tag of implicitTags || []) {
if (!tags.includes(tag)) {
tags.push(tag)
}
}
const isOnlyCache = staticGenerationStore.fetchCache === 'only-cache'
const isForceCache = staticGenerationStore.fetchCache === 'force-cache'
const isDefaultCache =
staticGenerationStore.fetchCache === 'default-cache'
const isDefaultNoStore =
staticGenerationStore.fetchCache === 'default-no-store'
const isOnlyNoStore =
staticGenerationStore.fetchCache === 'only-no-store'
const isForceNoStore =
staticGenerationStore.fetchCache === 'force-no-store'
let _cache = getRequestMeta('cache')
if (
typeof _cache === 'string' &&
typeof curRevalidate !== 'undefined'
) {
console.warn(
`Warning: fetch for ${fetchUrl} on ${staticGenerationStore.pathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.`
)
_cache = undefined
}
if (_cache === 'force-cache') {
curRevalidate = false
}
if (['no-cache', 'no-store'].includes(_cache || '')) {
curRevalidate = 0
}
if (typeof curRevalidate === 'number' || curRevalidate === false) {
revalidate = curRevalidate
}
let cacheReason = ''
const _headers = getRequestMeta('headers')
const initHeaders: Headers =
typeof _headers?.get === 'function'
? _headers
: new Headers(_headers || {})
const hasUnCacheableHeader =
initHeaders.get('authorization') || initHeaders.get('cookie')
const isUnCacheableMethod = !['get', 'head'].includes(
getRequestMeta('method')?.toLowerCase() || 'get'
)
// if there are authorized headers or a POST method and
// dynamic data usage was present above the tree we bail
// e.g. if cookies() is used before an authed/POST fetch
const autoNoCache =
(hasUnCacheableHeader || isUnCacheableMethod) &&
staticGenerationStore.revalidate === 0
if (isForceNoStore) {
revalidate = 0
cacheReason = 'fetchCache = force-no-store'
}
if (isOnlyNoStore) {
if (_cache === 'force-cache' || revalidate === 0) {
throw new Error(
`cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'`
)
}
revalidate = 0
cacheReason = 'fetchCache = only-no-store'
}
if (isOnlyCache && _cache === 'no-store') {
throw new Error(
`cache: 'no-store' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-cache'`
)
}
if (
isForceCache &&
(typeof curRevalidate === 'undefined' || curRevalidate === 0)
) {
cacheReason = 'fetchCache = force-cache'
revalidate = false
}
if (typeof revalidate === 'undefined') {
if (isDefaultCache) {
revalidate = false
cacheReason = 'fetchCache = default-cache'
} else if (autoNoCache) {
revalidate = 0
cacheReason = 'auto no cache'
} else if (isDefaultNoStore) {
revalidate = 0
cacheReason = 'fetchCache = default-no-store'
} else {
cacheReason = 'auto cache'
revalidate =
typeof staticGenerationStore.revalidate === 'boolean' ||
typeof staticGenerationStore.revalidate === 'undefined'
? false
: staticGenerationStore.revalidate
}
} else if (!cacheReason) {
cacheReason = `revalidate: ${revalidate}`
}
if (
// we don't consider autoNoCache to switch to dynamic during
// revalidate although if it occurs during build we do
!autoNoCache &&
(typeof staticGenerationStore.revalidate === 'undefined' ||
(typeof revalidate === 'number' &&
(staticGenerationStore.revalidate === false ||
(typeof staticGenerationStore.revalidate === 'number' &&
revalidate < staticGenerationStore.revalidate))))
) {
staticGenerationStore.revalidate = revalidate
}
const isCacheableRevalidate =
(typeof revalidate === 'number' && revalidate > 0) ||
revalidate === false
let cacheKey: string | undefined
if (staticGenerationStore.incrementalCache && isCacheableRevalidate) {
try {
cacheKey =
await staticGenerationStore.incrementalCache.fetchCacheKey(
fetchUrl,
isRequestInput ? (input as RequestInit) : init
)
} catch (err) {
console.error(`Failed to generate cache key for`, input)
}
}
const requestInputFields = [
'cache',
'credentials',
'headers',
'integrity',
'keepalive',
'method',
'mode',
'redirect',
'referrer',
'referrerPolicy',
'signal',
'window',
'duplex',
]
if (isRequestInput) {
const reqInput: Request = input as any
const reqOptions: RequestInit = {
body: (reqInput as any)._ogBody || reqInput.body,
}
for (const field of requestInputFields) {
// @ts-expect-error custom fields
reqOptions[field] = reqInput[field]
}
input = new Request(reqInput.url, reqOptions)
} else if (init) {
const initialInit = init
init = {
body: (init as any)._ogBody || init.body,
}
for (const field of requestInputFields) {
// @ts-expect-error custom fields
init[field] = initialInit[field]
}
}
const fetchIdx = staticGenerationStore.nextFetchId ?? 1
staticGenerationStore.nextFetchId = fetchIdx + 1
const normalizedRevalidate =
typeof revalidate !== 'number' ? CACHE_ONE_YEAR : revalidate
const doOriginalFetch = async (isStale?: boolean) => {
// add metadata to init without editing the original
const clonedInit = {
...init,
next: { ...init?.next, fetchType: 'origin', fetchIdx },
}
return originFetch(input, clonedInit).then(async (res) => {
if (!isStale) {
trackFetchMetric(staticGenerationStore, {
start: fetchStart,
url: fetchUrl,
cacheReason,
cacheStatus: 'miss',
status: res.status,
method: clonedInit.method || 'GET',
})
}
if (
res.status === 200 &&
staticGenerationStore.incrementalCache &&
cacheKey &&
isCacheableRevalidate
) {
const bodyBuffer = Buffer.from(await res.arrayBuffer())
try {
await staticGenerationStore.incrementalCache.set(
cacheKey,
{
kind: 'FETCH',
data: {
headers: Object.fromEntries(res.headers.entries()),
body: bodyBuffer.toString('base64'),
status: res.status,
tags,
},
revalidate: normalizedRevalidate,
},
revalidate,
true,
fetchUrl,
fetchIdx
)
} catch (err) {
console.warn(`Failed to set fetch cache`, input, err)
}
return new Response(bodyBuffer, {
headers: new Headers(res.headers),
status: res.status,
})
}
return res
})
}
if (cacheKey && staticGenerationStore?.incrementalCache) {
const entry = staticGenerationStore.isOnDemandRevalidate
? null
: await staticGenerationStore.incrementalCache.get(
cacheKey,
true,
revalidate,
fetchUrl,
fetchIdx
)
if (entry?.value && entry.value.kind === 'FETCH') {
const currentTags = entry.value.data.tags
// when stale and is revalidating we wait for fresh data
// so the revalidated entry has the updated data
if (!(staticGenerationStore.isRevalidate && entry.isStale)) {
if (entry.isStale) {
if (!staticGenerationStore.pendingRevalidates) {
staticGenerationStore.pendingRevalidates = []
}
staticGenerationStore.pendingRevalidates.push(
doOriginalFetch(true).catch(console.error)
)
} else if (
tags &&
!tags.every((tag) => currentTags?.includes(tag))
) {
// if new tags are being added we need to set even if
// the data isn't stale
if (!entry.value.data.tags) {
entry.value.data.tags = []
}
for (const tag of tags) {
if (!entry.value.data.tags.includes(tag)) {
entry.value.data.tags.push(tag)
}
}
staticGenerationStore.incrementalCache?.set(
cacheKey,
entry.value,
revalidate,
true,
fetchUrl,
fetchIdx
)
}
const resData = entry.value.data
let decodedBody: ArrayBuffer
if (process.env.NEXT_RUNTIME === 'edge') {
const { decode } =
require('../../shared/lib/base64-arraybuffer') as typeof import('../../shared/lib/base64-arraybuffer')
decodedBody = decode(resData.body)
} else {
decodedBody = Buffer.from(resData.body, 'base64').subarray()
}
trackFetchMetric(staticGenerationStore, {
start: fetchStart,
url: fetchUrl,
cacheReason,
cacheStatus: 'hit',
status: resData.status || 200,
method: init?.method || 'GET',
})
return new Response(decodedBody, {
headers: resData.headers,
status: resData.status,
})
}
}
}
if (staticGenerationStore.isStaticGeneration) {
if (init && typeof init === 'object') {
const cache = init.cache
// Delete `cache` property as Cloudflare Workers will throw an error
if (isEdgeRuntime) {
delete init.cache
}
if (cache === 'no-store') {
staticGenerationStore.revalidate = 0
const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.pathname
? ` ${staticGenerationStore.pathname}`
: ''
}`
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
}
const hasNextConfig = 'next' in init
const next = init.next || {}
if (
typeof next.revalidate === 'number' &&
(typeof staticGenerationStore.revalidate === 'undefined' ||
(typeof staticGenerationStore.revalidate === 'number' &&
next.revalidate < staticGenerationStore.revalidate))
) {
const forceDynamic = staticGenerationStore.forceDynamic
if (!forceDynamic || next.revalidate !== 0) {
staticGenerationStore.revalidate = next.revalidate
}
if (!forceDynamic && next.revalidate === 0) {
const dynamicUsageReason = `revalidate: ${
next.revalidate
} fetch ${input}${
staticGenerationStore.pathname
? ` ${staticGenerationStore.pathname}`
: ''
}`
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription =
dynamicUsageReason
}
}
if (hasNextConfig) delete init.next
}
}
return doOriginalFetch()
}
)
}
;(globalThis.fetch as any).__nextGetStaticStore = () => {
return staticGenerationAsyncStorage
}
;(globalThis.fetch as any).__nextPatched = true
}