Skip to content

Commit

Permalink
checking the pwa installation capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Gokul K authored and Gokul K committed Aug 24, 2024
1 parent abe9fe3 commit 922810d
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
16 changes: 16 additions & 0 deletions esbuild-helper/copy-file-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from "fs";

const copyFilePlugin = (fileSrcPath, fileDestPAth) => ({
name: "copy-file-plugin",
setup(build) {
build.onEnd(async () => {
try {
fs.copyFileSync(fileSrcPath, fileDestPAth);
} catch (e) {
console.error(`Failed to copy file: ${fileSrcPath}`, e);
}
});
},
});

export default copyFilePlugin;
3 changes: 3 additions & 0 deletions esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as esbuild from "esbuild";
import copyStaticFiles from "esbuild-copy-static-files";
import * as fsExtra from "fs-extra";
import { sentryEsbuildPlugin } from "@sentry/esbuild-plugin";
import copyFilePlugin from "./esbuild-helper/copy-file-plugin.js";

fsExtra.emptyDirSync("./dist");

Expand All @@ -13,6 +14,8 @@ esbuild.build({
sourcemap: true, // Source map needed for sentry to work
mainFields: ["module", "main"],
plugins: [
copyFilePlugin("./manifest.json", "./dist/manifest.json"),
copyFilePlugin("./sw.js", "./dist/sw.js"),
copyStaticFiles({
src: "./css",
dest: "dist/css",
Expand Down
1 change: 1 addition & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
type="image/png"
href="./assets/images/favicon/favicon-48.png"
/>
<link rel="manifest" href="manifest.json">
</head>
<body>
<h1 class="no-show">
Expand Down
23 changes: 23 additions & 0 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,28 @@ export async function initSentry() {
});
}

export function registerSW() {
try {
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker
.register("../sw.js")
.then((registration) => {
console.log(
"Service Worker registered with scope:",
registration.scope
);
})
.catch((error) => {
console.error("Service Worker registration failed:", error);
});
});
}
} catch (error) {
console.error("Service Worker registration failed:", error);
}
}

export async function init() {
initSentry();
setupDocument();
Expand All @@ -770,4 +792,5 @@ export async function init() {
setupListeners();
evaluate(editor.innerText);
updateOutputDisplay(output);
registerSW();
}
31 changes: 31 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "Type to Calculate",
"short_name": "Type to Calculate",

"icons": [
{
"src": "/assets/images/favicon/favicon-16.png",
"sizes": "16x16",
"type": "image/png"
},
{
"src": "/assets/images/favicon/favicon-32.png",
"sizes": "32x32",
"type": "image/png"
},
{
"src": "/assets/images/favicon/favicon-48.png",
"sizes": "48x48",
"type": "image/png"
},
{
"src": "/assets/images/favicon/favicon-150.png",
"sizes": "150x150",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#191919",
"background_color": "#7f7f7f"
}
28 changes: 28 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// sw.js
const CACHE_NAME = "ttc-cache-v1";
const urlsToCache = [
"/",
"/index.html",
"css/*.css",
"js/editor.js",
"/favicon-150.png",
"/favicon-48.png",
];

// Install the service worker
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(urlsToCache);
})
);
});

// Fetch the cached assets
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});

0 comments on commit 922810d

Please sign in to comment.