ensureQueryData in loader
I'm using Start (^1.105.3) + Query (^5.66.3) and I'm trying to ensure query data in the loader Argument in
createFileRoute
.
The problem is that I get Unexpected end on JSON input
when I reload the page (i.e server rendered) but it works when I'm loading a page without the loader and then navigate to one which has it.
Thanks in advance
createRouter:
$projectId.tsx:
projectQueryOptions:
24 Replies
harsh-harlequin•7mo ago
can the queryFn be executed on the server during SSR?
so is this initialized correctly?
client.api.projects[":projectId"].$get
xenial-blackOP•7mo ago
Quick idea how I could check?
The call itself is correct, it's Hono RPC
harsh-harlequin•7mo ago
Quick idea how I could check?call it from a server function maybe? but thats not the same as SSR so how do you create that client?
xenial-blackOP•7mo ago
It's exported from my Hono app and uses the cookies as authorization, maybe that's the problem now that I think about it...
Although, is it? I mean I request the Tanstack Start Server with the credentials included, but I don't know if they're passed to my client..
Any idea where I could look on how to implement this right? I probably need to create a seperate instance for the server rendering / my instance needs to supports both
harsh-harlequin•7mo ago
you probably would need to do a
getCookie('your-cookie')
on the server during SSR to pass the cookie on
it really depends how your client reads that cookie right now or what it expectsxenial-blackOP•7mo ago
It passes it on with
credentials: "include"
currentlyharsh-harlequin•7mo ago
how does that look like?
xenial-blackOP•7mo ago
This is just a wrapped
fetch
from Hono for the trpc-like architecture
This seems to work for the SSR side (at least it doesn't crash anymore) but of course doesn't work on the client side
harsh-harlequin•7mo ago
where is that client created?
xenial-blackOP•7mo ago
In my Start app and used in queryFn's
So basically SSR and CSR
harsh-harlequin•7mo ago
one solution could be to put the client into router context.
in your createRouter function in router.tsx (i assume you have the default files from the examples), add an arg:
then in
ssr.tsx
:
and in client.tsx
xenial-blackOP•7mo ago
This seems to work
I've added it as described but I also have Query in my
router.tsx
which wraps createTanStackRouter
with routerWithQueryClient
and the type check of routerWithQueryClient
seems to hijack the context-type and throws an error: Object literal may only specify known properties, and 'client' does not exist in type '{ queryClient: QueryClient; }
When ignoring for know (bad idea, I know) and passing it to my query options I did this:
And this seems to work! Any idea how I can fix the type error in context?harsh-harlequin•7mo ago
whats the type arg of
createRootRouteWithContext
?xenial-blackOP•7mo ago
Ohhh! This fixed it! Thank you!
This type magic takes a bit getting used to really :D
harsh-harlequin•7mo ago
😄
xenial-blackOP•7mo ago
While I have you here, is there a better way than to pass the client as an argument to every
...QueryOptions
or is this the best way for this problem?harsh-harlequin•7mo ago
show me some examples where you do that repeatedly
xenial-blackOP•7mo ago
A lot of my pages would look like this now:
harsh-harlequin•7mo ago
i see. a different way would be to not put the client into router context, but instead define a function
getHonoClient
with different implementations for client and server.
and then call getHonoClient()
wherever you need access to the clientxenial-blackOP•7mo ago
Oh, you thought of everything huh? Will try! Thank you very much for your help!
Yeah, this works beautifully and looks even good, thank you!
harsh-harlequin•7mo ago
nice
just as an FYI, why the
createIsomorphicFn()
is necessary. if you would do the classic if (typeof document === "undefined")
runtime check, the getCookie
function (and all it depends upon) would end up in the client bundlexenial-blackOP•7mo ago
I love SSR so much but I hate it for stuff like this so much aswell 😄
harsh-harlequin•7mo ago
yeah, unfortunately sometimes the abstractions leak ... and then you need an escape hatch as this
but luckily it does not happen that often
xenial-blackOP•7mo ago
Yeah, but this solution is beautiful, I really like this. It makes so much sense and everyone will understand it in the future and that's perfect!