-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
Copy pathpatch-fetch.ts
314 lines (279 loc) · 10.4 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
import type { StaticGenerationAsyncStorage } from '../../client/components/static-generation-async-storage'
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'
// we patch fetch to collect cache information used for
// determining if a page is static or not
export function patchFetch({
serverHooks,
staticGenerationAsyncStorage,
}: {
serverHooks: typeof import('../../client/components/hooks-server-context')
staticGenerationAsyncStorage: StaticGenerationAsyncStorage
}) {
if ((fetch as any).__nextPatched) return
const { DynamicServerError } = serverHooks
const originFetch = fetch
// @ts-expect-error - we're patching fetch
// eslint-disable-next-line no-native-reassign
fetch = getTracer().wrap(
AppRenderSpan.fetch,
{
kind: SpanKind.CLIENT,
},
async (input: RequestInfo | URL, init: RequestInit | undefined) => {
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
// RequestInit doesn't keep extra fields e.g. next so it's
// only available if init is used separate
let curRevalidate =
typeof init?.next?.revalidate !== 'undefined'
? init?.next?.revalidate
: isRequestInput
? (input as any).next?.revalidate
: undefined
const _cache = getRequestMeta('cache')
if (_cache === 'force-cache') {
curRevalidate = false
}
if (['no-cache', 'no-store'].includes(_cache || '')) {
curRevalidate = 0
}
if (typeof curRevalidate === 'number') {
revalidate = curRevalidate
}
if (curRevalidate === false) {
revalidate = CACHE_ONE_YEAR
}
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 (typeof revalidate === 'undefined') {
if (autoNoCache) {
revalidate = 0
} else {
revalidate =
typeof staticGenerationStore.revalidate === 'boolean' ||
typeof staticGenerationStore.revalidate === 'undefined'
? CACHE_ONE_YEAR
: staticGenerationStore.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' &&
revalidate < staticGenerationStore.revalidate))
) {
staticGenerationStore.revalidate = revalidate
}
let cacheKey: string | undefined
if (
staticGenerationStore.incrementalCache &&
typeof revalidate === 'number' &&
revalidate > 0
) {
try {
cacheKey = await staticGenerationStore.incrementalCache.fetchCacheKey(
isRequestInput ? (input as Request).url : input.toString(),
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 doOriginalFetch = async () => {
return originFetch(input, init).then(async (res) => {
if (
staticGenerationStore.incrementalCache &&
cacheKey &&
typeof revalidate === 'number' &&
revalidate > 0
) {
let base64Body = ''
const resBlob = await res.blob()
const arrayBuffer = await resBlob.arrayBuffer()
if (process.env.NEXT_RUNTIME === 'edge') {
const { encode } =
require('../../shared/lib/bloom-filter/base64-arraybuffer') as typeof import('../../shared/lib/bloom-filter/base64-arraybuffer')
base64Body = encode(arrayBuffer)
} else {
base64Body = Buffer.from(arrayBuffer).toString('base64')
}
try {
await staticGenerationStore.incrementalCache.set(
cacheKey,
{
kind: 'FETCH',
data: {
headers: Object.fromEntries(res.headers.entries()),
body: base64Body,
},
revalidate,
},
revalidate,
true
)
} catch (err) {
console.warn(`Failed to set fetch cache`, input, err)
}
return new Response(resBlob, {
headers: res.headers,
status: res.status,
})
}
return res
})
}
if (cacheKey && staticGenerationStore?.incrementalCache) {
const entry = await staticGenerationStore.incrementalCache.get(
cacheKey,
true
)
if (entry?.value && entry.value.kind === 'FETCH') {
// 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().catch(console.error)
)
}
const resData = entry.value.data
let decodedBody: ArrayBuffer
if (process.env.NEXT_RUNTIME === 'edge') {
const { decode } =
require('../../shared/lib/bloom-filter/base64-arraybuffer') as typeof import('../../shared/lib/bloom-filter/base64-arraybuffer')
decodedBody = decode(resData.body)
} else {
decodedBody = Buffer.from(resData.body, 'base64').subarray()
}
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
// TODO: ensure this error isn't logged to the user
// seems it's slipping through currently
const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.pathname
? ` ${staticGenerationStore.pathname}`
: ''
}`
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}
const hasNextConfig = 'next' in init
const next = init.next || {}
if (
typeof next.revalidate === 'number' &&
(typeof staticGenerationStore.revalidate === 'undefined' ||
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.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}
}
if (hasNextConfig) delete init.next
}
}
return doOriginalFetch()
}
)
;(fetch as any).__nextPatched = true
}