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 want
5 Replies
Mikle
Mikle•9mo ago
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'] }, )
Samual 🦢
Samual 🦢•9mo ago
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 awaits 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
createServerData$(async () => {
const { responseHeaders } = useRequest()
const result = await somethingAsync()

responseHeaders.set("x-foo", "bar")

return result
})
createServerData$(async () => {
const { responseHeaders } = useRequest()
const result = await somethingAsync()

responseHeaders.set("x-foo", "bar")

return result
})
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 callback
Samual 🦢
Samual 🦢•9mo ago
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.
Edivado
Edivado•9mo ago
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
Samual 🦢
Samual 🦢•9mo ago
ty
Want results from more Discord servers?
Add your server
More Posts