Skip to content

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

Merged
merged 3 commits into from
Apr 26, 2022
Merged

fix: handle absolute rewrite URLs #1325

merged 3 commits into from
Apr 26, 2022

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Apr 26, 2022

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:

  • If the absolute URL is actually local then it makes it into a relative URL using the same algorithm as Next.js
  • If the URL is external, then instead of using context.rewrite() it instead calls fetch(), adds the appropriate headers to the response, and returns the response.

Test plan

  1. Visit the middleware deploy preview
  2. View the absolute URL demo. Check that the page loads correctly and that the x-middleware-rewrite headers is relative
  3. Visit the external URL demo. Check that it returns the proxied content of "example.com". Check that the x-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

black capybara

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/6267d808e79b820008c5a921
😎 Deploy Preview https://deploy-preview-1325--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/6267d808e79b820008c5a926
😎 Deploy Preview https://deploy-preview-1325--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@ascorbic ascorbic requested a review from a team April 26, 2022 11:31
@ascorbic ascorbic self-assigned this Apr 26, 2022
@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/6267d808d5445b0009e29336
😎 Deploy Preview https://deploy-preview-1325--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/6267d807e79b820008c5a91c
😎 Deploy Preview https://deploy-preview-1325--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Apr 26, 2022
@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for next-plugin-rsc-demo canceled.

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-rsc-demo/deploys/6267d808ba5a8b0008486d72

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/6267d808fcb6ac000889632d
😎 Deploy Preview https://deploy-preview-1325--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for netlify-plugin-nextjs-next-auth-demo failed.

Name Link
🔨 Latest commit f43a041
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/6267d80828e6220009e74324

@cypress
Copy link

cypress bot commented Apr 26, 2022



Test summary

7 0 0 0


Run details

Project netlify-plugin-nextjs-static-demo
Status Passed
Commit d511775 ℹ️
Started Apr 26, 2022 11:34 AM
Ended Apr 26, 2022 11:35 AM
Duration 01:20 💡
OS Linux Ubuntu - 20.04
Browser Chrome 100

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

@cypress
Copy link

cypress bot commented Apr 26, 2022



Test summary

2 0 0 0


Run details

Project netlify-plugin-nextjs-nx-monorepo-demo
Status Passed
Commit d511775 ℹ️
Started Apr 26, 2022 11:34 AM
Ended Apr 26, 2022 11:36 AM
Duration 01:11 💡
OS Linux Ubuntu - 20.04
Browser Chrome 100

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

@cypress
Copy link

cypress bot commented Apr 26, 2022



Test summary

22 0 0 0


Run details

Project netlify-plugin-nextjs-default-demo
Status Passed
Commit d511775 ℹ️
Started Apr 26, 2022 11:34 AM
Ended Apr 26, 2022 11:36 AM
Duration 01:38 💡
OS Linux Ubuntu - 20.04
Browser Chrome 100

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

Copy link

@ericapisani ericapisani left a 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.

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) {

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?

Suggested change
export function relativizeURL(url: string | string, base: string | URL) {
export function relativizeURL(url: string | URL, base: string | URL) {

Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants