Skip to content

fix: correctly handle matchers with lookaheads and i18n #1693

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 7 commits into from
Oct 19, 2022

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Oct 18, 2022

Summary

Currently we translate the Next.js middleware matcher regex into a regex used to target the edge function. We double-check when the edge funciton runs, because there are addiitonal conditions that can be set in matchers that can't be expressed in the path regex. However the Next.js and Netlify Edge Function regexes are not fully compatible, because for performance reasons the Go regexp library (used in the Netlify proxy) doesn't support lookaheads like JavaScript does, and the Next.js docs now suggest using these.

This PR parses the matcher regexes, and strips any lookahead clauses before writing them to the Netlify manifest. Loosening them in this way doesn't prevent them from working, because it retains the lookaheads in the matchers used in the edge functions themselves, so the condition is still enfored at that point.

Additionally, if a site uses i18n, the generated regex was not matching the default locale. This is because Next.js checks the path after it has been normalized and the default locale interpolated. However Netlify runs the match before it it normalised. This PR loosens that regex by making the locale slug optional, meaning it matches the default locale with no slug included.

Test plan

  1. The middleware test site now includes i18n, ensuring the tests pass with it enabled. It also includes a matcher with a negative lookahead. Load this page, which is excluded with a negative lookahead and ensure the x-modified-edge header is not set in the response. Load this page and see that it is set.

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

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 Oct 18, 2022

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

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/63501e595a7ac600089f76fb
😎 Deploy Preview https://deploy-preview-1693--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 Oct 18, 2022

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

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/63501e59291c48000857d151
😎 Deploy Preview https://deploy-preview-1693--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 Oct 18, 2022

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

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/63501e590cc8af0008152ae6
😎 Deploy Preview https://deploy-preview-1693--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.

@netlify
Copy link

netlify bot commented Oct 18, 2022

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

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/63501e599c44d20008fbeaea
😎 Deploy Preview https://deploy-preview-1693--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 Oct 18, 2022

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

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/63501e591601f60009ddbbb5
😎 Deploy Preview https://deploy-preview-1693--netlify-plugin-nextjs-next-auth-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 Oct 18, 2022
@netlify
Copy link

netlify bot commented Oct 18, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 9fe2fe6
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/634ea5a5757d160008801c4d
😎 Deploy Preview https://deploy-preview-1693--next-plugin-canary.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 Oct 18, 2022

Deploy Preview for next-plugin-rsc-demo ready!

Name Link
🔨 Latest commit 9fe2fe6
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-rsc-demo/deploys/634ea5a571e7740009c6d4e6
😎 Deploy Preview https://deploy-preview-1693--next-plugin-rsc-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 Oct 18, 2022

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

Name Link
🔨 Latest commit 9fe2fe6
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/634ea5a558824800099e16cd
😎 Deploy Preview https://deploy-preview-1693--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.

@netlify
Copy link

netlify bot commented Oct 18, 2022

Deploy Preview for next-plugin-rsc-demo ready!

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-rsc-demo/deploys/63501e59375f6b00087e32db
😎 Deploy Preview https://deploy-preview-1693--next-plugin-rsc-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 Oct 18, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/63501e592a5e70000a9996ce
😎 Deploy Preview https://deploy-preview-1693--next-plugin-canary.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 Oct 18, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 9fe2fe6
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/634ea5a563df8f00090f94c8
😎 Deploy Preview https://deploy-preview-1693--next-i18next-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 Oct 18, 2022

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/63501e59dd798700089e0ecc
😎 Deploy Preview https://deploy-preview-1693--nextjs-plugin-custom-routes-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 Oct 18, 2022

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

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/63501e595341180009efd35b
😎 Deploy Preview https://deploy-preview-1693--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.

@netlify
Copy link

netlify bot commented Oct 18, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit a265850
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/63501e59c345400008de6f79
😎 Deploy Preview https://deploy-preview-1693--next-i18next-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 marked this pull request as ready for review October 19, 2022 09:41
@ascorbic ascorbic requested a review from a team October 19, 2022 09:41
Copy link

@sarahetter sarahetter left a comment

Choose a reason for hiding this comment

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

LGTM!

@kodiakhq kodiakhq bot merged commit 9d440ab into main Oct 19, 2022
@kodiakhq kodiakhq bot deleted the mk/matcher-magic branch October 19, 2022 16:08
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.

2 participants