Skip to content

Commit

Permalink
checking the pwa installation capabilities (#34)
Browse files Browse the repository at this point in the history
* checking the pwa installation capabilities

* fixing PR review comments

* added better and bigger icon images

* adding image files to cahce; loading pwacompact in async way

* deferring the pwacompact to render the DOM first

* more clean code

---------

Co-authored-by: Gokul K <gokulk@Gokuls-MacBook-Air.local>
  • Loading branch information
gokulk16 and Gokul K authored Sep 22, 2024
1 parent 57138f9 commit 8918dc5
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
Binary file added assets/images/favicon/favicon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/favicon/favicon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
7 changes: 7 additions & 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 Expand Up @@ -107,5 +108,11 @@ <h2>
</section>
</div>
</div>
<script
defer
src="https://cdn.jsdelivr.net/npm/pwacompat/pwacompat.min.js"
integrity="sha256-QmifG9ty2co3761lBWJwL5KdDOdJ4sFjQ/ULE4aD18U="
crossorigin="anonymous"
></script>
</body>
</html>
21 changes: 21 additions & 0 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,28 @@ export async function initSentry() {
});
}

export async function registerSW() {
try {
if (!("serviceWorker" in navigator)) {
console.log("Service Workers are not supported by this browser");
return;
}

window.addEventListener("load", () => {
navigator.serviceWorker
.register("../sw.js")
.then((registration) => {})
.catch((error) => {
console.error("Service Worker registration failed:", error);
});
});
} catch (error) {
console.error("Service Worker registration failed:", error);
}
}

export async function init() {
registerSW();
initSentry();
setupDocument();
await loadSettings();
Expand Down
41 changes: 41 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"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"
},
{
"src": "/assets/images/favicon/favicon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/assets/images/favicon/favicon-512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#191919",
"background_color": "#7f7f7f"
}
30 changes: 30 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// sw.js
const CACHE_NAME = "ttc-cache-v1";
const urlsToCache = [
"/",
"/index.html",
"/css/*.css",
"/js/editor.js",
"/favicon-150.png",
"/favicon-48.png",
"/favicon-512.png",
"/favicon-192.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 8918dc5

Please sign in to comment.