Skip to content
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

Headers set by middleware not accessible to edge route handlers on prod and any kind of route handler on dev #40722

Open
1 task done
yourtallness opened this issue Sep 20, 2022 · 4 comments
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.

Comments

@yourtallness
Copy link

yourtallness commented Sep 20, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:22 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T6000
Binaries:
Node: 16.16.0
npm: 8.11.0
Yarn: 3.2.3
pnpm: N/A
Relevant packages:
next: 12.3.1-canary.5
eslint-config-next: 12.3.0
react: 18.2.0
react-dom: 18.2.0

What browser are you using? (if relevant)

Version 105.0.5195.125 (Official Build) (arm64)

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

Headers set by middleware are not consistently accessible to the route (api or page) across route types & environments.

The following list summarizes the behavior:

Prod:

  • node route handlers can read header set in middleware
  • edge route handlers cannot read header set in middleware

Dev:

  • node route handlers cannot read header set in middleware
  • edge route handlers cannot read header set in middleware

Expected Behavior

A header set by the middleware should be accessible via req.headers.get() or the req.headers object for all route types and on all environments.

Link to reproduction

https://github.com/yourtallness/next-middleware-tests

To Reproduce

The sample app is deployed on vercel here.

The sample middleware sets a header:

// Set a header
response.headers.set('my-header', 'baz');

The following table shows if the header can be read back in the route handler.

Test case Production Development
GET /api/node link link
GET /api/edge link link

The same behavior occurs on pages depending on whether they are using the nodejs or edge runtime.

Note: The header is included in the final response to the client, but the issue is route handlers cannot dependably run custom business logic based on a header set in the middleware.

@yourtallness yourtallness added the bug Issue was opened via the bug report template. label Sep 20, 2022
@StephaneBischoff
Copy link

StephaneBischoff commented Sep 23, 2022

I have the same issue with cookies and headers

@yourtallness
Copy link
Author

True, cookies are not accessible in any case.

But the fact that headers do work in the node api route case makes me think this could be ported to the other cases.

@yourtallness
Copy link
Author

For lack of a current mechanism to pass a value from the middleware to the route handlers, @colinclerk has made the following writeup that provides snippets which can be used for this purpose:

How to pass a value from Next.js middleware to API routes and getServerSideProps

Sharing here in case this comes in handy for anyone that had the same need to pass data as we did.

@vercel-release-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@vercel-release-bot vercel-release-bot added the stale The issue has not seen recent activity. label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

3 participants