S
SolidJS16mo ago
hotshoe

root.tsx & cookies

I need to read cookie data so I can setup my theme (i.e., an html data attribute) and load a script (based on runtime data), so need access to request object from within root.tsx. A solution appears to be anything but straight-forward. I found a couple related threads (below) on loading data into root.tsx but I feel they lead down the wrong path for something that should be so simple. Is there an SS prescribed method for reading cookie data from root.tsx? Thanks! P.S. As an aside. Tried root.data.ts and works for passing in data via useRouteData, but it's undocumented afaict and isomorphic, and need it to be server only. If there's example of how to apply root.data.ts for the use case described then that could work for me. Any help much apprecaited! Thanks, -Roland. related links: https://discord.com/channels/722131463138705510/910635844119982080/1111155446368182302 https://github.com/solidjs/solid-start/issues/647
GitHub
routeData Is Not Called from the root.tsx File · Issue #647 · s...
Exporting a routeData function from root.tsx doesn't seem to do anything. The function is not called under any circumstances. It seems like this has been mentioned on Discord already, but I'...
1 Reply
hotshoe
hotshoeOP16mo ago
Did a little more reading and this seems to work, but, again, please lmk if a better or prescribed method for accomplishing the same, as this is very common use case. Thanks!
// root.data.ts
import {
createServerData$,
useRequest,
useServerContext,
} from "solid-start/server";
import { isServer } from "solid-js/web";

export default () => {
const event = useRequest();
if (isServer) {
const cookie = event.request.headers.get("cookie");
// parse it
// return parsed cookie val;
}
};
// root.data.ts
import {
createServerData$,
useRequest,
useServerContext,
} from "solid-start/server";
import { isServer } from "solid-js/web";

export default () => {
const event = useRequest();
if (isServer) {
const cookie = event.request.headers.get("cookie");
// parse it
// return parsed cookie val;
}
};
// root.tsx
const data = useRouteData<T>();

// use it wherever
// root.tsx
const data = useRouteData<T>();

// use it wherever
`
Want results from more Discord servers?
Add your server