Skip to content

feat: add Next 13 request header mutation to middleware #1866

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 15 commits into from
Jan 9, 2023

Conversation

ericapisani
Copy link

Summary

Test plan

  1. Visit the Deploy Preview (insert link to specific page) ...

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 Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/63bc1e1187118a000c89fcf1
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/63bc1e111b14d60008b5e5c8
😎 Deploy Preview https://deploy-preview-1866--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.

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Jan 4, 2023
@netlify
Copy link

netlify bot commented Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/63bc1e11c21c3e00091d7334
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/63bc1e12652ede000986cad0
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/63bc1e127cffe80009283ff5
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/63bc1e12fb39ce00079f75bd
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/63bc1e12816de80008c3c434
😎 Deploy Preview https://deploy-preview-1866--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.

@netlify
Copy link

netlify bot commented Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/63bc1e12b966820009641e96
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

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

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/63bc1e12652ede000986cad4
😎 Deploy Preview https://deploy-preview-1866--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 Jan 4, 2023

Deploy Preview for ep-next-middleware-request-mutation ready!

Name Link
🔨 Latest commit 6e6152c
🔍 Latest deploy log https://app.netlify.com/sites/ep-next-middleware-request-mutation/deploys/63bc1e1261c73c0008729756
😎 Deploy Preview https://deploy-preview-1866--ep-next-middleware-request-mutation.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.

package.json Outdated
@@ -103,6 +103,9 @@
"!**/test/fixtures/**",
"!**/test/sample/**"
],
"moduleNameMapper": {
"https://deno.land/x/[email protected]": "<rootDir>/test/mockedDenoModules/html_rewriter"
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use deno test instead of Jest for EF stuff

Copy link
Contributor

Choose a reason for hiding this comment

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

// * https://github.com/vercel/next.js/blob/68d06fe015b28d8f81da52ca107a5f4bd72ab37c/packages/next/server/next-server.ts#L1918-L1928
// * https://github.com/vercel/next.js/blob/43c9d8940dc42337dd2f7d66aa90e6abf952278e/packages/next/server/web/spec-extension/response.ts#L10-L27
export function updateModifiedHeaders(response: Response) {
const overriddenHeaders = response.headers.get('x-middleware-override-headers') || ''
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the fallback if you're checking for falsiness right away

const oldHeaderKey = 'x-middleware-request-' + header
const headerValue = response.headers.get(oldHeaderKey) || ''

response.headers.set(header, headerValue)
Copy link
Contributor

@ascorbic ascorbic Jan 5, 2023

Choose a reason for hiding this comment

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

We don't want to set these in the response - it's the request that needs them, so we should be calling this before calling context.next() and setting them on req instead

Copy link
Author

Choose a reason for hiding this comment

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

The request headers appear to be fine in that they don't have the x-middleware-request string prefixed to every header, nor does it contain the x-middleware-override-headers header - it's the result.response headers that have them and are not having them cleaned up.

Would be happy to pair on this tomorrow though as there's likely more insight that you have that I'm missing

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not setting them on the request though: https://deploy-preview-1866--next-plugin-edge-middleware.netlify.app/api/hello
They're not meant to be on the response at all, so they need to be stripped and not added back in. They're just meant to be in the request that sent to the origin.

Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Sorry, I somehow missed that this was still in draft! Added a few comments.

@ericapisani
Copy link
Author

@ascorbic I don't mind the early feedback, thank you! 😄

@ericapisani ericapisani marked this pull request as ready for review January 5, 2023 21:00
@ericapisani ericapisani requested a review from a team January 5, 2023 21:00
@ascorbic
Copy link
Contributor

ascorbic commented Jan 6, 2023

…fy/next-runtime into ep/support-request-header-mutation
Copy link
Contributor

@ascorbic ascorbic left a comment

Choose a reason for hiding this comment

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

Great work! One [pebble]: move the Cypress test from "enhanced" to "standard" because it's now testing the core feature.

@kodiakhq kodiakhq bot merged commit 5d60191 into main Jan 9, 2023
@kodiakhq kodiakhq bot deleted the ep/support-request-header-mutation branch January 9, 2023 14:24
@ericapisani ericapisani linked an issue Jan 9, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Next 13 request header mutation
3 participants