-
Notifications
You must be signed in to change notification settings - Fork 95
fix: produce relative redirect location url for same origin #3129
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
fix: produce relative redirect location url for same origin #3129
Conversation
📊 Package size report 0%↑
Unchanged files
🤖 This report was automatically generated by pkg-size-action |
| return | ||
| } | ||
| edgeResponse.headers.set('location', redirect) | ||
| edgeResponse.headers.set('location', relativizeURL(redirect, request.url)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: if redirect target is external, the location will remain absolute - relativeURL only make it relative if origin/host of redirect and request.url matches
opennextjs-netlify/edge-runtime/lib/util.ts
Lines 85 to 92 in c29381e
| export function relativizeURL(url: string | string, base: string | URL) { | |
| const baseURL = typeof base === 'string' ? new URL(base) : base | |
| const relative = new URL(url, base) | |
| const origin = `${baseURL.protocol}//${baseURL.host}` | |
| return `${relative.protocol}//${relative.host}` === origin | |
| ? relative.toString().replace(origin, '') | |
| : relative.toString() | |
| } |
Added some unit tests for this in dbde615
| expect(response.headers.get('location'), 'added a location header').toBeTypeOf('string') | ||
| expect( | ||
| new URL(response.headers.get('location') as string).pathname, | ||
| new URL(response.headers.get('location') as string, 'http://n').pathname, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added base here (and few other tests) because relative location without it just fails to construct URL object and throws Invalid URL. We do assert on pathname URL portion anyway so base will be discarded on actual assertion
Description
This is not really fixing something currently not behaving correctly, but brings our middleware redirect handling closer to behavior on Vercel where "local" redirect doesn't result in
locationredirect header being absolute