Skip to content

lightweight, client-only calendar web application. All data persists in the URL hash for instant sharing, No backend required. Optional AES-GCM password protection keeps shared links locked without a server

JavaScript

update

Supun Lakmal63ef6e149bfe
diff --git a/Caddyfile b/Caddyfile
index d766e5b..921d929 100644
--- a/Caddyfile
+++ b/Caddyfile
@@ -7,11 +7,22 @@
# Compress responses
encode gzip
- # Set appropriate cache headers for static assets
- @static {
- path *.css *.js *.json *.png *.jpg *.jpeg *.gif *.svg *.ico *.woff *.woff2 *.ttf *.eot
+ # Long cache for fingerprintable static assets only.
+ @static_immutable {
+ path *.png *.jpg *.jpeg *.gif *.svg *.ico *.woff *.woff2 *.ttf *.eot
}
- header @static Cache-Control "public, max-age=31536000, immutable"
+ header @static_immutable Cache-Control "public, max-age=31536000, immutable"
+
+ # App code must be revalidated to avoid stale JS/CSS/JSON after deploys.
+ @app_code {
+ path *.js *.css *.json
+ not path /sw.js
+ }
+ header @app_code Cache-Control "public, max-age=0, must-revalidate"
+
+ # Always fetch the latest service worker script.
+ @service_worker path /sw.js
+ header @service_worker Cache-Control "no-cache, no-store, must-revalidate"
# Dynamic content (HTML) - don't cache
@html {
diff --git a/netlify.toml b/netlify.toml
index 04c96a5..70f5249 100644
--- a/netlify.toml
+++ b/netlify.toml
@@ -1,5 +1,30 @@
[build]
functions = "netlify/functions"
-[functions]
- node_bundler = "esbuild"
+[functions]
+ node_bundler = "esbuild"
+
+[[headers]]
+ for = "/sw.js"
+ [headers.values]
+ Cache-Control = "no-cache, no-store, must-revalidate"
+
+[[headers]]
+ for = "/*.js"
+ [headers.values]
+ Cache-Control = "public, max-age=0, must-revalidate"
+
+[[headers]]
+ for = "/*.css"
+ [headers.values]
+ Cache-Control = "public, max-age=0, must-revalidate"
+
+[[headers]]
+ for = "/*.json"
+ [headers.values]
+ Cache-Control = "public, max-age=0, must-revalidate"
+
+[[headers]]
+ for = "/modules/*"
+ [headers.values]
+ Cache-Control = "public, max-age=0, must-revalidate"
diff --git a/script.js b/script.js
index 2bca273..a8b49e6 100644
--- a/script.js
+++ b/script.js
@@ -2785,10 +2785,12 @@ document.addEventListener("click", (e) => {
/* --- PWA Service Worker Registration --- */
if ("serviceWorker" in navigator) {
window.addEventListener("load", () => {
+ const serviceWorkerUrl = "./sw.js?v=6";
navigator.serviceWorker
- .register("./sw.js")
+ .register(serviceWorkerUrl)
.then((registration) => {
console.log("ServiceWorker registration successful with scope: ", registration.scope);
+ registration.update();
})
.catch((err) => {
console.log("ServiceWorker registration failed: ", err);
diff --git a/sw.js b/sw.js
index 167fb42..2d083ad 100644
--- a/sw.js
+++ b/sw.js
@@ -1,85 +1,104 @@
-const CACHE_NAME = 'hash-calendar-v5';
-const ASSETS_TO_CACHE = [
- './',
- './index.html',
- './json.html',
- './styles.css',
- './script.js',
- './manifest.json',
- './favicon.ico',
- './favicon.png',
- './apple-touch-icon.png',
- './logo.png',
- './modules/lz-string.min.js',
- './modules/app_launcher.js',
- // Modules
- './modules/agendaRender.js',
- './modules/calendarRender.js',
- './modules/countdownManager.js',
- './modules/focusMode.js',
- './modules/hashcalUrlManager.js',
- './modules/icsImporter.js',
- './modules/modalManager.js',
- './modules/pathImportManager.js',
- './modules/qrCodeManager.js',
- './modules/recurrenceEngine.js',
- './modules/stateSaveManager.js',
- './modules/timezoneManager.js',
- // External resources (Font Awesome, Fonts)
- 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css',
- 'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js',
- 'https://fonts.googleapis.com/css2?family=Fraunces:wght@400;600;700&family=Space+Grotesk:wght@400;500;600&display=swap'
+const CACHE_NAME = "hash-calendar-v6";
+const PRECACHE_ASSETS = [
+ "./",
+ "./index.html",
+ "./json.html",
+ "./styles.css",
+ "./script.js",
+ "./manifest.json",
+ "./favicon.ico",
+ "./favicon.png",
+ "./apple-touch-icon.png",
+ "./logo.png",
+ "./modules/lz-string.min.js",
];
-self.addEventListener('install', (event) => {
- event.waitUntil(
- caches.open(CACHE_NAME).then((cache) => {
- return cache.addAll(ASSETS_TO_CACHE);
+function isSameOrigin(requestUrl) {
+ return new URL(requestUrl).origin === self.location.origin;
+}
+
+function isAppCodePath(pathname) {
+ return pathname === "/" || pathname.endsWith(".html") || pathname.endsWith(".js") || pathname.endsWith(".css") || pathname.endsWith(".json");
+}
+
+async function putInCache(cache, request, response) {
+ if (!response || response.status !== 200 || response.type === "error") return;
+ await cache.put(request, response.clone());
+}
+
+async function networkFirst(request) {
+ const cache = await caches.open(CACHE_NAME);
+ try {
+ const response = await fetch(request, { cache: "no-store" });
+ await putInCache(cache, request, response);
+ return response;
+ } catch (_error) {
+ const cached = await cache.match(request);
+ if (cached) return cached;
+ if (request.mode === "navigate") {
+ const fallback = await cache.match("./index.html");
+ if (fallback) return fallback;
+ }
+ throw _error;
+ }
+}
+
+async function staleWhileRevalidate(request) {
+ const cache = await caches.open(CACHE_NAME);
+ const cached = await cache.match(request);
+
+ const networkFetch = fetch(request)
+ .then(async (response) => {
+ await putInCache(cache, request, response);
+ return response;
})
+ .catch(() => null);
+
+ if (cached) {
+ void networkFetch;
+ return cached;
+ }
+
+ const network = await networkFetch;
+ if (network) return network;
+ return fetch(request);
+}
+
+self.addEventListener("install", (event) => {
+ event.waitUntil(
+ caches
+ .open(CACHE_NAME)
+ .then((cache) => cache.addAll(PRECACHE_ASSETS))
+ .then(() => self.skipWaiting()),
);
});
-self.addEventListener('activate', (event) => {
+self.addEventListener("activate", (event) => {
event.waitUntil(
- caches.keys().then((cacheNames) => {
- return Promise.all(
- cacheNames.map((cacheName) => {
- if (cacheName !== CACHE_NAME) {
- return caches.delete(cacheName);
- }
- })
- );
- })
+ caches
+ .keys()
+ .then((cacheNames) => Promise.all(cacheNames.map((cacheName) => (cacheName === CACHE_NAME ? null : caches.delete(cacheName)))))
+ .then(() => self.clients.claim()),
);
});
-self.addEventListener('fetch', (event) => {
- event.respondWith(
- caches.match(event.request).then((response) => {
- // Return cached response if found
- if (response) {
- return response;
- }
-
- // Clone request for fetch
- const fetchRequest = event.request.clone();
+self.addEventListener("message", (event) => {
+ if (event.data && event.data.type === "SKIP_WAITING") {
+ self.skipWaiting();
+ }
+});
- return fetch(fetchRequest).then((response) => {
- // Check if valid response
- if (!response || response.status !== 200 || response.type !== 'basic') {
- return response;
- }
+self.addEventListener("fetch", (event) => {
+ const { request } = event;
+ if (request.method !== "GET") return;
+ if (request.cache === "only-if-cached" && request.mode !== "same-origin") return;
+ if (!isSameOrigin(request.url)) return;
- // Cache the new resource (if it's local)
- if (event.request.url.startsWith(self.location.origin)) {
- const responseToCache = response.clone();
- caches.open(CACHE_NAME).then((cache) => {
- cache.put(event.request, responseToCache);
- });
- }
+ const url = new URL(request.url);
+ if (request.mode === "navigate" || isAppCodePath(url.pathname)) {
+ event.respondWith(networkFirst(request));
+ return;
+ }
- return response;
- });
- })
- );
+ event.respondWith(staleWhileRevalidate(request));
});