Skip to content

Commit 69114ad

Browse files
committed
fix: updated redirect data urls
1 parent 244b3be commit 69114ad

File tree

2 files changed

+20
-3
lines changed
  • packages/runtime/src/templates/edge-shared
  • test/e2e/modified-tests/middleware-redirects/test

2 files changed

+20
-3
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@ export interface FetchEventResult {
66
waitUntil: Promise<any>
77
}
88

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(/^\/_next\/data\//, '')
18+
.replace(/\.json$/, '')
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+
}
1027

1128
/**
1229
* This is how Next handles rewritten URLs.
@@ -247,7 +264,7 @@ export const buildResponse = async ({
247264
// Data requests shouldn;t automatically redirect in the browser (they might be HTML pages): they're handled by the router
248265
if (redirect && isDataReq) {
249266
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))
251268
}
252269

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

test/e2e/modified-tests/middleware-redirects/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('Middleware Redirect', () => {
3737
expect(res.headers.get('location')?.endsWith('/default/about')).toEqual(false)
3838
})
3939

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 () => {
4141
const res = await fetchViaHTTP(
4242
next.url,
4343
`/_next/data/${next.buildId}/es/old-home.json`,

0 commit comments

Comments
 (0)