Next.js Discord

Discord Forum

Cannot read headers in the getServerSideProps

Answered
Giant Chinchilla posted this in #help-forum
Open in Discord
Giant ChinchillaOP
I'm passing some headers in the middleware like this
    const response = NextResponse.rewrite(new URL(urlString, request.url));

    response.headers.set('access_from', 'first_domain');
    return response;

than I'm getting undefined in getServerSideProps
when I read like
context.res.getHeader('access_from')

but I'm getting undefined, why?
Answered by joulev
Try adding the header to the request object instead of the response object
View full answer

11 Replies

Answer
@joulev Try adding the header to the request object instead of the response object
Giant ChinchillaOP
nah, it was working like that, but from the point when we upgraded next.js to 13, it breaks.
Really? That’s… surprising. Try reproducing it again in a more minimal setup and send me this minimal reproduction repository.

The reason setting it to response doesn’t work is because I’m fairly sure the response header is only added after gssp when everything is already done
@joulev Try adding the header to the request object instead of the response object
Giant ChinchillaOP
wait, how to do that
middleware only provides request
Giant ChinchillaOP
I can see that headers in chrome
@Giant Chinchilla middleware only provides request
request.headers.set(…)
@Giant Chinchilla I can see that headers in chrome
Yes because the header is still added, it’s just added after gssp is already run
Giant ChinchillaOP
it seems like its working
thanks
its weird because it used to work like that before