Skip to content

Instantly share code, notes, and snippets.

@TCotton
Created May 17, 2021 10:55
Show Gist options
  • Save TCotton/b3043a768ee7ef12cc4e1cd9e7b30385 to your computer and use it in GitHub Desktop.
Save TCotton/b3043a768ee7ef12cc4e1cd9e7b30385 to your computer and use it in GitHub Desktop.
Stale data invalidation
const swr = new Map;
const useSWR = (path, fetcher, cache) => {
let [data, update] = useState(null);
if (!swr.has(path) || swr.get(path) !== cache) {
fetcher(path).then(update, () => update(new Error(path)));
swr.set(path, cache);
}
const isError = data instanceof Error;
return {
data: isError ? null : data,
error: isError ? data : null
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment