Use `fetch` from load event
david-plugge opened this issue · comments
Describe the bug
The fetch function provided by the event inside load function deduplicates api calls by inlining the reponse.
The first fetch on the client will get that inlined data.
Expected behavior
No extra api call on the first client side load.
Additional context
This will result in calling createTRPCClient
in every load function, not sure it that´s a big deal.
After a bit of testing i found out that passing the origin in the url disables the inlining.
I modified the createTRPCClient
function and got it working:
function createTRPCClient<Router extends AnyRouter>(...) {
let link: TRPCLink<Router>;
if (init) {
const { fetch } = init;
link = httpBatchLink({ url, fetch, headers });
} else {
if (typeof window === 'undefined') {
throw new Error(
'Calling createTRPCClient() on the server requires passing a valid LoadEvent argument'
);
}
link = httpBatchLink({ url, headers });
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return createTRPCProxyClient<Router>({ transformer, links: [link] });
}
We actually don´t need the origin here because sveltekit handles it internally
Thanks for the tip! This should be fixed in v3.2.5
.