Skip to content
Merged
Changes from 2 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
2 changes: 1 addition & 1 deletion edge-runtime/lib/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const buildResponse = async ({
logger.withFields({ redirect_url: redirect }).debug('Redirect url is same as original url')
return
}
edgeResponse.headers.set('location', redirect)
edgeResponse.headers.set('location', relativizeURL(redirect, request.url))
Copy link
Contributor Author

@pieh pieh Sep 16, 2025

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

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

}

// Data requests shouldn't automatically redirect in the browser (they might be HTML pages): they're handled by the router
Expand Down
Loading