Skip to content

Commit

Permalink
Merge pull request #441 from Decatur-Robotics/pwa
Browse files Browse the repository at this point in the history
PWA
  • Loading branch information
renatodellosso authored Feb 10, 2025
2 parents cd45525 + 93209d4 commit 8b60e7c
Show file tree
Hide file tree
Showing 10 changed files with 577 additions and 2,934 deletions.
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

/public/sw.js
/public/sw.js.map
/public/workbox-*
/public/fallback-*
/certs/*.*

/certs/*.*
# PWA
public/sw.js
public/swe-worker*
10 changes: 0 additions & 10 deletions components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ export default function Footer() {
? `SW Status: ${registration.active?.state}`
: "Service worker not found",
);
console.log("Service worker registration: ", registration);
if (registration) {
registration.addEventListener("updatefound", () => {
setSwStatus("Service worker update found");
console.log("Service worker update found");
registration.installing?.addEventListener("statechange", () => {
console.log(
"Service worker state change: ",
Expand All @@ -41,14 +39,6 @@ export default function Footer() {

registration.active?.addEventListener("statechange", () => {
setSwStatus(`SW Status: ${registration.active?.state}`);
console.log(
"Service worker state change: ",
registration.active?.state,
);
});

registration.update().then(() => {
console.log("Service worker update initiated");
});
}
});
Expand Down
29 changes: 29 additions & 0 deletions lib/sw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defaultCache } from "@serwist/next/worker";
import type { PrecacheEntry, SerwistGlobalConfig } from "serwist";
import { Serwist } from "serwist";

declare global {
interface WorkerGlobalScope extends SerwistGlobalConfig {
__SW_MANIFEST: (PrecacheEntry | string)[];
}
}

declare const self: ServiceWorkerGlobalScope;

const serwist = new Serwist({
precacheEntries: self.__SW_MANIFEST,
skipWaiting: true,
clientsClaim: true,
navigationPreload: true,
runtimeCaching: defaultCache,
fallbacks: {
entries: [
{
url: "/offline",
matcher: ({ request }) => request.mode === "navigate",
},
],
},
});

serwist.addEventListeners();
13 changes: 11 additions & 2 deletions next.config.js → next.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
const packageConfig = require("./package.json");
import packageConfig from "./package.json";
import withSerwistInit from "@serwist/next";

const withSerwist = withSerwistInit({
swSrc: "lib/sw.ts",
swDest: "public/sw.js",
additionalPrecacheEntries: [
{ url: "/offline", revision: new Date().toDateString() },
],
});

/** @type {import('next').NextConfig} */
const nextConfig = {
Expand All @@ -23,4 +32,4 @@ const nextConfig = {
},
};

module.exports = nextConfig;
module.exports = withSerwist(nextConfig);
Loading

0 comments on commit 8b60e7c

Please sign in to comment.