How to set a cookie when using `createRouteData` or `createServerData$`?
For user authentication it may be necessary to set a cookie. In Remix, for example, it is solved so that a
loader
can either directly return a value or alternatively a Response
. The Response
can then be used to set the Set-Cookie
header. Remix seems to recognize whether the values are returned directly or via a Response
, e.g. with json({...})
, and interprets the types correctly when calling useLoaderData<typeof loader>()
. This does not seem to work with SolidStart. Is there an alternative way? https://remix.run/docs/en/v1/route/loader9 Replies
https://discord.com/channels/722131463138705510/1055857658952962058
This is how I made it work. Dunno if it's the best way though. Server side I just use the createCookieSessionStorage
That's more or less the same: you should use
createCookieSessionStorage
in conjonction with createServerData$()
that would return a Response, like in Remix.
I think it is necessary to do it only in createServerData$
so you don't leak the SESSION_SECRET
: the cookie should be signed on the server – to be confirmedSolidStart Beta Docuentation
SolidStart Beta Documentation
Early release documentation and resources for SolidStart Beta
This way the returned value of
useRouteData
is a Response
and I cannot use the data in the component as it works in Remix. Maybe @nksaraf knows the answer.if u return a Response from createServerData, you can use that data, even if its Response. You will just have to get the data using response.json(), might need another resource
@nksaraf thanks for your answer! Is it meant as in the following code example?
Is it possible to implement
createRouteData
and createServerData
similar to Remix, so that it makes no difference whether you return the values directly or via a Response
, e.g. with json()
? Otherwise unpacking the data in the component leads to a lot of boilerplate code. Especially with createRouteData
this would make the code more consistent and lead to a better DX.
@nksaraf what are your thoughts on this? If you're interested, I can take a look at the code on GitHub and create a PR. Cookies are not uncommon in data fetching, so I expect more people will run into this "problem" in the future.I completely agree .. we need a good way of doing this
Would love to look at PRs
related with https://github.com/solidjs/solid-start/pull/485?
GitHub
Expose responseHeaders to createServerData$ by oscartbeaumont · Pul...
This PR exposes a way to set HTTP headers on the response within the createServerData$ function.
This makes it possible to do:
export function routeData() {
return createServerData$((_, { respons...
Thanks for linking @colinshen!