T
TanStackβ€’2y ago
optimistic-gold

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
other-emerald
other-emeraldβ€’2y ago
It would be better to use a serverFn here that has a consistent access pattern. Buuuuut, your approach isn’t bad necessarily b
mute-gold
mute-goldβ€’9mo ago
@lithdew did you find better way to implement this?
rising-crimson
rising-crimsonβ€’9mo ago
we have createIsomorphicFn now

Did you find this page helpful?