-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Unable to dynamically set response headers in page.js
components (App router)
#50914
Comments
Have you been able to fix this? This is a massive issue. |
Facing same issue, not able to migrate to app routing because of this limitation. |
related to #42112 maybe too? |
There aren't any work arounds that I'm aware of, the Next.js folks will need to provide a mechanism for these Server Components to be able to apply response headers during SSR. |
I've been trying to use this: https://nextjs.org/docs/pages/api-reference/next-config-js/headers. |
How would I go about it if my api upstream is responsible for setting the headers? I would retrieve them out of my getInitialProps and set them based on what the API upstream was telling to set it. But I can no longer do that |
This is massive not having this considering I was doing stuff like this before in pages:
|
@Nhollas pretty much in the same boat as you. And doing it through the middleware isnt really an option either because even if managed to do that it would trigger the external API request twice |
buuuuuuuuuuump!! |
We are having this exact issue aswell! |
Having the same issue while trying to add the last-modified header dynamically! |
Similar issue, looking for workarounds myself. Anyone know: is it possible to use |
any news ? |
I think the recommended approach is to move that logic up to Next.js Middleware |
If that's recommended, how would I set a response header based on a value that was asynchronously fetched in a |
You can rely on Next.js's caching to de-dupe requests from Middleware and from a page. So you'd fetch the data you are fetching from your page, within the middleware, use the response to set the cookies. Then the page should see a cache HIT when it performs the same fetch, using cached data to avoid duplicate fetches. |
So the recommendation is to stuff a copy/pasted duplicate |
We are facing a similar issue. We are using the app router structure and need to set a response header inside the page.tsx file. Do you have any ideas on how to accomplish this? Or perhaps a method to pass a value to the middleware and set the header there? |
Is there any way to do this without using the middleware? The issue we are facing is that we need to hit an API to get that status to set the API. But we need to proxy the request, which we were using node-fetch. However, middleware only uses edge so we can't use node-fetch and there seems to be no way to set it as Node. So we are caught in a catch-22, we can't set the headers in the middle because we don't have the data, and where we can get the data, we set the headers! If there a solution to this? |
For people who faced with this problem I can recommend my temporary solution:
|
Data Cache and deduping is not available on the middleware. Not being able to set headers in |
@cvle As far as I understand, middleware is for use cases such as authentication etc. Why do you need data cache in a middleware? Even if it did support, using It would make your application very slow because calls to Data Cache still takes 20-100ms. De-duping is not supported because Middleware is a different runtime and it runs separately before the page functions. Not being able to set headers is definitely frustrating but there are some workarounds as well. |
We need to set custom headers in our page.ts, can you tell us the workarounds @4hmaad ? |
@timneutkens Who do we have to @mention on this issue to get some movement? It would be nice to get an official response from the Next.js folks on possible workarounds and if they are currently working on adding the ability to set dynamic response headers within a Page component using the app router. |
bump! Like many other people, this missing feature is the only reason I cannot use the App Router structure. |
This may sound silly, but this isn't the first time (and probably not the last one) I see problems with mutating HTTP request/response headers. On the old router, the |
@julianobrasil As far as I am aware, there is no way to access the response object(o.g context.res) from a page on the new App Router structure. That is the limitation at hand here. We do not have a way to modify the response headers after retrieving context during SSR for a given page. |
Any ideas how to solve this or who we can @mention? Other idea, does anyone have an idea how to access the contents of a file in the middleware? fs doesn't work in the middleware for me. |
I have only found this out a week before we launch a big project. Seriously there is not way to set http headers? I will have to migrate back the the pages router now. |
This appears to work! Put this in middleware.ts at the root level of the repo.
Hmm wipes out the etag too then but maybe I can figure that out by taking a hash of the html response or something along those lines. Hmm the middleware runs before page is rendered so that makes it tricky. |
We're going to post a discussion next week with more details about setting headers outside Middleware and invite feedback on the use cases / product requirements you're looking for 🙏 |
Hey folks, I've written up a bit about our reasons for not currently having a way to set arbitrary headers during a render with App Router here: #58110 The short version is, We stream the responses while we render in App Router and headers generally have to go before the body. I'd encourage everyone here who feels strongly that there should be an API like the one proposed here to contribute to the discussion by providing more details about your use case. I'm going to close this issue for now because we are not going to add the API as proposed here |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue or a replay of the bug
n/a
To Reproduce
Create an
app/page.js
file and then cry when you realize there is no way to set a response header based on data you fetched to render the page.Describe the Bug
When using the Page router, you are able to dynamically set response headers within the
getServerSideProps
function since it is passed a reference to theresponse
object. When using the App router, there is no method in which you can dynamically set a header on the response based on data fetches that occur when SSR the page. This appears to be a regression or missed feature that wasn't migrated from the Page router.Expected Behavior
When using the App router, you can get a read-only copy of the request headers, but I'd expect there to be a method in which I can dynamically set the response header based on data I fetched to render the page server-side.
The main use case for us is to set the
Surrogate-Key
header which is used by our CDN (Fastly) to programmatically purge the cache via tagging: https://docs.fastly.com/en/guides/working-with-surrogate-keysThe only methods I see for setting response headers currently when using the App router is to apply them in the middleware or in the next.config.js. Both of these options are not aware of the data that was fetched when rendering a page so neither of them are able to set headers using data from the page.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: