Skip to Content
Effetch Query
The cache

Sign out and switch accounts

Clear one user's cached data on sign-out or an account switch.

A cache must not outlive the account that filled it: after a sign-out, the next person at the screen shouldn't see the last one's todos. There are two ways to drop it: make a client per account, or keep one client and clear it. This page shows both, and when each is enough.

A session can end without your sign-out button. The user signs out in another tab, a refresh token is refused, or a 401 signs them out. Another account can sign in from another tab, and this tab follows it. Your button's click handler runs in none of these, so nothing clears the cache: the tab refetches the old account's queries with no session, or shows them to the new account.

The examples import api.ts from Getting started.

Make a client per account

Give QueryProvider a make instead of a client, and the account's ID as resetKey. It makes a client for each account, and disposes the one before. It follows the account, not the click, so it handles every way a session ends or changes:

import {  } from "@monarcode/effetch-query";
import {  } from "@monarcode/effetch-query/react";
import type {  } from "react";

import {  } from "./api.js";

// The signed-in account's ID, or "signed-out". It changes on every sign-in,
// sign-out and switch, in every tab.
declare function (): string;

export function ({  }: { readonly :  }) {
  const  = ();
  return (
    <
      ={() => .({ :  })}
      ={}
    >
      {}
    </>
  );
}

With make and resetKey:

  • Nothing of the old account shows. When resetKey changes, the tree renders with the new client from that very render, and fetches afresh. The old client is disposed: its cache goes, and its fetches and mutation runs are interrupted.
  • There's one client per resetKey. A new make function on each render doesn't make a new client, so an inline arrow is fine. If React renders without committing, as StrictMode's second render or an abandoned transition does, the extra client it made is disposed.
  • Put it above every Suspense boundary. A first render that React throws away before showing it leaves its client undisposed.

With client, the provider never disposes the client: it's yours. A provider can switch between the two forms, and the client it owned is disposed when a client replaces it. The props are an OwnedQueryProviderProps with make, and a QueryProviderProps with client.

Keep one client and clear it

If only your own sign-out button can end the session, clearing is simpler. client.clear() drops every value without fetching, so the screens still showing as you sign out don't refetch with no session and fail with a 401:

import {  } from "@monarcode/effetch-query/react";

declare function (): Promise<void>;

export function () {
  const  = ();
  return (
    <
      ={async () => {
        await ();
        await .();
      }}
    >
      Sign out
    </>
  );
}

From Effect, Query.clear([]) does the same. After a clear:

  • Queries in use show Initial until something asks for them: a component that starts using one, client.fetch, invalidate or refetch. A component already showing one keeps showing Initial, or its Suspense fallback. So after the next sign-in, navigate to a screen that mounts its queries, or call queryClient.invalidate(), so the screens ask again.
  • What depends on a cleared query goes too. A query that reads a cleared one through ctx.result is cleared as well.
  • Nothing in progress brings it back. A client.fetch waiting on a cleared query is interrupted, and a mutation run in progress doesn't roll its optimistic writes back over the cleared values.
  • Values from the server go too. A query no one has read since the server's values were loaded into the client fetches the next time it's used.

client.reset() drops the values too, but fetches those in use again at once.

A mutation that settles after you clear can refetch with no session. invalidate asks for the queries it matches, so it fetches cleared ones again. If every mutation invalidates the whole cache (mutations: { onSuccess: () => Query.invalidate([]) }), one that settles after signing out fetches the queries in use with no session. A client per account doesn't: disposing it interrupts its mutation runs.

What's next

On this page