Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checking the pwa installation capabilities #34

Merged
merged 6 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => ({
gokulk16 marked this conversation as resolved.
Show resolved Hide resolved
name: "copy-file-plugin",
setup(build) {
build.onEnd(async () => {
gokulk16 marked this conversation as resolved.
Show resolved Hide resolved
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() {
gokulk16 marked this conversation as resolved.
Show resolved Hide resolved
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",
gokulk16 marked this conversation as resolved.
Show resolved Hide resolved
"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);
})
);
});
Loading