using next/headers in a server component (not page or layout)
Unanswered
European sprat posted this in #help-forum
European spratOP
my
the problem is no headers are found.
layout.tsx
is importing a server component Footer.tsx
Footer
is importing headers
from next/headers
the problem is no headers are found.
Footer
snippet:const FooterInner = ({ dict, lng }: Props) => {
// const headerStore = headers();
// console.log("FooterInner -- headerStore.values()", headerStore.values()); <======= This should log headers
return (
<>
</>
);
};
3 Replies
headers function return an iterator if you want to log all the values use
to get a single value use
More info:
https://developer.mozilla.org/en-US/docs/Web/API/Headers/values
console.log(Object.fromEntries(headers().entries()));
to get a single value use
get
headers().get("my-header-name")
More info:
https://developer.mozilla.org/en-US/docs/Web/API/Headers/values
European spratOP
thanks @Jaga Santagostino I tried to do that as well and still received a blank object.
I did the same thing in middleware.ts and this worked. however, I don't want to use middleware for this case.
I did the same thing in middleware.ts and this worked. however, I don't want to use middleware for this case.
Asian black bear
Have you tried accessing a specific header directly? Because iterating and trying to log all of them is an uncommon and unrealistic usecase to begin with