-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathutils.ts
294 lines (258 loc) · 9.69 KB
/
utils.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
import type { Context } from 'https://edge.netlify.com'
import { ElementHandlers, HTMLRewriter } from 'https://deno.land/x/[email protected]/index.ts'
export interface FetchEventResult {
response: Response
waitUntil: Promise<any>
}
type NextDataTransform = <T>(data: T) => T
function normalizeDataUrl(redirect: string) {
// If the redirect is a data URL, we need to normalize it.
// next.js code reference: https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/router/utils/get-next-pathname-info.ts#L46
if (redirect.startsWith('/_next/data/') && redirect.includes('.json')) {
const paths = redirect
.replace(/^\/_next\/data\//, '')
.replace(/\.json/, '')
.split('/')
const buildId = paths[0]
redirect = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
}
return redirect
}
/**
* This is how Next handles rewritten URLs.
*/
export function relativizeURL(url: string | string, base: string | URL) {
const baseURL = typeof base === 'string' ? new URL(base) : base
const relative = new URL(url, base)
const origin = `${baseURL.protocol}//${baseURL.host}`
return `${relative.protocol}//${relative.host}` === origin
? relative.toString().replace(origin, '')
: relative.toString()
}
export const addMiddlewareHeaders = async (
originResponse: Promise<Response> | Response,
middlewareResponse: Response,
) => {
// If there are extra headers, we need to add them to the response.
if ([...middlewareResponse.headers.keys()].length === 0) {
return originResponse
}
// We need to await the response to get the origin headers, then we can add the ones from middleware.
const res = await originResponse
const response = new Response(res.body, res)
middlewareResponse.headers.forEach((value, key) => {
if (key === 'set-cookie') {
response.headers.append(key, value)
} else {
response.headers.set(key, value)
}
})
return response
}
interface MiddlewareResponse extends Response {
originResponse: Response
dataTransforms: NextDataTransform[]
elementHandlers: Array<[selector: string, handlers: ElementHandlers]>
}
interface MiddlewareRequest {
request: Request
context: Context
originalRequest: Request
next(): Promise<MiddlewareResponse>
rewrite(destination: string | URL, init?: ResponseInit): Response
}
export interface I18NConfig {
defaultLocale: string
localeDetection?: false
locales: string[]
}
export interface RequestData {
geo?: {
city?: string
country?: string
region?: string
latitude?: string
longitude?: string
timezone?: string
}
headers: Record<string, string>
ip?: string
method: string
nextConfig?: {
basePath?: string
i18n?: I18NConfig | null
trailingSlash?: boolean
}
page?: {
name?: string
params?: { [key: string]: string }
}
url: string
body?: ReadableStream<Uint8Array>
}
function isMiddlewareRequest(response: Response | MiddlewareRequest): response is MiddlewareRequest {
return 'originalRequest' in response
}
function isMiddlewareResponse(response: Response | MiddlewareResponse): response is MiddlewareResponse {
return 'dataTransforms' in response
}
// Next 13 supports request header mutations and has the side effect of prepending header values with 'x-middleware-request'
// as part of invoking NextResponse.next() in the middleware. We need to remove that before sending the response the user
// as the code that removes it in Next isn't run based on how we handle the middleware
//
// Related Next.js code:
// * https://github.com/vercel/next.js/blob/68d06fe015b28d8f81da52ca107a5f4bd72ab37c/packages/next/server/next-server.ts#L1918-L1928
// * https://github.com/vercel/next.js/blob/43c9d8940dc42337dd2f7d66aa90e6abf952278e/packages/next/server/web/spec-extension/response.ts#L10-L27
export function updateModifiedHeaders(requestHeaders: Headers, responseHeaders: Headers) {
const overriddenHeaders = responseHeaders.get('x-middleware-override-headers')
if (!overriddenHeaders) {
return
}
const headersToUpdate = overriddenHeaders.split(',').map((header) => header.trim())
for (const header of headersToUpdate) {
const oldHeaderKey = 'x-middleware-request-' + header
const headerValue = responseHeaders.get(oldHeaderKey) || ''
requestHeaders.set(header, headerValue)
responseHeaders.delete(oldHeaderKey)
}
responseHeaders.delete('x-middleware-override-headers')
}
export const buildNextRequest = (
request: Request,
context: Context,
nextConfig?: RequestData['nextConfig'],
): RequestData => {
const { url, method, body, headers } = request
const { country, subdivision, city, latitude, longitude, timezone } = context.geo
const geo: RequestData['geo'] = {
country: country?.code,
region: subdivision?.code,
city,
latitude: latitude?.toString(),
longitude: longitude?.toString(),
timezone,
}
return {
headers: Object.fromEntries(headers.entries()),
geo,
url,
method,
ip: context.ip,
body: body ?? undefined,
nextConfig,
}
}
export const buildResponse = async ({
result,
request,
context,
}: {
result: FetchEventResult
request: Request
context: Context
}) => {
updateModifiedHeaders(request.headers, result.response.headers)
// They've returned the MiddlewareRequest directly, so we'll call `next()` for them.
if (isMiddlewareRequest(result.response)) {
result.response = await result.response.next()
}
if (isMiddlewareResponse(result.response)) {
const { response } = result
if (request.method === 'HEAD' || request.method === 'OPTIONS') {
return response.originResponse
}
// If it's JSON we don't need to use the rewriter, we can just parse it
if (response.originResponse.headers.get('content-type')?.includes('application/json')) {
const props = await response.originResponse.json()
const transformed = response.dataTransforms.reduce((prev, transform) => {
return transform(prev)
}, props)
const body = JSON.stringify(transformed)
const headers = new Headers(response.headers)
headers.set('content-length', String(body.length))
return new Response(body, { ...response, headers })
}
// This var will hold the contents of the script tag
let buffer = ''
// Create an HTMLRewriter that matches the Next data script tag
const rewriter = new HTMLRewriter()
if (response.dataTransforms.length > 0) {
rewriter.on('script[id="__NEXT_DATA__"]', {
text(textChunk) {
// Grab all the chunks in the Next data script tag
buffer += textChunk.text
if (textChunk.lastInTextNode) {
try {
// When we have all the data, try to parse it as JSON
const data = JSON.parse(buffer.trim())
// Apply all of the transforms to the props
const props = response.dataTransforms.reduce((prev, transform) => transform(prev), data.props)
// Replace the data with the transformed props
textChunk.replace(JSON.stringify({ ...data, props }))
} catch (err) {
console.log('Could not parse', err)
}
} else {
// Remove the chunk after we've appended it to the buffer
textChunk.remove()
}
},
})
}
if (response.elementHandlers.length > 0) {
response.elementHandlers.forEach(([selector, handlers]) => rewriter.on(selector, handlers))
}
return rewriter.transform(response.originResponse)
}
const res = new Response(result.response.body, result.response)
request.headers.set('x-nf-next-middleware', 'skip')
const rewrite = res.headers.get('x-middleware-rewrite')
// Data requests (i.e. requests for /_next/data ) need special handling
const isDataReq = request.headers.get('x-nextjs-data')
if (rewrite) {
const rewriteUrl = new URL(rewrite, request.url)
const baseUrl = new URL(request.url)
const relativeUrl = relativizeURL(rewrite, request.url)
// Data requests might be rewritten to an external URL
// This header tells the client router the redirect target, and if it's external then it will do a full navigation
if (isDataReq) {
res.headers.set('x-nextjs-rewrite', relativeUrl)
}
if (rewriteUrl.hostname !== baseUrl.hostname) {
// Netlify Edge Functions don't support proxying to external domains, but Next middleware does
const proxied = fetch(new Request(rewriteUrl.toString(), request))
return addMiddlewareHeaders(proxied, res)
}
res.headers.set('x-middleware-rewrite', relativeUrl)
return addMiddlewareHeaders(context.rewrite(rewrite), res)
}
const redirect = res.headers.get('Location')
// Data requests shouldn;t automatically redirect in the browser (they might be HTML pages): they're handled by the router
if (redirect && isDataReq) {
res.headers.delete('location')
res.headers.set('x-nextjs-redirect', relativizeURL(redirect, request.url))
}
const nextRedirect = res.headers.get('x-nextjs-redirect')
if (nextRedirect && isDataReq) {
res.headers.set('x-nextjs-redirect', normalizeDataUrl(nextRedirect))
}
if (res.headers.get('x-middleware-next') === '1') {
return addMiddlewareHeaders(context.next(), res)
}
return res
}
export const redirectTrailingSlash = (url: URL, trailingSlash: boolean): Response | undefined => {
const { pathname } = url
if (pathname === '/') {
return
}
if (!trailingSlash && pathname.endsWith('/')) {
url.pathname = pathname.slice(0, -1)
return Response.redirect(url, 308)
}
// Add a slash, unless there's a file extension
if (trailingSlash && !pathname.endsWith('/') && !pathname.split('/').pop()?.includes('.')) {
url.pathname = `${pathname}/`
return Response.redirect(url, 308)
}
}