Next.js Discord

Discord Forum

Getting headers in client side code in app router

Unanswered
Pond loach posted this in #help-forum
Open in Discord
Pond loachOP
Hello, How can i get header "Accept-Language" in client sided code?

23 Replies

@Pond loach Hello, How can i get header "Accept-Language" in client sided code?
Catalan Sheepdog
First method: You can use middleware that get header within it. And then you can import it to your client-sided code.
Second method: You can use server component like before.
If you need detailed code, please feel free to contact me.
Looks like easier method
@Pond loach Looks like easier method
Catalan Sheepdog
You mean above methods are too easy?
Pond loachOP
Yep, but when i have for every path make server component, rather not 😄 Or if i do not need? Still i dont know it this great
@Catalan Sheepdog You mean above methods are too easy?
Pond loachOP
Not too easy
Just looks like easier 😄
Catalan Sheepdog
so you mean do you have any other opinion? just tell me about it.
Pond loachOP
No i dont, thats the reason why am i asking for an example
Catalan Sheepdog
i don't think it is not so hard to making the server component that get the header
Pond loachOP
How looks example with middleware?
I cant imagine it
Catalan Sheepdog
If you have to use header in many components, it can be straightforward method.
Pond loachOP
I do, in every component.
Catalan Sheepdog
you mean every component need header?
Pond loachOP
I mean not every, cause i can save it to cookies
Catalan Sheepdog
oh can you send me your issue image?
Pond loachOP
I have no issue just wonder how to get headers in client side renderring
Catalan Sheepdog
Getting header is server side option so you have to use server component. It is the only way I think so.
Pond loachOP
Soo no middleware.ts?
Use a context provider in your layout to feed server components with the header.