Next.js Discord

Discord Forum

using next/headers in a server component (not page or layout)

Unanswered
European sprat posted this in #help-forum
Open in Discord
Avatar
European spratOP
my 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

Avatar
Jaga Santagostino
headers function return an iterator if you want to log all the values use
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
Avatar
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.
Avatar
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