T
TanStack•3mo ago
grumpy-cyan

Invalidate context

Hey guys ! 👋 I think i'm missing something on how to properly invalidate the root context. I created a router with a context like this :
const router = createRouter({
routeTree,
context: {
session: async () => {
const session = await authClient.getSession();
return session;
},
queryClient,
establishmentId: document.cookie
.split("; ")
.find((row) => row.startsWith("establishment_id="))
?.split("=")[1] ?? null
},
defaultPreload: "intent",
scrollRestoration: true,
defaultStructuralSharing: true,
defaultPreloadStaleTime: 0,
});
const router = createRouter({
routeTree,
context: {
session: async () => {
const session = await authClient.getSession();
return session;
},
queryClient,
establishmentId: document.cookie
.split("; ")
.find((row) => row.startsWith("establishment_id="))
?.split("=")[1] ?? null
},
defaultPreload: "intent",
scrollRestoration: true,
defaultStructuralSharing: true,
defaultPreloadStaleTime: 0,
});
So every time i use useRouter().invalidate() i'm expecting the value of establishmentId to be recomputed ? It doesn't seem to be the case because even if i update the cookie, the value does not change. I tried to log the value of establishmentId by using a self called function and it seems to be only called once Am i missing something about context (or maybe it's the cookie usage)
2 Replies
genetic-orange
genetic-orange•3mo ago
you would need to put this into e.g. root's beforeLoad and return it from there the context you pass into createRouter is static
grumpy-cyan
grumpy-cyanOP•3mo ago
Yep it's working just fine like this 🙂 Thank a lot for the quick answer !

Did you find this page helpful?