T
TanStack6mo ago
fair-rose

Global Persisting Cache

Hiya, really confused with how i would go about having global persisting cache with tanstack start and tanstack query? 1) I see the docs only mention persisting it stuff in localstorage.. but i would like to have a global cache like nextjs. 2) Even if i do localstorage.. i get errors that localstorage is not defined
import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persister";
import { QueryClient } from "@tanstack/react-query";
import {
persistQueryClient,
removeOldestQuery,
} from "@tanstack/react-query-persist-client";
import { createRouter as createTanStackRouter } from "@tanstack/react-router";
import { routerWithQueryClient } from "@tanstack/react-router-with-query";
import { DefaultCatchBoundary } from "./components/DefaultCatchBoundary";
import { NotFound } from "./components/NotFound";
import { routeTree } from "./routeTree.gen";

// NOTE: Most of the integration code found here is experimental and will
// definitely end up in a more streamlined API in the future. This is just
// to show what's possible with the current APIs.

export function createRouter() {
const queryClient = new QueryClient();
const persister = createSyncStoragePersister({
storage: window.localStorage,
retry: removeOldestQuery,
});
persistQueryClient({
queryClient,
persister: persister,
});
return routerWithQueryClient(
createTanStackRouter({
routeTree,
context: { queryClient },
defaultPreload: "intent",
defaultErrorComponent: DefaultCatchBoundary,
defaultNotFoundComponent: () => <NotFound />,
}),
queryClient,
);
}

declare module "@tanstack/react-router" {
interface Register {
router: ReturnType<typeof createRouter>;
}
}
import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persister";
import { QueryClient } from "@tanstack/react-query";
import {
persistQueryClient,
removeOldestQuery,
} from "@tanstack/react-query-persist-client";
import { createRouter as createTanStackRouter } from "@tanstack/react-router";
import { routerWithQueryClient } from "@tanstack/react-router-with-query";
import { DefaultCatchBoundary } from "./components/DefaultCatchBoundary";
import { NotFound } from "./components/NotFound";
import { routeTree } from "./routeTree.gen";

// NOTE: Most of the integration code found here is experimental and will
// definitely end up in a more streamlined API in the future. This is just
// to show what's possible with the current APIs.

export function createRouter() {
const queryClient = new QueryClient();
const persister = createSyncStoragePersister({
storage: window.localStorage,
retry: removeOldestQuery,
});
persistQueryClient({
queryClient,
persister: persister,
});
return routerWithQueryClient(
createTanStackRouter({
routeTree,
context: { queryClient },
defaultPreload: "intent",
defaultErrorComponent: DefaultCatchBoundary,
defaultNotFoundComponent: () => <NotFound />,
}),
queryClient,
);
}

