T
TanStackβ€’16mo ago
adverse-sapphire

Isomorphic code in loader functions w/ SSR

What is the recommended way to write isomorphic code (code for both client/server, SSR-only, etc) for loaders? i.e. if I want to read the users cookies in the loader function, or headers used for a SSR request etc? For cookies right now I'm doing the following - would like to know how code like this is intended to be done πŸ™ .
beforeLoad: () => {
if (!import.meta.env.SSR) {
return { cookies: cookie.parse(document.cookie) };
} else {
return { cookies: getRequestCookies() };
}
},
beforeLoad: () => {
if (!import.meta.env.SSR) {
return { cookies: cookie.parse(document.cookie) };
} else {
return { cookies: getRequestCookies() };
}
},
3 Replies
metropolitan-bronze
metropolitan-bronzeβ€’16mo ago
It would be better to use a serverFn here that has a consistent access pattern. Buuuuut, your approach isn’t bad necessarily b
adverse-sapphire
adverse-sapphireβ€’7mo ago
@lithdew did you find better way to implement this?
ratty-blush
ratty-blushβ€’7mo ago
we have createIsomorphicFn now

Did you find this page helpful?