Skip to content

Commit 1edaacb

Browse files
fix: updated redirect data urls (#1928)
* fix: updated redirect data urls * fix: typescript errors and accidental deletion * fix: console logging test * fix: getting the redirect header and normalizing url outisde of orig if statement * fix: added ref link * fix: removing console.log * fix: commenting/reverting changes for testing * fix: undo "commenting/reverting changes.." --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 89904cb commit 1edaacb

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

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

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ export interface FetchEventResult {
88

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

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(/^\/_next\/data\//, '')
17+
.replace(/\.json/, '')
18+
.split('/')
19+
20+
const buildId = paths[0]
21+
redirect = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
22+
}
23+
24+
return redirect
25+
}
26+
1127
/**
1228
* This is how Next handles rewritten URLs.
1329
*/
@@ -249,6 +265,12 @@ export const buildResponse = async ({
249265
res.headers.set('x-nextjs-redirect', relativizeURL(redirect, request.url))
250266
}
251267

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+
252274
if (res.headers.get('x-middleware-next') === '1') {
253275
return addMiddlewareHeaders(context.next(), res)
254276
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ 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`,
4444
{ override: 'internal' },
4545
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } },
4646
)
47-
4847
expect(res.headers.get('x-nextjs-redirect')?.endsWith(`/es/new-home?override=internal`)).toEqual(true)
4948
expect(res.headers.get('location')).toEqual(null)
5049
})

0 commit comments

Comments
 (0)