Skip to content

Commit

Permalink
Add service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
adamscott committed Nov 29, 2024
1 parent e048736 commit 9e040de
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
26 changes: 24 additions & 2 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Godot Engine">
<meta name="description" content="{% if page.description %}{{ page.description }}{% elsif page.excerpt %}{{ page.excerpt }}{% else %}Develop your 2D & 3D games, cross-platform projects, or even XR ideas!{% endif %}">

<!-- Social meta tags -->
<meta property="og:site_name" content="Godot Engine">
<meta property="og:url" content="https://godotengine.org{{ page.url }}">
Expand All @@ -28,7 +28,7 @@
{% else %}
<title>Godot Engine</title>
{% endif %}

<link rel="alternate" type="application/rss+xml" title="Godot News" href="/rss.xml">
<link rel="icon" href="/assets/favicon.png" sizes="any">
<link rel="icon" href="/assets/favicon.svg" type="image/svg+xml">
Expand All @@ -37,6 +37,28 @@
<link rel="preload" as="font" href="/assets/fonts/Montserrat-Italic-VariableFont_wght.woff2" crossorigin>
<link rel="preload" as="font" href="/assets/fonts/Montserrat-VariableFont_wght.woff2" crossorigin>
<link rel="me" href="https://mastodon.gamedev.place/@godotengine">

<script>
const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register("/godot-website/sw.js", {
scope: "/godot-website/",
});
if (registration.installing) {
console.log("Service worker installing");
} else if (registration.waiting) {
console.log("Service worker installed");
} else if (registration.active) {
console.log("Service worker active");
}
} catch (error) {
console.error(`Registration failed with ${error}`);
}
}
};
registerServiceWorker();
</script>
</head>

<body>
Expand Down
36 changes: 36 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -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<Response>} */
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);

0 comments on commit 9e040de

Please sign in to comment.