Skip to content

fix: preserve original path after middleware rewrite #2177

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
Jun 28, 2023

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Jun 21, 2023

Description

This looks to address issue with locale not being set correctly (or at least not in the same way as next build + next start behaves) when there is rewrite happening in middleware which in turn cause weirdness with client-side routing.

Documentation

Tests

Added a test case to cypress tests

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

@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/649bfa3c85e8d400083fc5a6
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/649bfa3c0f67ca0007e88fe9
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@github-actions github-actions bot added the type: bug code to address defects in shipped code label Jun 21, 2023
@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/649bfa3c44af8500085b192e
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/649bfa3d85a81d0007690ccd
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/649bfa3ce08e4e000859cafb
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/649bfa3c80acdf0008b6f5d8
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/649bfa3c4ec02f0008bd1c81
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/649bfa3c62130a0008eae0bf
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@netlify
Copy link

netlify bot commented Jun 21, 2023

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

Name Link
🔨 Latest commit 83cf701
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/649bfa3cd69b4a0008973717
😎 Deploy Preview https://deploy-preview-2177--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 configuration.

@pieh pieh force-pushed the fix/preserve-original-path-after-middleware-rewrite branch 3 times, most recently from 8e86daa to 9de97eb Compare June 27, 2023 11:06
@pieh pieh force-pushed the fix/preserve-original-path-after-middleware-rewrite branch from 9de97eb to 2b5b6f8 Compare June 27, 2023 11:20
@pieh pieh marked this pull request as ready for review June 27, 2023 11:57
@pieh pieh requested a review from a team as a code owner June 27, 2023 11:57
Comment on lines +42 to +45
if (!parsedUrl && typeof req?.headers?.['x-middleware-rewrite'] === 'string') {
parsedUrl = parse(req.headers['x-middleware-rewrite'], true)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Next does same parsing in https://github.com/vercel/next.js/blob/7dfa56c7141ae2556af0d7fd837e0af686d8fa55/packages/next/src/server/base-server.ts#L662-L665 and later on does a lot of checking on it and potentially messing with it.

Passing parsedUrl along the request handler route allows some control

Copy link
Contributor

@orinokai orinokai left a comment

Choose a reason for hiding this comment

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

Nice catch here, thanks @pieh. I'd been wondering what the significance was of the parsedUrl and why it was always empty.

@pieh pieh added the automerge label Jun 28, 2023
@kodiakhq kodiakhq bot merged commit 9c6ec9f into main Jun 28, 2023
@kodiakhq kodiakhq bot deleted the fix/preserve-original-path-after-middleware-rewrite branch June 28, 2023 09:19
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