Skip to content

fix: updated redirect data urls #1928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/runtime/src/templates/edge-shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ export interface FetchEventResult {

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

function normalizeDataUrl(redirect: string) {
// If the redirect is a data URL, we need to normalize it.
// 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
if (redirect.startsWith('/_next/data/') && redirect.includes('.json')) {
const paths = redirect
.replace(/^\/_next\/data\//, '')
.replace(/\.json/, '')
.split('/')

const buildId = paths[0]
redirect = paths[1] !== 'index' ? `/${paths.slice(1).join('/')}` : '/'
}

return redirect
}

/**
* This is how Next handles rewritten URLs.
*/
Expand Down Expand Up @@ -250,6 +266,12 @@ export const buildResponse = async ({
res.headers.set('x-nextjs-redirect', relativizeURL(redirect, request.url))
}

const nextRedirect = res.headers.get('x-nextjs-redirect')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tested this together and it works well. I still have no idea why we have to set the header first, get it and the reset it. If anyone knows why, I'm curious. 🕵🏻


if (nextRedirect && isDataReq) {
res.headers.set('x-nextjs-redirect', normalizeDataUrl(nextRedirect))
}

if (res.headers.get('x-middleware-next') === '1') {
return addMiddlewareHeaders(context.next(), res)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ describe('Middleware Redirect', () => {
expect(res.headers.get('location')?.endsWith('/default/about')).toEqual(false)
})

usuallySkip(`should redirect to data urls with data requests and internal redirects`, async () => {
it(`should redirect to data urls with data requests and internal redirects`, async () => {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/es/old-home.json`,
{ override: 'internal' },
{ redirect: 'manual', headers: { 'x-nextjs-data': '1' } },
)

expect(res.headers.get('x-nextjs-redirect')?.endsWith(`/es/new-home?override=internal`)).toEqual(true)
expect(res.headers.get('location')).toEqual(null)
})
Expand Down