Files
mc-webui/app/static/js/sw.js
T
MarekWoandClaude Opus 5 8f1334ee00 fix(map): reject impossible advert coordinates before they blank the map
A tester's cached-contacts map came up empty and Path Analyzer drew routes
across the globe. Two of his 2131 cached contacts carried garbage positions
from corrupted adverts - lat 1642 / lon -323 and lat 309 / lon -1768.

Leaflet clamps latitude when projecting but not longitude, so fitBounds()
spanned 1797 degrees, dropped to zoom 0 and centred on lon -870; all 2273
real markers were rendered several world-widths outside the viewport.
Reproduced against his DB: zoom 0, bounds -1431..-309. The device-only view
was fine because both bad rows are cache-only.

Four layers, so no single miss can blank a map again:

- app/geo.py: one definition of a usable coordinate, applied on write in
  Database.upsert_contact() so the cache stops storing garbage, and on API
  output so a cache poisoned by an older build heals itself with no
  migration. Corrupted advert types (61, 126, 152, 210 seen in the wild)
  are logged but kept raw - they already render as UNKNOWN.
- static/js/geo-utils.js: hasValidGps()/withValidGps()/fitMapToPoints()
  replace eight copies of the same check across six files. Path Analyzer
  had a looser variant that also accepted lon == 0, which put nodes off
  west Africa and stretched routes across the map; it now shares the rule.
- Per-marker try/catch and a bounds validity check, so an entry that still
  slips through costs its own pin rather than the whole map.

Verified against the tester's DB in a container: cached map 2273 markers,
lon span 1797 -> 19 degrees, centred on Poland; the route that drew the
world-spanning line (msg 29499, token 72AF) now renders as a real
Krakow-Kielce path with the bad hop left unresolved. Live local instance
regression-checked on all five map pages. tests/test_geo.py: 43 passed;
full suite 290 passed, 2 pre-existing failures unrelated to this change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 11:35:53 +02:00

89 lines
3.3 KiB
JavaScript

const CACHE_NAME = 'mc-webui-v11';
const ASSETS_TO_CACHE = [
'/',
'/static/css/style.css',
'/static/js/app.js',
'/static/js/dm.js',
'/static/js/contacts.js',
'/static/js/message-utils.js',
'/static/js/filter-utils.js',
'/static/js/geo-utils.js',
'/static/js/i18n-runtime.js',
'/static/js/datetime-utils.js',
'/static/js/console.js',
'/static/images/android-chrome-192x192.png',
'/static/images/android-chrome-512x512.png',
// Bootstrap 5.3.2 (local)
'/static/vendor/bootstrap/css/bootstrap.min.css',
'/static/vendor/bootstrap/js/bootstrap.bundle.min.js',
// Bootstrap Icons 1.11.2 (local)
'/static/vendor/bootstrap-icons/bootstrap-icons.css',
'/static/vendor/bootstrap-icons/fonts/bootstrap-icons.woff2',
'/static/vendor/bootstrap-icons/fonts/bootstrap-icons.woff',
// Emoji Picker Element 1.28.1 (local)
'/static/vendor/emoji-picker-element/index.js',
'/static/vendor/emoji-picker-element/picker.js',
'/static/vendor/emoji-picker-element/database.js',
'/static/vendor/emoji-picker-element-data/en/emojibase/data.json',
// Socket.IO client 4.x (local)
'/static/vendor/socket.io/socket.io.min.js',
// Console page
'/console'
];
// Install event - cache core assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(ASSETS_TO_CACHE))
.then(() => self.skipWaiting())
);
});
// Activate event - clean old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
);
}).then(() => self.clients.claim())
);
});
// Fetch event - hybrid strategy:
// - Cache-first for vendor libraries (static, unchanging)
// - Cache-first for translation catalogs (URL is content-hashed, so it cannot go stale)
// - Network-first for app content (dynamic, needs updates)
//
// Note: /i18n/<lang>.<hash>.js is deliberately NOT in ASSETS_TO_CACHE — the hashed
// filename isn't known when this file is written. It gets cached on first fetch instead.
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Cache-first for vendor libraries (Bootstrap, Icons) and translation catalogs
if (url.pathname.includes('/static/vendor/') || url.pathname.startsWith('/i18n/')) {
event.respondWith(
caches.match(event.request)
.then((cachedResponse) => {
return cachedResponse || fetch(event.request)
.then((response) => {
// Cache the fetched vendor file for future use
return caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, response.clone());
return response;
});
});
})
);
} else {
// Network-first for everything else (app code, API calls, dynamic content)
event.respondWith(
fetch(event.request)
.catch(() => caches.match(event.request))
);
}
});