Skip to content

Commit a6d69b2

Browse files
committed
fix: getting the redirect header and normalizing url outisde of orig if statement
1 parent 83c1ad5 commit a6d69b2

File tree

1 file changed

+13
-9
lines changed
  • packages/runtime/src/templates/edge-shared

1 file changed

+13
-9
lines changed

packages/runtime/src/templates/edge-shared/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ export interface FetchEventResult {
88

99
type NextDataTransform = <T>(data: T) => T
1010

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
1614
.replace(/^\/_next\/data\//, '')
17-
.replace(/\.json$/, '')
15+
.replace(/\.json/, '')
1816
.split('/')
1917

2018
const buildId = paths[0]
21-
normalizedUrl.pathname = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
19+
redirect = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
2220
}
2321

24-
return normalizedUrl.pathname + normalizedUrl.search
22+
return redirect
2523
}
2624

2725
/**
@@ -263,7 +261,13 @@ export const buildResponse = async ({
263261
// Data requests shouldn;t automatically redirect in the browser (they might be HTML pages): they're handled by the router
264262
if (redirect && isDataReq) {
265263
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))
267271
}
268272

269273
if (res.headers.get('x-middleware-next') === '1') {

0 commit comments

Comments
 (0)