Replies: 1 comment
-
|
The fundamental issue is that Until your PR or an official API lands, here is a workaround using the global import { mutate } from "swr";
function onOrgSwitch(newOrgId: string) {
// Clear all cached data to prevent cross-org leaks
mutate(
(key) => true, // matches all keys
undefined, // set data to undefined
{ revalidate: false }
);
// Re-run preloads for the new org
preloadForOrg(newOrgId);
}This nukes the entire SWR cache (both regular and preloaded), which is the safest approach for an org switch since you genuinely want all stale data gone. If you want to be more surgical and only clear specific keys: function onOrgSwitch(newOrgId: string) {
const keysToInvalidate = ["/api/dashboard", "/api/settings", "/api/members"];
keysToInvalidate.forEach((key) => {
mutate(key, undefined, { revalidate: false });
});
preloadForOrg(newOrgId);
}That said, including the org ID in your preload keys is still the more robust long-term pattern, because it makes cache collisions impossible by design rather than relying on manual cleanup. But your point about wanting explicit cache clearing for preloaded-but-never-subscribed keys is valid, and I think the PR is a reasonable addition. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Due to the current use case that I have right now, useSWR are loaded dynamically, but we do have a static list of preloads that load everytime my user opens up the website. The issue I'm facing right now is that when the user opens the website, then move to other organization (without loading the page that calls the useSWR). This meant that the user are shown wrong data on the other organization due to it having the same keys.
I know that I could just add the id of the organization on the preload keys. But I would rather know that all the data from the other orgs are not cached via SWR. So I propose a way to clear out the preload cache for those use case where the preload data are not loaded to the normal cache via useSWR.
While referencing the other issue related to this (preload not inside cache), I've also have made a simple implementation in this PR #4194
Beta Was this translation helpful? Give feedback.
All reactions