-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
perf: Add config option enableResourceCache to cache dashboard resources locally for faster loading in additional browser tabs
#2920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
755d423
fix: dashboard reloads when opening in new tab
mtrezza 63bb63a
feat: optionally cache dashboard resources
mtrezza 00b5368
fix: service worker persists after closing tabs
mtrezza f219c37
fix: prevent stale tab count from blocking sw unregister
mtrezza 63f04df
fix
mtrezza 0c306b1
Update README.md
mtrezza cb54720
rename config
mtrezza df47ab7
Update README.md
mtrezza e1a8e66
fixes
mtrezza 14dc9e7
fix
mtrezza bfba1a8
Merge branch 'alpha' into codex/optimize-dashboard-resource-loading
mtrezza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| const CACHE_NAME = 'dashboard-cache-v1'; | ||
|
|
||
| self.addEventListener('install', () => { | ||
| self.skipWaiting(); | ||
| }); | ||
|
|
||
| self.addEventListener('activate', event => { | ||
| event.waitUntil( | ||
| Promise.all([ | ||
| self.clients.claim(), | ||
| caches.keys().then(cacheNames => { | ||
| return Promise.all( | ||
| cacheNames.map(cacheName => { | ||
| if (cacheName !== CACHE_NAME) { | ||
| return caches.delete(cacheName); | ||
| } | ||
| }) | ||
| ); | ||
| }) | ||
| ]) | ||
| ); | ||
| }); | ||
|
|
||
| self.addEventListener('fetch', event => { | ||
| const req = event.request; | ||
| if (req.destination === 'script' || req.destination === 'style' || req.url.includes('/bundles/')) { | ||
| event.respondWith( | ||
| caches.match(req).then(cached => { | ||
| return ( | ||
| cached || | ||
| fetch(req).then(resp => { | ||
| const resClone = resp.clone(); | ||
| caches.open(CACHE_NAME).then(cache => cache.put(req, resClone)); | ||
| return resp; | ||
| }) | ||
|
mtrezza marked this conversation as resolved.
|
||
| ); | ||
| }) | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| self.addEventListener('message', event => { | ||
| if (event.data === 'unregister') { | ||
| self.registration.unregister(); | ||
| } | ||
| }); | ||
This file contains hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| function registerServiceWorker() { | ||
|
|
||
| if (!window.PARSE_DASHBOARD_ENABLE_RESOURCE_CACHE) { | ||
| return; | ||
| } | ||
|
|
||
| if (!('serviceWorker' in navigator)) { | ||
| return; | ||
| } | ||
|
|
||
| const mountPath = (window.PARSE_DASHBOARD_PATH || '/').replace(/\/?$/, '/'); | ||
| const swPath = `${mountPath}sw.js`; | ||
| const countKey = `pd-sw-tabs:${mountPath}`; | ||
|
|
||
| /** | ||
| * Registers the service worker at the specified path. | ||
| */ | ||
| const register = () => { | ||
| navigator.serviceWorker.register(swPath).catch(() => {}); | ||
| }; | ||
|
|
||
| /** | ||
| * Increments the count of open dashboard tabs in localStorage. | ||
| */ | ||
| const increment = () => { | ||
| let current = parseInt(localStorage.getItem(countKey) || '0', 10); | ||
| if (!navigator.serviceWorker.controller && current > 0) { | ||
| current = 0; | ||
| } | ||
| localStorage.setItem(countKey, String(current + 1)); | ||
| }; | ||
|
|
||
| /** | ||
| * Decrements the count of open dashboard tabs in localStorage. | ||
| */ | ||
| const decrement = () => { | ||
| const current = parseInt(localStorage.getItem(countKey) || '0', 10); | ||
| const next = Math.max(0, current - 1); | ||
| localStorage.setItem(countKey, String(next)); | ||
| if (next === 0) { | ||
| if (navigator.serviceWorker.controller) { | ||
| navigator.serviceWorker.controller.postMessage('unregister'); | ||
| } | ||
| navigator.serviceWorker.getRegistration(swPath).then(reg => { | ||
| if (reg) { | ||
| reg.unregister(); | ||
| } | ||
| }); | ||
| caches.keys().then(keys => keys.forEach(k => caches.delete(k))); | ||
| } | ||
| }; | ||
|
|
||
| increment(); | ||
|
|
||
| window.addEventListener('load', () => { | ||
| register(); | ||
| }); | ||
| window.addEventListener('beforeunload', () => { | ||
| decrement(); | ||
| }); | ||
| window.addEventListener('pagehide', () => { | ||
| decrement(); | ||
| }); | ||
| } | ||
|
|
||
| export default registerServiceWorker; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.