1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-05-11 05:55:29 +00:00

Enable contenthash in filename for dynamic assets (#20813)

This should solve the main problem of dynamic assets getting stale after
a version upgrade. Everything not affected will use query-string based
cache busting, which includes files loaded via HTML or worker scripts.
This commit is contained in:
silverwind
2022-08-23 14:58:04 +02:00
committed by GitHub
parent 0a9ed54abb
commit 56220515fc
15 changed files with 1102 additions and 802 deletions

View File

@ -1,8 +1,8 @@
import {joinPaths} from '../utils.js';
import {joinPaths, parseUrl} from '../utils.js';
const {useServiceWorker, assetUrlPrefix, appVer} = window.config;
const {useServiceWorker, assetUrlPrefix, appVer, assetVersionEncoded} = window.config;
const cachePrefix = 'static-cache-v'; // actual version is set in the service worker script
const workerAssetPath = joinPaths(assetUrlPrefix, 'serviceworker.js');
const workerUrl = `${joinPaths(assetUrlPrefix, 'serviceworker.js')}?v=${assetVersionEncoded}`;
async function unregisterAll() {
for (const registration of await navigator.serviceWorker.getRegistrations()) {
@ -12,8 +12,9 @@ async function unregisterAll() {
async function unregisterOtherWorkers() {
for (const registration of await navigator.serviceWorker.getRegistrations()) {
const scriptURL = registration.active?.scriptURL || '';
if (!scriptURL.endsWith(workerAssetPath)) await registration.unregister();
const scriptPath = parseUrl(registration.active?.scriptURL || '').pathname;
const workerPath = parseUrl(workerUrl).pathname;
if (scriptPath !== workerPath) await registration.unregister();
}
}
@ -43,7 +44,7 @@ export default async function initServiceWorker() {
try {
// the spec strictly requires it to be same-origin so the AssetUrlPrefix should contain AppSubUrl
await checkCacheValidity();
await navigator.serviceWorker.register(workerAssetPath);
await navigator.serviceWorker.register(workerUrl);
} catch (err) {
console.error(err);
await invalidateCache();