From 92a094bfe437fda1299a8748e46f43b00b0f78ae Mon Sep 17 00:00:00 2001 From: Dwayne Charrington Date: Sun, 22 Dec 2024 12:53:39 +1000 Subject: [PATCH] fix(site): use service worker to fix embed headers for stackblitz --- themes/aurelia-theme/assets/js/coi-serviceworker.min.js | 2 ++ themes/aurelia-theme/assets/js/main.js | 2 -- themes/aurelia-theme/layouts/partials/head/js.html | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 themes/aurelia-theme/assets/js/coi-serviceworker.min.js diff --git a/themes/aurelia-theme/assets/js/coi-serviceworker.min.js b/themes/aurelia-theme/assets/js/coi-serviceworker.min.js new file mode 100644 index 0000000..d39e88d --- /dev/null +++ b/themes/aurelia-theme/assets/js/coi-serviceworker.min.js @@ -0,0 +1,2 @@ +/*! coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */ +"undefined"==typeof window?(self.addEventListener("install",(()=>self.skipWaiting())),self.addEventListener("activate",(e=>e.waitUntil(self.clients.claim()))),self.addEventListener("message",(e=>{e.data&&"deregister"===e.data.type&&self.registration.unregister().then((()=>self.clients.matchAll())).then((e=>{e.forEach((e=>e.navigate(e.url)))}))})),self.addEventListener("fetch",(function(e){"only-if-cached"===e.request.cache&&"same-origin"!==e.request.mode||e.respondWith(fetch(e.request).then((e=>{if(0===e.status)return e;const r=new Headers(e.headers);return r.set("Cross-Origin-Embedder-Policy","require-corp"),r.set("Cross-Origin-Opener-Policy","same-origin"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:r})})).catch((e=>console.error(e))))}))):(()=>{const e={shouldRegister:()=>!0,shouldDeregister:()=>!1,doReload:()=>window.location.reload(),quiet:!1,...window.coi},r=navigator;e.shouldDeregister()&&r.serviceWorker&&r.serviceWorker.controller&&r.serviceWorker.controller.postMessage({type:"deregister"}),!1===window.crossOriginIsolated&&e.shouldRegister()&&(window.isSecureContext?r.serviceWorker&&r.serviceWorker.register(window.document.currentScript.src).then((t=>{!e.quiet&&console.log("COOP/COEP Service Worker registered",t.scope),t.addEventListener("updatefound",(()=>{!e.quiet&&console.log("Reloading page to make use of updated COOP/COEP Service Worker."),e.doReload()})),t.active&&!r.serviceWorker.controller&&(!e.quiet&&console.log("Reloading page to make use of COOP/COEP Service Worker."),e.doReload())}),(r=>{!e.quiet&&console.error("COOP/COEP Service Worker failed to register:",r)})):!e.quiet&&console.log("COOP/COEP Service Worker not registered, a secure context is required."))})(); diff --git a/themes/aurelia-theme/assets/js/main.js b/themes/aurelia-theme/assets/js/main.js index 8b40419..66b0619 100644 --- a/themes/aurelia-theme/assets/js/main.js +++ b/themes/aurelia-theme/assets/js/main.js @@ -1,5 +1,3 @@ -console.log('This site was generated by Hugo.'); - document.addEventListener('DOMContentLoaded', () => { // Mobile menu functionality const mobileMenuButton = document.getElementById('mobile-menu-button'); diff --git a/themes/aurelia-theme/layouts/partials/head/js.html b/themes/aurelia-theme/layouts/partials/head/js.html index 18fe842..b826693 100644 --- a/themes/aurelia-theme/layouts/partials/head/js.html +++ b/themes/aurelia-theme/layouts/partials/head/js.html @@ -10,3 +10,7 @@ {{- end }} {{- end }} {{- end }} + +{{- with resources.Get "js/coi-serviceworker.min.js" }} + +{{- end }}