Client headers do not get passed to API routes called in loader/beforeLoad {TanStack Start}
I wanted to create an architecture where I have an API and frontend together using TanStack Start whilst having SSR so I tried to achieve this using the API routes provided by the framework.
But when I want to fetch data from authenticated API routes in the loader the client's cookies/headers containing authentication info don't get passed to API.
The only method I found to resolve this is to wrap the API call in a server function and use the
getHeaders()
function to pass the headers to the fetch call.
Is there any other way to resolve this without using a server function23 Replies
yappiest-sapphire•12mo ago
I believe you will need to manually get the client headers when in an SSR context. If you want an isomorphic approach you can make a wrapper around your http calls - for example I'm using ts-rest which has an easy way to customize each request, so this is what my client looks like
Then I can use my api client isomorphically from both a client and a ssr context
Doing this with fetch is harder since there's no built in way to wrap each request, so you either need to build your own wrapper or use a 3rd party one that implements that functionality
unwilling-turquoiseOP•12mo ago
Have you tried running a build. I always end up with build errors when I access
getHeaders
outside of a server function.
This is my current workaround by wrapping the getHeader
call in a server function
yappiest-sapphire•12mo ago
Yeah build isn't working for me - I did something similar in solid start with vinxi and lazy imports worked if they were imported in a check for isServer but it seems like that doesn't work in tanstack router
I bet using a server function there is a perfectly fine workaround
unwilling-turquoiseOP•12mo ago
Thanks
yappiest-sapphire•12mo ago
Take a look at
vite-env-only
for a server specific import, it's recommended in the react remix docs. I just set it up and it seems to be working well https://github.com/pcattori/vite-env-onlyGitHub
GitHub - pcattori/vite-env-only
Contribute to pcattori/vite-env-only development by creating an account on GitHub.
yappiest-sapphire•12mo ago
i added envOnlyMacros to my vite plugins, and then for my api client I have serverOnly$ prevents the code inside and related imports from making it into the client bundle, and returns whatever is passed to it in a server context or
undefined
in a client contextunwilling-turquoiseOP•12mo ago
Thanks. Yet to try it but posting the relevant remix documentation here for reference
https://remix.run/docs/en/main/discussion/server-vs-client#vite-env-only
unwilling-turquoiseOP•12mo ago
It worked successfully
My only issue is with the
envOnlyMacros
config plugin having a type error so I had to set it to any
harsh-harlequin•12mo ago
about that type error: most likely you have multiple versions of vite installed somehow
make sure you use the same everywhere
yappiest-sapphire•12mo ago
I also get the same type error - it seems there could be a discrepancy between the typings of the plugins field on
vite.config.ts
and the vite plugin’s plugin field on app.config.ts
in vite.config.ts
, plugins is PluginOption[] | undefined
whereas the plugins field exported from the start config is ((...args: unknown[]) => Plugin<any>[]) | undefined
. According to vite Plugin<any>
is specifically a resolved plugin and PluginOption
is a union of Plugin<any>
plus a bunch of other types that vite can resolve to a pluginharsh-harlequin•12mo ago
looking at this right now
yappiest-sapphire•12mo ago
also, my output from
bun pm ls --all | grep vite
should mean I only have one version of vite installed? I've had some weirdness with packages installed with bun though so I'll try a clean install
harsh-harlequin•12mo ago
i don't know how bun works sorry
but looks fine
i just noticed with pnpm sometimes you can end up in situations where multiple versions are installed of deps
and then types conflict
unwilling-turquoiseOP•12mo ago
Just run a clean install and it didn't work
Here's the type error
harsh-harlequin•12mo ago
can you please the packages built for this PR:
https://github.com/TanStack/router/pull/2407
harsh-harlequin•12mo ago
yappiest-sapphire•12mo ago
types look good, let me make sure everything builds correctly
harsh-harlequin•12mo ago
love the fast feedback!
yappiest-sapphire•12mo ago
haha of course thanks for the quick fix as well!
build looking good
harsh-harlequin•12mo ago
so I also just tested this locally, works
merging it
harsh-harlequin•12mo ago
GitHub
Release v1.58.8 · TanStack/router
Version 1.58.8 - 9/24/24, 5:41 PM
Changes
Fix
start: use vite.PluginOption instead of vite.Plugin (#2407) (59064b6) by Manuel Schiller
Packages
@tanstack/start@1.58.8
unwilling-turquoiseOP•12mo ago
Updated and it's working without any type errors now
harsh-harlequin•12mo ago
cool
thanks for reporting