Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(pwa): only the latest badge recount applies; MES cache writes never fail a request
naveenkashb1d9934898ad
diff --git a/apps/erp/app/hooks/useAppBadge.ts b/apps/erp/app/hooks/useAppBadge.tsindex e7e9b89c37..aad721e419 100644--- a/apps/erp/app/hooks/useAppBadge.ts+++ b/apps/erp/app/hooks/useAppBadge.ts@@ -30,9 +30,14 @@ export function useAppBadge({ userId }: { userId: string }) {[shell?.companies]);const timer = useRef<ReturnType<typeof setTimeout> | null>(null);+ // Only the latest recount may set the badge: an older query that finishes+ // last would restore a stale count, and one still running at unmount (sign+ // out) would undo the login page's clear.+ const latest = useRef(0);const recount = useCallback(async () => {if (!carbon || !companyIds) return;+ const run = ++latest.current;const { count, error } = await carbon.from("notification").select("id", { count: "exact", head: true })@@ -40,6 +45,7 @@ export function useAppBadge({ userId }: { userId: string }) {.in("companyId", companyIds.split(",")).is("readAt", null).is("digestedInto", null);+ if (run !== latest.current) return;if (error) {logger.warn("Failed to count unread notifications for the badge", {error@@ -53,6 +59,7 @@ export function useAppBadge({ userId }: { userId: string }) {recount();return () => {if (timer.current) clearTimeout(timer.current);+ latest.current++;};}, [recount]);diff --git a/apps/mes/public/service-worker.js b/apps/mes/public/service-worker.jsindex fe0bdf4d6a..b780950a76 100644--- a/apps/mes/public/service-worker.js+++ b/apps/mes/public/service-worker.js@@ -73,23 +73,43 @@ async function trimAssets(cache) {}}-async function cacheFirst(request) {- const cache = await caches.open(ASSET_CACHE);- const cached = await cache.match(request);- if (cached) return cached;- const response = await fetch(request);- if (response.ok) {- await cache.put(request, response.clone());- await trimAssets(cache);+// The cache only ever speeds a response up: a cache that cannot be opened,+// or a write that fails (storage full, or a 206 partial response, which+// cache.put refuses), still serves the file from the network.+async function openAssetCache() {+ try {+ return await caches.open(ASSET_CACHE);+ } catch {+ return null;}+}++// Saves a complete response in the background; the response itself is+// returned without waiting for the write.+function saveAsset(event, cache, response, { trim = false } = {}) {+ if (!cache || response.status !== 200) return;+ event.waitUntil(+ cache+ .put(event.request, response.clone())+ .then(() => (trim ? trimAssets(cache) : undefined))+ .catch(() => undefined)+ );+}++async function cacheFirst(event) {+ const cache = await openAssetCache();+ const cached = await cache?.match(event.request).catch(() => undefined);+ if (cached) return cached;+ const response = await fetch(event.request);+ saveAsset(event, cache, response, { trim: true });return response;}async function staleWhileRevalidate(event) {- const cache = await caches.open(ASSET_CACHE);- const cached = await cache.match(event.request);- const refresh = fetch(event.request).then(async (response) => {- if (response.ok) await cache.put(event.request, response.clone());+ const cache = await openAssetCache();+ const cached = await cache?.match(event.request).catch(() => undefined);+ const refresh = fetch(event.request).then((response) => {+ saveAsset(event, cache, response);return response;});if (cached) {@@ -151,7 +171,7 @@ self.addEventListener("fetch", (event) => {if (request.mode === "navigate") {event.respondWith(navigate(event));} else if (isHashedAsset(url)) {- event.respondWith(cacheFirst(request));+ event.respondWith(cacheFirst(event));} else if (isPublicFile(url)) {event.respondWith(staleWhileRevalidate(event));}