set response header from within `createServerData$()` callback
hi how do I set a response header from within the callback of
createServerData$()
?
useRequest().responseHeaders
is undefined
(despite typescript saying it's always a Headers
object)
this
evaluates to a different PageEvent
that is also missing the responseHeaders
property
I've tried setting the .responseHeaders
property with the headers I want myself on both but neither worked
my goal is just to set a cookie when the server data is fetched, that's all I want5 Replies
You can access the request headers and set cookies like this:
createServerData$(
async (_, { request }) => {
request.headers.set('cookie', request.headers.get('cookie') || '')
},
{ key: () => ['auth_user'] },
)
what does that achieve?
when trying to help people please test your proposed solution
okay I figured it out,
useRequest().responseHeaders
is defined at the beginning of the callback
but as soon as the callback await
s something, it becomes undefined
but if you save a reference to it to a variable
making changes still works even after an await
so the lesson is always have useRequest()
at the top of a function if you plan on using it
that only fixes initial server render though
for when the resource is invalidated and the client side router is fetching the data again
useRequest().responseHeaders
is undefined
even at the beginning of the callbackjudging by https://github.com/solidjs/solid-start/blob/043577a6400b8461baa2345e70b665f86da9692f/packages/start/server/server-functions/server.ts#L160 it doesn't look like this is fixable
GitHub
solid-start/packages/start/server/server-functions/server.ts at 043...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
leaving this here: https://github.com/solidjs/solid-start/pull/485#issuecomment-1676906831
also @giyomoon created a patch that let's you use responseHeaders in both cases https://github.com/solidjs/solid-start/pull/485#issuecomment-1677699897
ty