Skip to content

Commit

Permalink
Revalidate only when orgs change (#6409)
Browse files Browse the repository at this point in the history
* revalidate only when orgs change

* remove hasRun cache

* use hash value to revalidate on login
  • Loading branch information
gatzjames authored Aug 29, 2023
1 parent a9f32d9 commit 00ef2e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
7 changes: 7 additions & 0 deletions packages/insomnia/src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ const router = createMemoryRouter(
id: 'root',
loader: async (...args) =>
(await import('./routes/root')).loader(...args),
shouldRevalidate: ({
currentParams,
nextParams,
nextUrl,
}) => {
return currentParams.organizationId !== nextParams.organizationId || nextUrl.hash === '#revalidate=true';
},
element: <Root />,
errorElement: <ErrorRoute />,
children: [
Expand Down
23 changes: 12 additions & 11 deletions packages/insomnia/src/ui/routes/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import {
NavLink,
Outlet,
useLoaderData,
useLocation,
useNavigate,
useParams,
useRevalidator,
useRouteLoaderData,
} from 'react-router-dom';

Expand Down Expand Up @@ -75,20 +76,15 @@ export interface RootLoaderData {
organizations: Organization[];
settings: Settings;
}
// NOTE: when getVCS is rewritten to be less spaghetti
// this can be removed, and get team should only run once,
// at app start and whenever the user logs in
// This "workaround" will not work if a user logs out and back in again
let hasRun = false;

export const loader: LoaderFunction = async (): Promise<RootLoaderData> => {
// Load all projects
try {
const vcs = getVCS();
if (vcs && isLoggedIn() && !hasRun) {
if (vcs && isLoggedIn()) {
const teams = await vcs.teams();
const projects = await Promise.all(teams.map(initializeProjectFromTeam));
await database.batchModifyDocs({ upsert: projects });
hasRun = true;
}
} catch {
console.log('Failed to load projects');
Expand Down Expand Up @@ -130,7 +126,8 @@ const getNameInitials = (name: string) => {
};

const Root = () => {
const { revalidate } = useRevalidator();
const navigate = useNavigate();
const location = useLocation();
const { organizations, settings } = useLoaderData() as RootLoaderData;
const workspaceData = useRouteLoaderData(
':workspaceId'
Expand All @@ -140,9 +137,13 @@ const Root = () => {

useEffect(() => {
onLoginLogout(() => {
revalidate();
// Update the hash of the current route to force revalidation of data
navigate({
pathname: location.pathname,
hash: 'revalidate=true',
});
});
}, [revalidate]);
}, [location.pathname, navigate]);

useEffect(() => {
return window.main.on(
Expand Down

0 comments on commit 00ef2e8

Please sign in to comment.