-
Notifications
You must be signed in to change notification settings - Fork 86
fix: handle absolute rewrite URLs #1325
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
Conversation
✅ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-export-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-rsc-demo canceled.
|
✅ Deploy Preview for next-plugin-edge-middleware ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
❌ Deploy Preview for netlify-plugin-nextjs-next-auth-demo failed.
|
Test summaryRun details
View run in Cypress Dashboard ➡️ This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard |
Test summaryRun details
View run in Cypress Dashboard ➡️ This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard |
Test summaryRun details
View run in Cypress Dashboard ➡️ This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard |
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.
Looking good to me 🚀
Also as a [pebble] comment - would be good to add test coverage here as well. Based on existing coverage it looks like Cypress might be the way to go but if there's a different approach that we want to take in this case that's fine too.
@@ -5,6 +5,19 @@ export interface FetchEventResult { | |||
waitUntil: Promise<any> | |||
} | |||
|
|||
/** | |||
* This is how Next handles rewritten URLs. |
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.
[dust] Might be handy to include the same link that was provided in the pull request summary here so it makes it easier to folks to find the relevant code that's being referred to here
/** | ||
* This is how Next handles rewritten URLs. | ||
*/ | ||
export function relativizeURL(url: string | string, base: string | 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.
[boulder] Is it a mistake that the url
parameter's type is string | string
or was it meant to be string | URL
?
export function relativizeURL(url: string | string, base: string | URL) { | |
export function relativizeURL(url: string | URL, base: string | 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.
Oh, missed this before adding automerge! That's copied directly from the Next.js source and certainly looks like a mistake!
Summary
When rewriting a URL in middleware, it attaches an
x-middleware-rewrite
header with the target URL. This is used both for server-side rewrites, but also for client-side preflights. The returned value can be either a relative or absolute URL. Previously we passed-through the header unchanged. This caused problems for two reasons. First, if it is an absolute URL it is treated as external by the router on the client, even if it is not, which caused Next to get into an infinite reload loop as it tried to redirect to itself. For actual external URLs it was failing because Netlify Edge Functions don't support rewriting to external URLs.This fixes these problems in two parts:
context.rewrite()
it instead callsfetch()
, adds the appropriate headers to the response, and returns the response.Test plan
x-middleware-rewrite
headers is relativex-middleware-rewrite
header is "http://example.com".Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal
Fixes issue mentioned in this comment
Standard checks:
🧪 Once merged, make sure to update the version if needed and that it was published correctly.