-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unload event listener to handle clearing cache on signout.
- Loading branch information
Showing
2 changed files
with
29 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,35 @@ | ||
import { Cache, State, useSWRConfig } from 'swr'; | ||
import { Cache, State } from 'swr'; | ||
|
||
export let unloadListener: ((ev: BeforeUnloadEvent) => void) | null = null; | ||
|
||
export function localStorageProvider(): Cache { | ||
// When initializing, we restore the data from `localStorage` into a map. | ||
if (typeof window !== 'undefined') { | ||
const map: Map<string, State> = new Map( | ||
JSON.parse(localStorage.getItem('app-cache') || '[]') | ||
); | ||
|
||
// Before unloading the app, we write back all the data into `localStorage`. | ||
window.addEventListener('beforeunload', () => { | ||
const appCache = JSON.stringify(Array.from(map.entries())); | ||
localStorage.setItem('app-cache', appCache); | ||
}); | ||
// Remove any existing listener | ||
if (unloadListener) { | ||
window.removeEventListener('beforeunload', unloadListener); | ||
} | ||
|
||
unloadListener = () => { | ||
localStorage.setItem('app-cache', JSON.stringify(Array.from(map.entries()))); | ||
}; | ||
window.addEventListener('beforeunload', unloadListener); | ||
|
||
return map; | ||
} | ||
|
||
return new Map(); | ||
} | ||
|
||
export const clearSWRCache = async () => { | ||
const { mutate } = useSWRConfig(); | ||
mutate( | ||
() => true, // clear all keys | ||
undefined, // set to undefined | ||
{ revalidate: false } | ||
); | ||
export const clearSWRCache = () => { | ||
if (typeof window !== 'undefined') { | ||
// Remove the listener before clearing cache | ||
if (unloadListener) { | ||
window.removeEventListener('beforeunload', unloadListener); | ||
unloadListener = null; | ||
} | ||
localStorage.removeItem('app-cache'); | ||
} | ||
}; |