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