DogStark / DogStark/Wordbloc

🐛 Service worker is broken: localStorage in SW scope throws, cache-first serves stale app forever, precache list incomplete

Open
#3 2 comments 0 reactions 1 assignee Claimed by @Lspnjr1 View on GitHub
bug frontend GrantFox OSS Maybe Rewarded Official Campaign | FWC26
Dominant language
JavaScript
Stars
1
Forks
12
Avg merge
2d 7h
Merged PRs (30d)
4

Description

## Summary
The PWA service worker (`sw.js`) has three real problems: it calls `localStorage` inside the service worker scope (which throws), its cache-first strategy means deployed updates never reach returning users, and its precache list is missing most of the app.

## Bugs

**1. `localStorage` does not exist in a service worker (`sw.js:77-83`)**
```js
async function getPendingAnalytics() {
return JSON.parse(localStorage.getItem('spellbloc_pending_analytics') || '[]');
}
```
Service workers have no `localStorage` — this throws `ReferenceError: localStorage is not defined` whenever the `analytics-sync` background sync fires (`sw.js:49-53`). The sync silently fails forever. These functions must use IndexedDB (the comment even says "IndexedDB operations" — the implementation just never happened).

**2. Cache-first with a static cache name = permanently stale app (`sw.js:23-31`)**
```js
return response || fetch(event.request);
```
Every cached file is served from cache unconditionally, and `CACHE_NAME` is hardcoded to `spellbloc-v1.0.0` (`sw.js:2`). Unless someone remembers to bump the version string on every deploy, users keep running old `game.js`/`styles.css` forever. Suggested: stale-while-revalidate (or network-first for HTML), plus `self.skipWaiting()` / `clients.claim()` for clean activation.

**3. Precache list doesn't match the app (`sw.js:3-10`)**
Cached: `/`, `/index.html`, `/styles.css`, `/game.js`, `/advanced-systems.js`, `/manifest.json`.
Not cached but required: `game.html`, `login.html`, `auth-manager.js`, `ai-agent.js`, `email-integration.js`, `minipay-integration.js`, etc. Offline mode therefore breaks as soon as the user navigates past the landing page. Also, `cache.addAll()` rejects entirely if *any* single URL fails, which disables precaching wholesale.

**Bonus:** the push notification handler (`sw.js:86-109`) references `/icon-192x192.png`, `/badge-72x72.png`, `/play-icon.png`, `/dismiss-icon.png` — none of these files exist in the repo (icons were removed in commit `773a807`).

## Acceptance criteria
- [ ] Background sync uses IndexedDB and successfully posts queued analytics
- [ ] Deploying a change to `game.js` reaches users on next load without manually bumping a version string
- [ ] Full game flow (login → game) works offline after first visit
- [ ] No references to non-existent icon assets

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.