-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move serviceworker to workbox and fix SSE interference (#11538)
* Move serviceworker to workbox and fix SSE interference Instead of statically hardcoding every frontend asset, this uses a type-based approach to cache all js,css and manifest.json requests. This also fixes the issue that the service worker was interfering with EventSource because it was unconditionally handling all requests which this new implementation doesn't. Fixes: #11092 Fixes: #7372 * rethrow error instead of logging * await .register * Revert "rethrow error instead of logging" This reverts commit 043162b. * improve comment * remove JSRenderer * add version-based cache invalidation * refactor * more refactor * remove comment * rename item to fit cache name Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
- Loading branch information
1 parent
f6f4970
commit 88fe7b5
Showing
12 changed files
with
96 additions
and
133 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,43 @@ | ||
const {UseServiceWorker, AppSubUrl, AppVer} = window.config; | ||
const cacheName = 'static-cache-v2'; | ||
|
||
async function unregister() { | ||
for (const registration of await navigator.serviceWorker.getRegistrations()) { | ||
const serviceWorker = registration.active; | ||
if (!serviceWorker) continue; | ||
registration.unregister(); | ||
} | ||
} | ||
|
||
async function invalidateCache() { | ||
await caches.delete(cacheName); | ||
} | ||
|
||
async function checkCacheValidity() { | ||
const cacheKey = AppVer; | ||
const storedCacheKey = localStorage.getItem('staticCacheKey'); | ||
|
||
// invalidate cache if it belongs to a different gitea version | ||
if (cacheKey && storedCacheKey !== cacheKey) { | ||
invalidateCache(); | ||
localStorage.setItem('staticCacheKey', cacheKey); | ||
} | ||
} | ||
|
||
export default async function initServiceWorker() { | ||
if (!('serviceWorker' in navigator)) return; | ||
|
||
if (UseServiceWorker) { | ||
await checkCacheValidity(); | ||
try { | ||
await navigator.serviceWorker.register(`${AppSubUrl}/serviceworker.js`); | ||
} catch (err) { | ||
console.error(err); | ||
await invalidateCache(); | ||
await unregister(); | ||
} | ||
} else { | ||
await invalidateCache(); | ||
await unregister(); | ||
} | ||
} |
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,16 @@ | ||
import {registerRoute} from 'workbox-routing'; | ||
import {StaleWhileRevalidate} from 'workbox-strategies'; | ||
|
||
const cacheName = 'static-cache-v2'; | ||
|
||
const cachedDestinations = new Set([ | ||
'manifest', | ||
'script', | ||
'style', | ||
'worker', | ||
]); | ||
|
||
registerRoute( | ||
({request}) => cachedDestinations.has(request.destination), | ||
new StaleWhileRevalidate({cacheName}), | ||
); |
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