mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-22 01:20:12 +01:00
Improve Service Worker (#8089)
* Update service-worker.js * Update serviceWorker.ts * Update serviceWorker.ts
This commit is contained in:
parent
f9d71002fc
commit
99c4f1f890
2 changed files with 16 additions and 11 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
// https://github.com/siyuan-note/siyuan/pull/8012
|
// https://github.com/siyuan-note/siyuan/pull/8012
|
||||||
export const registerServiceWorker = (scriptURL: string) => {
|
export const registerServiceWorker = (scriptURL: string, scope: string = "/", workerType: WorkerType = "module") => {
|
||||||
if (!("serviceWorker" in navigator) || typeof (navigator.serviceWorker) === "undefined" ||
|
if (!("serviceWorker" in navigator) || typeof (navigator.serviceWorker) === "undefined" ||
|
||||||
!("caches" in window) || !("fetch" in window)) {
|
!("caches" in window) || !("fetch" in window)) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -7,8 +7,8 @@ export const registerServiceWorker = (scriptURL: string) => {
|
||||||
// REF https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
|
// REF https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
|
||||||
window.navigator.serviceWorker
|
window.navigator.serviceWorker
|
||||||
.register(scriptURL, {
|
.register(scriptURL, {
|
||||||
scope: "./",
|
scope,
|
||||||
type: "module",
|
type: workerType,
|
||||||
}).then(registration => {
|
}).then(registration => {
|
||||||
registration.update();
|
registration.update();
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,6 @@ const INITIAL_CACHED_RESOURCES = [
|
||||||
"/stage/icon.png",
|
"/stage/icon.png",
|
||||||
"/stage/loading-pure.svg",
|
"/stage/loading-pure.svg",
|
||||||
"/stage/loading.svg",
|
"/stage/loading.svg",
|
||||||
"/stage/manifest.webmanifest",
|
|
||||||
"/stage/service-worker.js",
|
|
||||||
"/stage/build/fonts/JetBrainsMono-Regular.woff",
|
"/stage/build/fonts/JetBrainsMono-Regular.woff",
|
||||||
"/stage/protyle/js/lute/lute.min.js",
|
"/stage/protyle/js/lute/lute.min.js",
|
||||||
"/stage/protyle/js/protyle-html.js"
|
"/stage/protyle/js/protyle-html.js"
|
||||||
|
|
@ -41,19 +39,26 @@ self.addEventListener("fetch", event => {
|
||||||
|
|
||||||
// Don't care about other-origin URLs.
|
// Don't care about other-origin URLs.
|
||||||
if (url.origin !== location.origin &&
|
if (url.origin !== location.origin &&
|
||||||
url.origin !== "https://assets.b3logfile.com") {
|
url.origin !== "https://assets.b3logfile.com"
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't care about anything else than GET.
|
// Don't care about anything else than GET.
|
||||||
if (event.request.method !== 'GET' ||
|
if (event.request.method !== 'GET' ||
|
||||||
event.request.destination === "document") {
|
event.request.destination === "document"
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Don't care about widget requests.
|
|
||||||
if (!url.pathname.startsWith("/stage/") && !url.pathname.startsWith("/appearance/") &&
|
// Don't care about other requests.
|
||||||
"/favicon.ico" !== url.pathname &&
|
if (!url.pathname.startsWith("/stage/") &&
|
||||||
url.href.indexOf("https://assets.b3logfile.com/avatar/") === -1) {
|
!url.pathname.startsWith("/appearance/boot/") &&
|
||||||
|
!url.pathname.startsWith("/appearance/emojis/") &&
|
||||||
|
!url.pathname.startsWith("/appearance/langs/") &&
|
||||||
|
!url.href.startsWith("https://assets.b3logfile.com/avatar/") &&
|
||||||
|
url.pathname !== "/favicon.ico"
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue