knpwrs
knpwrs
SSolidJS
Created by knpwrs on 8/19/2023 in #support
Setting cookies (or headers in general) from `createServerData$`
Is it possible to set cookies (or any other headers) from the fetcher passed to createServerData$? I have the following code:
export function routeData() {
const user = createServerData$(async (_, { request }) => {
const client = await createAuthenticatedClient(request);

return client.request<MeQuery>(gql`
// ...
`);
});

const success = createServerData$(async (_, { request }) => {
const session = await getSession(request);

return session.get('success');
});

return { user, success };
}
export function routeData() {
const user = createServerData$(async (_, { request }) => {
const client = await createAuthenticatedClient(request);

return client.request<MeQuery>(gql`
// ...
`);
});

const success = createServerData$(async (_, { request }) => {
const session = await getSession(request);

return session.get('success');
});

return { user, success };
}
getSession is just a wrapper around the cookie session storage that's built in to solid-start. When I call useRouteData<typeof routeData>() in my component I can get success from the session. success was set on a previous route as such:
session.flash('success', 'Your email has been verified!');

return redirect('/', {
headers: { 'Set-Cookie': await storage.commitSession(session) },
});
session.flash('success', 'Your email has been verified!');

return redirect('/', {
headers: { 'Set-Cookie': await storage.commitSession(session) },
});
Now I would like to commit the session with the flash message deleted. How can I do this from within createServerData$? It doesn't seem that I'm able to return a Response or anything like that, and redirect wouldn't make sense either.
3 replies