diff --git a/build/esbuild-build.ts b/build/esbuild-build.ts index 704f317a..6b86d80f 100644 --- a/build/esbuild-build.ts +++ b/build/esbuild-build.ts @@ -7,7 +7,7 @@ const typescriptEntries = [ // "src/authenticated/authenticated.ts" ]; const cssEntries = ["static/style/style.css"]; -const entries = [...typescriptEntries, ...cssEntries]; +const entries = [...typescriptEntries, ...cssEntries, "static/manifest.json", "static/favicon.svg"]; export const esBuildContext: esbuild.BuildOptions = { define: createEnvDefines(["SUPABASE_URL", "SUPABASE_KEY"]), diff --git a/src/home/home.ts b/src/home/home.ts index ade1f52b..39992531 100644 --- a/src/home/home.ts +++ b/src/home/home.ts @@ -1,3 +1,5 @@ +import { pwa } from "../progressive-web-app"; +pwa(); import { grid } from "../the-grid"; import { authentication } from "./authentication"; import { fetchAndDisplayPreviewsFromCache } from "./fetch-github/fetch-and-display-previews"; diff --git a/src/progressive-web-app.ts b/src/progressive-web-app.ts new file mode 100644 index 00000000..c31af61b --- /dev/null +++ b/src/progressive-web-app.ts @@ -0,0 +1,30 @@ +export function pwa() { + self.addEventListener("install", (event: InstallEvent) => { + event.waitUntil( + caches.open("v1").then((cache) => { + return cache.addAll(["/", "/dist/src/home/home.js", "/style/style.css", "/style/inverted-style.css", "/favicon.svg"]); + }) + ); + }); + + self.addEventListener("fetch", (event: FetchEvent) => { + event.respondWith( + caches.match(event.request).then((response) => { + return response || fetch(event.request); + }) + ); + }); + + if ("serviceWorker" in navigator) { + window.addEventListener("load", () => { + navigator.serviceWorker.register("/sw.js").then( + (registration) => { + console.log("ServiceWorker registration successful with scope: ", registration.scope); + }, + (err) => { + console.log("ServiceWorker registration failed: ", err); + } + ); + }); + } +} diff --git a/static/index.html b/static/index.html index 043cc64f..3549c26e 100644 --- a/static/index.html +++ b/static/index.html @@ -1,6 +1,7 @@
+