From 9f8e16095acb9a06bf4d026dbf695ac6bfc4f147 Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Mon, 11 Nov 2024 14:04:37 -0500 Subject: [PATCH] Add service worker --- _layouts/default.html | 26 ++++++++++++++++++++++++-- sw.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 sw.js diff --git a/_layouts/default.html b/_layouts/default.html index bf837c7313..561cc87c51 100644 --- a/_layouts/default.html +++ b/_layouts/default.html @@ -6,7 +6,7 @@ - + @@ -28,7 +28,7 @@ {% else %} Godot Engine {% endif %} - + @@ -37,6 +37,28 @@ + + diff --git a/sw.js b/sw.js new file mode 100644 index 0000000000..e7ee34153c --- /dev/null +++ b/sw.js @@ -0,0 +1,36 @@ +importScripts( + "https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js" +); + +/** @type {(options: { url: URL, request: Request, event: Event }) => boolean} */ +const matchCb = ({url, request, event}) => { + if (url.origin !== "https://adamscott.github.io") { + return false; + } + return url.pathname.startsWith("/") && !url.pathname.startsWith("/godot-website"); +}; + +/** @type {(options: { url: URL, request: Request, event: Event, params: string[] }) => Promise} */ +const handlerCb = async ({url, request, event, params}) => { + const newRequest = new Request( + url.origin + "/godot-website" + url.pathname, + { + body: request.body, + cache: request.cache, + credentials: request.credentials, + headers: request.headers, + integrity: request.integrity, + keepalive: request.keepalive, + method: request.method, + mode: request.mode, + redirect: request.redirect, + referrer: request.referrer, + referrerPolicy: request.referrerPolicy, + signal: request.signal, + } + ); + const response = await fetch(newRequest, { cache: "no-cache" }); + return response; +}; + +workbox.routing.registerRoute(matchCb, handlerCb);