-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* swr local storage cache * IndexedDB SWR cache with invalidation. * Fix build. * Refactor. * Update providers.tsx * Use localstorage cache. * Remove dep * Update pnpm-lock.yaml * Adds unload event listener to handle clearing cache on signout. --------- Co-authored-by: hyusap <[email protected]>
- Loading branch information
Showing
5 changed files
with
88 additions
and
36 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
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Cache, State } from 'swr'; | ||
|
||
export let unloadListener: ((ev: BeforeUnloadEvent) => void) | null = null; | ||
|
||
export function localStorageProvider(): Cache { | ||
if (typeof window !== 'undefined') { | ||
const map: Map<string, State> = new Map( | ||
JSON.parse(localStorage.getItem('app-cache') || '[]') | ||
); | ||
|
||
// 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 = () => { | ||
if (typeof window !== 'undefined') { | ||
// Remove the listener before clearing cache | ||
if (unloadListener) { | ||
window.removeEventListener('beforeunload', unloadListener); | ||
unloadListener = null; | ||
} | ||
localStorage.removeItem('app-cache'); | ||
} | ||
}; |