@@ -6,7 +6,24 @@ export interface FetchEventResult {
6
6
waitUntil : Promise < any >
7
7
}
8
8
9
- type NextDataTransform = < T > ( data : T ) => T
9
+ function normalizeDataUrl ( redirect , url ) {
10
+ const normalizedUrl = new URL ( redirect , url )
11
+
12
+ if (
13
+ normalizedUrl . pathname . startsWith ( '/_next/data/' ) &&
14
+ normalizedUrl . pathname . endsWith ( '.json' )
15
+ ) {
16
+ const paths = normalizedUrl . pathname
17
+ . replace ( / ^ \/ _ n e x t \/ d a t a \/ / , '' )
18
+ . replace ( / \. j s o n $ / , '' )
19
+ . split ( '/' )
20
+
21
+ const buildId = paths [ 0 ]
22
+ normalizedUrl . pathname = paths [ 1 ] !== 'index' ? `/${ paths . slice ( 1 ) . join ( '/' ) } ` : '/'
23
+ }
24
+
25
+ return normalizedUrl . pathname + normalizedUrl . search ;
26
+ }
10
27
11
28
/**
12
29
* This is how Next handles rewritten URLs.
@@ -247,7 +264,7 @@ export const buildResponse = async ({
247
264
// Data requests shouldn;t automatically redirect in the browser (they might be HTML pages): they're handled by the router
248
265
if ( redirect && isDataReq ) {
249
266
res . headers . delete ( 'location' )
250
- res . headers . set ( 'x-nextjs-redirect' , relativizeURL ( redirect , request . url ) )
267
+ res . headers . set ( 'x-nextjs-redirect' , normalizeDataUrl ( redirect , request . url ) )
251
268
}
252
269
253
270
if ( res . headers . get ( 'x-middleware-next' ) === '1' ) {
0 commit comments