-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache video.js in background and require it only where needed
- Loading branch information
1 parent
9c79f55
commit 582246f
Showing
7 changed files
with
105 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"use strict"; | ||
|
||
const version = "20241231"; | ||
const staticCacheName = "static-" + version; | ||
const cacheList = [staticCacheName]; | ||
|
||
async function updateStaticCache() { | ||
let staticCache = await caches.open(staticCacheName); | ||
staticCache.addAll([ | ||
"/assets/video.js", | ||
"/assets/app.css", | ||
"/assets/app.js", | ||
"/images/signee.png", | ||
"/images/pfeil.png", | ||
"/images/avatar.jpg", | ||
"/font/noway-regular-webfont.woff", | ||
"/font/noway-regular-webfont.woff2", | ||
"/font/Virgil.woff2", | ||
]); | ||
} | ||
|
||
async function clearOldCaches() { | ||
let keys = await caches.keys(); | ||
return Promise.all( | ||
keys | ||
.filter((key) => !cacheList.includes(key)) | ||
.map((key) => caches.delete(key)), | ||
); | ||
} | ||
|
||
addEventListener("install", (installEvent) => { | ||
installEvent.waitUntil(updateStaticCache()); | ||
skipWaiting(); | ||
}); | ||
|
||
addEventListener("activate", (activateEvent) => { | ||
activateEvent.waitUntil(clearOldCaches()); | ||
clients.claim(); | ||
}); | ||
|
||
addEventListener("fetch", (fetchEvent) => { | ||
let request = fetchEvent.request; | ||
let url = new URL(request.url); | ||
|
||
// Only deal with requests to my own server | ||
if (url.origin !== location.origin) { | ||
return; | ||
} | ||
|
||
// Only deal with GET requests | ||
if (request.method !== "GET") { | ||
return; | ||
} | ||
|
||
// For HTML requests, try the preload first, then network, fall back to the cache, finally the offline page | ||
if ( | ||
request.mode === "navigate" || | ||
request.headers.get("Accept").includes("text/html") | ||
) { | ||
return; | ||
} | ||
|
||
// For non-HTML requests, look in the cache first, fall back to the network | ||
fetchEvent.respondWith( | ||
caches.match(request).then((responseFromCache) => { | ||
return responseFromCache || fetch(request); | ||
}), | ||
); | ||
}); |
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