-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4481 from janhq/chore/engines-data-cache
chore: add caching for engines to improve load time
- Loading branch information
Showing
5 changed files
with
56 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use client' | ||
|
||
import * as React from 'react' | ||
|
||
import { SWRConfig } from 'swr' | ||
|
||
function SWRConfigProvider({ children }: { children: React.ReactNode }) { | ||
// https://swr.vercel.app/docs/advanced/cache#localstorage-based-persistent-cache | ||
// When initializing, we restore the data from `localStorage` into a map. | ||
|
||
const map = React.useMemo(() => new Map<string, object>(), []) | ||
React.useEffect(() => { | ||
const savedCache = JSON.parse( | ||
window.localStorage.getItem('app-cache') || '[]' | ||
) | ||
savedCache.forEach(([key, value]: [string, object]) => { | ||
map.set(key, value) | ||
}) | ||
|
||
// Before unloading the app, we write back all the data into `localStorage`. | ||
window.addEventListener('beforeunload', () => { | ||
const appCache = JSON.stringify(Array.from(map.entries())) | ||
window.localStorage.setItem('app-cache', appCache) | ||
}) | ||
}, [map]) | ||
|
||
return <SWRConfig value={{ provider: () => map }}>{children}</SWRConfig> | ||
} | ||
|
||
export default SWRConfigProvider |
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