File tree 2 files changed +23
-2
lines changed
packages/runtime/src/templates/edge-shared
test/e2e/modified-tests/middleware-redirects/test
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,22 @@ export interface FetchEventResult {
8
8
9
9
type NextDataTransform = < T > ( data : T ) => T
10
10
11
+ function normalizeDataUrl ( redirect : string ) {
12
+ // If the redirect is a data URL, we need to normalize it.
13
+ // 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
14
+ if ( redirect . startsWith ( '/_next/data/' ) && redirect . includes ( '.json' ) ) {
15
+ const paths = redirect
16
+ . replace ( / ^ \/ _ n e x t \/ d a t a \/ / , '' )
17
+ . replace ( / \. j s o n / , '' )
18
+ . split ( '/' )
19
+
20
+ const buildId = paths [ 0 ]
21
+ redirect = paths [ 1 ] !== 'index' ? `/${ paths . slice ( 1 ) . join ( '/' ) } ` : '/'
22
+ }
23
+
24
+ return redirect
25
+ }
26
+
11
27
/**
12
28
* This is how Next handles rewritten URLs.
13
29
*/
@@ -249,6 +265,12 @@ export const buildResponse = async ({
249
265
res . headers . set ( 'x-nextjs-redirect' , relativizeURL ( redirect , request . url ) )
250
266
}
251
267
268
+ const nextRedirect = res . headers . get ( 'x-nextjs-redirect' )
269
+
270
+ if ( nextRedirect && isDataReq ) {
271
+ res . headers . set ( 'x-nextjs-redirect' , normalizeDataUrl ( nextRedirect ) )
272
+ }
273
+
252
274
if ( res . headers . get ( 'x-middleware-next' ) === '1' ) {
253
275
return addMiddlewareHeaders ( context . next ( ) , res )
254
276
}
Original file line number Diff line number Diff line change @@ -37,14 +37,13 @@ describe('Middleware Redirect', () => {
37
37
expect ( res . headers . get ( 'location' ) ?. endsWith ( '/default/about' ) ) . toEqual ( false )
38
38
} )
39
39
40
- usuallySkip ( `should redirect to data urls with data requests and internal redirects` , async ( ) => {
40
+ it ( `should redirect to data urls with data requests and internal redirects` , async ( ) => {
41
41
const res = await fetchViaHTTP (
42
42
next . url ,
43
43
`/_next/data/${ next . buildId } /es/old-home.json` ,
44
44
{ override : 'internal' } ,
45
45
{ redirect : 'manual' , headers : { 'x-nextjs-data' : '1' } } ,
46
46
)
47
-
48
47
expect ( res . headers . get ( 'x-nextjs-redirect' ) ?. endsWith ( `/es/new-home?override=internal` ) ) . toEqual ( true )
49
48
expect ( res . headers . get ( 'location' ) ) . toEqual ( null )
50
49
} )
You can’t perform that action at this time.
0 commit comments