-
Notifications
You must be signed in to change notification settings - Fork 86
fix: add missing data to middleware request object #1634
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
When the middleware runs on the edge, it's missing data that is hydrated by the NextServer when run in the context of the origin server. Also fix which property is accessed on nextURL in the context of the MiddlewareRequest.
✅ Deploy Preview for netlify-plugin-nextjs-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-nx-monorepo-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-edge-middleware ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-hp-edge-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
@@ -81,10 +82,11 @@ const handler = async (req: Request, context: Context) => { | |||
const request: RequestData = { | |||
headers: Object.fromEntries(req.headers.entries()), | |||
geo, | |||
url: url.toString(), | |||
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.
This changes addresses the missing pathname
property on req.nextURL
✅ Deploy Preview for next-plugin-rsc-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-canary ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for nextjs-plugin-custom-routes-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-i18next-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
❌ Deploy Preview for next-plugin-app-dir failed.
|
@@ -48,6 +48,12 @@ Next.js Middleware works out of the box on Netlify. By default, middleware runs | |||
support for running Middleware at the origin, set the environment variable `NEXT_DISABLE_NETLIFY_EDGE` to `true`. Be | |||
aware that this will result in slower performance, as all pages that match middleware must use SSR. | |||
|
|||
### Limitations | |||
|
|||
Due to how the site configuration is handled when it's run using Netlify Edge Functions, data such as `locale` and `defaultLocale` will be missing on the `req.nextUrl` object when running `netlify dev`. |
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.
When you say "such as", does that mean there are other things missing?
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.
I haven't yet done an audit of all the properties that are affected by this so I'm not 100% certain that these are the only properties affected on the nextUrl
object.
Something to add here as well but the pathname (as reported by the user who originally raised this issue with us) was also incorrect before these changes and should be mentioned.
@@ -0,0 +1,2 @@ | |||
// eslint-disable-next-line n/no-unpublished-require | |||
require('jest-fetch-mock').enableMocks() |
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.
Without this package and setup, the Request
and Headers
objects aren't found in the context of the test file that was added (request.spec.ts
)
Summary
When the middleware runs on the edge, it's missing data that is hydrated by the NextServer when run in the context of the origin server.
Also fixes which property is accessed on nextURL in the context of the MiddlewareRequest.
Test plan
Test site
Can also do the following:
next.config.js
of the middleware demo to include the following:locale
anddefaultLocale
en-US
Note: It's a known issue that the middleware example isn't working entirely as expected and are not related to these changes. For instance, the path to set the
netlifyCookie
via the 'Cookies API' route isn't setting the cookie properly, although the user will be routed to the correct page. Issue for this has been created hereRelevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal
Fixes https://github.com/netlify/pod-ecosystem-frameworks/issues/190
Standard checks:
🧪 Once merged, make sure to update the version if needed and that it was published correctly.