declare module "@tanstack/react-router" {
interface Register {
router: ReturnType<typeof createRouter>;
}
}
35 Replies
absent-sapphire
absent-sapphire6mo ago
what's a "global persisting cache" for you? where? on the server, on the client ... ?
fair-rose
fair-roseOP6mo ago
on the server something everyone can access.. but its cached so in the future it an be revalidated or else i could just ssg on a side note.. how do you even persist cache on the server xD... do i need to like roll my own redis and stuff?
absent-sapphire
absent-sapphire6mo ago
you can use redis, yes you would do the same in nextjs unless you deploy on vercel
fair-rose
fair-roseOP6mo ago
not really know.. nextjs by default uses a filebased system the .next folder
absent-sapphire
absent-sapphire6mo ago
at runtime?
fair-rose
fair-roseOP6mo ago
works on any nextjs app
absent-sapphire
absent-sapphire6mo ago
what does it put there
fair-rose
fair-roseOP6mo ago
yep
absent-sapphire
absent-sapphire6mo ago
how does it get there
fair-rose
fair-roseOP6mo ago
all of your cache files, etc even on vercel.. it uses .next
absent-sapphire
absent-sapphire6mo ago
so how do you cache things ? which API
fair-rose
fair-roseOP6mo ago
in next?
absent-sapphire
absent-sapphire6mo ago
yes
fair-rose
fair-roseOP6mo ago
well my golang backends expose dynamic apis.. so no caching on them.. and then in nextjs i either use the fetch cache or unstable_cache the dynamic exports dont work with me well since i like granular caching now with tanstack i honestly just wanted a similar way to put stuff in a file based cache.. globally but idt tanstack start is built for that its more for a user specific dashboard type apps right
absent-sapphire
absent-sapphire6mo ago
isn't the .next folder just used during development?
fair-rose
fair-roseOP6mo ago
nope.. even production :D lemme show
fair-rose
fair-roseOP6mo ago
our company running a nextjs app on a pt panel
No description
fair-rose
fair-roseOP6mo ago
No description
fair-rose
fair-roseOP6mo ago
its not vercel specific
absent-sapphire
absent-sapphire6mo ago
some link to documentation would be nice. also how this works with severless setups, how does this sync across instances? I just found some discussions that state that this folder is only used during dev and can be deleted prior to deployment ... might be wrong then?
fair-rose
fair-roseOP6mo ago
cant seem to find specific documentation on the .next folder but you can ask chatgpt to explain
absent-sapphire
absent-sapphire6mo ago
hm how would chatgpt know if there are no docs?
fair-rose
fair-roseOP6mo ago
cause its a thing lol.. the docs dont specify directly
No description
typical-coral
typical-coral6mo ago
Start has the same limitations as next, excluding vercel specific features. So if you want a cache file, you can create and read and write to a cache file From a server function Or an api route That file may die if you’re using serverless Which would then mean you need something more persistent, like a file storage layer, distributed key value store, etc If you could clarify, what is a feature in next that requires this “global cache”?
fair-rose
fair-roseOP6mo ago
that is expected and fine with me, vercel is a big vendor lockin provider xD ideally i host it on my vps, and in that case i can also selfhost a redis instance i just couldnt find any good examples of a file based/redis setup.. and the stuff i did try didnt seem to work. I went through all of the projects in #start-showcase and the tanstack start examples, nobody seems to be using a server side persister
fair-rose
fair-roseOP6mo ago
this is what i needed the global cache for.. those 3 words, i want it so globally it gets cached and is updated every hour for all the users
No description
absent-sapphire
absent-sapphire6mo ago
Static Server Functions | TanStack Start React Docs
What are Static Server Functions? Static server functions are server functions that are executed at build time and cached as static assets when using prerendering/static-generation. They can be set to...
fair-rose
fair-roseOP6mo ago
its just SSG right? fetch on build and then use cache for future clients i can make a quick testing repo for you guys the feature on nextjs vs my attempt on tanstack i really want to use start for my codebase (i love the dev tools i cannot live without them anymore) lmao
absent-sapphire
absent-sapphire6mo ago
@Tanner Linsley did we ever merge the "dynamic" static server fn? https://x.com/tannerlinsley/status/1864721489309896798?t=GwfJFCBMg7h_teVsnLQFLg&s=19
Tanner Linsley (@tannerlinsley) on X
Alright, this is getting really cool. This opens up a lot of really cool use-cases and mixed static/dynamic patterns for @Tan_Stack Start
From An unknown user
X
fair-rose
fair-roseOP6mo ago
@Manuel Schiller @Tanner Linsley Made a recording for you guys to see what i mean. https://github.com/Arinji2/vocab-thing/tree/tanstack-testing thats the code of the video
GitHub
GitHub - Arinji2/vocab-thing at tanstack-testing
Contribute to Arinji2/vocab-thing development by creating an account on GitHub.
fair-rose
fair-roseOP6mo ago
Also why does adding a persisting setup delete the devtools? I liked them a lot lol
absent-sapphire
absent-sapphire6mo ago
what did delete the devtools?
fair-rose
fair-roseOP6mo ago
Adding the persistQueryClient Soo uhh any ideas @Manuel Schiller
absent-sapphire
absent-sapphire6mo ago
about what?
fair-rose
fair-roseOP6mo ago
doing something like this

Did you find this page helpful?