Next.js Discord

Discord Forum

"ReadonlyHeadersError: Headers cannot be modified."

Unanswered
cnow posted this in #help-forum
Open in Discord
How do i fix this? if i remove the headers property, then my api route that uses getServerSession dosen't work properly.

31 Replies

There should be no need to manually call fetch through nextauth's api endpoints
@alfon What are you trying todo? Why did you call fetch to next auth?
‘serverstatus’ is my own route, i just put it in the ‘api/auth’ folder
i actually am not sure why i put it there, but is that what’s causing my problem?
Cookies are automatically send within each request
So you dont need to send header again
Just call getServerSession(authOption) in your route ts
@alfon So you dont need to send header again
When i remove it, the getServerSession within my route returns null
btw it works fine in development but on vercel production server i get the Readonly headers error
@cnow When i remove it, the getServerSession within my route returns null
Sounds like the user arent logged in then
@alfon Sounds like the user arent logged in then
but they are, when i send a request to my route in my browser or with postman it works just fine
Is serverStatus in a client component?
nope
Then why use fetch to your own api in server component :/
On prod there will be no localhost
@alfon Then why use fetch to your own api in server component :/
Sorry, What am i supposed to be doing?
Just put the logic directly inside the server component
Okay, let me give that a try
getServerSession works by calling cookies() and headers() under the hood. So if the user doesnt have the cookie, then the user is not logged. Either that or you have set the incorrect AuthOptions
@alfon getServerSession works by calling cookies() and headers() under the hood. So if the user doesnt have the cookie, then the user is not logged. Either that or you have set the incorrect AuthOptions
then how come when i add headers() to my fetch it works just fine(in development mode, atleast) and when i don't include it dosen't work?
I have no idea withou knowing more information
I have used client side fetching to endpoint that uses getServerSession without sending header and it works
@alfon Just put the logic directly inside the server component
I tried this and it works fine, thanks for the help
still weird how i cant pass headers properly to the api route but oh well
@cnow still weird how i cant pass headers properly to the api route but oh well
You dont have to.. every request has their own header...
Its redundant
@alfon You dont have to.. every request has their own header...
It dosen't work for me if I don't pass in headers, you can try it yourself to reproduce it. create an api route that has getServerSession(authoptions), then call it from a page with fetch, getserversession returns null
ofc
coz when server calls fetch to a server it doesn't have a header
don't call the server from the server
Allegheny mound ant
@cnow today I faced this exact issue, hoping your solution solves it