fix(map): one persistent canvas renderer for critical-news pins

loadNewsPins() called renderer: pointCanvas() INSIDE the per-pin .map(),
giving every critical pin its own full-viewport L.canvas (~19 MB each at
retina 2x). Renderers were never removed on refresh (Leaflet keeps empty
renderers mounted), so canvases grew without bound (35 after 6 refreshes
measured) and stacked pointer-events:auto siblings at z100 meant only the
LAST pin's canvas received clicks — Kyiv (first) was dead, Kherson (last)
worked. This is the user-reported 'selecting Critical News reloads the
site': unbounded full-viewport canvases stall the GPU/main thread.

Fix (mirrors T2's articlesRenderer pattern):
- hoist one persistent L.canvas({padding:0.5}) into module-level
  newsRenderer, shared by all pins and reused across refreshes; L.Canvas
  hit-tests every drawn path so ALL pins are clickable
- remove the renderer on toggle-off (0 canvases when layer off)
- guard L.Canvas.prototype._redraw against detached renderers: Leaflet
  1.9.4's moveend-driven _updatePaths() calls _redraw() directly, nulling
  _redrawRequest without cancelling a pending animation frame; removing
  the renderer before that orphaned frame fires crashes on the deleted
  2d context ('clearRect of undefined'). No-op when detached, unchanged
  when live.

Verified in real browser against local mock of /api/news/map (5 pins) +
/api/news/blips (1398-row T2 fixture):
- all 5 pins open their popups on click, Article Blips ON and OFF
- canvas count flat at 1 across 12 pan/zoom refreshes (was +5/refresh);
  0 when toggled off, 1 when back on
- 6 toggle off/on cycles: every CDP eval 1-2 ms, no navigation/reload
  (boot sentinel + navigation entries unchanged), counts/HUD 0<->5
- 4 toggles mid-map-motion (race stress): zero errors
- article blips still clickable above the canvas (svg z200 pe:none)
- popup content, cyan styling, counts/HUD byte-identical; zero console
  errors across the full session

app/static/index.html only, +19/-2.
This commit is contained in:
Sirius DevOps 2026-11-19 23:29:48 -05:00
commit 577a96658d

View file

@ -2349,7 +2349,7 @@ async function saveSummaryModel() {
let map = null, mapLayer = null, mapInitStarted = false; let map = null, mapLayer = null, mapInitStarted = false;
let firesHeat = null, camsGroup = null, firesOn = false, camsOn = false; let firesHeat = null, camsGroup = null, firesOn = false, camsOn = false;
let blipsGroup = null, blipsOn = false, blipsSince = '24h'; let blipsGroup = null, blipsOn = false, blipsSince = '24h';
let newsGroup = null, newsOn = false, newsReq = 0; let newsGroup = null, newsOn = false, newsReq = 0, newsRenderer = null;
let articlesGroup = null, articlesOn = false, articlesReq = 0, articlesTimer = null, articlesRenderer = null; let articlesGroup = null, articlesOn = false, articlesReq = 0, articlesTimer = null, articlesRenderer = null;
let firesOpacity = 0.65, firesColor = 'brightness', firesSince = '24h'; let firesOpacity = 0.65, firesColor = 'brightness', firesSince = '24h';
let camsOpacity = 1.0, baseOpacity = 1.0; let camsOpacity = 1.0, baseOpacity = 1.0;
@ -2514,6 +2514,17 @@ function overlayFetch(url) {
return fetch(url, overlayAbort ? { signal: overlayAbort.signal } : {}); return fetch(url, overlayAbort ? { signal: overlayAbort.signal } : {});
} }
function isAbort(e) { return e && e.name === 'AbortError'; } function isAbort(e) { return e && e.name === 'AbortError'; }
/* Leaflet 1.9.4 race guard: a moveend-driven _updatePaths() calls _redraw()
directly, which nulls _redrawRequest WITHOUT cancelling an already-pending
animation frame. If the renderer is removed before that orphaned frame
fires (e.g. layer toggle-off), _redraw crashes on the deleted 2d context
("Cannot read properties of undefined (reading 'clearRect')"). No-op
redraws on detached renderers instead; live renderers are unaffected. */
const _canvasRedraw = L.Canvas.prototype._redraw;
L.Canvas.prototype._redraw = function () {
if (!this._ctx) return;
return _canvasRedraw.apply(this, arguments);
};
const pointCanvas = () => L.canvas({ padding: 0.5 }); const pointCanvas = () => L.canvas({ padding: 0.5 });
async function initMap() { async function initMap() {
@ -3281,6 +3292,7 @@ async function toggleNewsPins() {
} }
newsReq++; newsReq++;
if (newsGroup && map) { map.removeLayer(newsGroup); newsGroup = null; } if (newsGroup && map) { map.removeLayer(newsGroup); newsGroup = null; }
if (newsRenderer && map) { map.removeLayer(newsRenderer); newsRenderer = null; }
hudNewsCount = null; hudNewsCount = null;
setLayerCount('lp-news-count', '0'); setLayerCount('lp-news-count', '0');
syncHud(); syncHud();
@ -3296,11 +3308,16 @@ async function loadNewsPins() {
const items = Array.isArray(data) ? data : []; const items = Array.isArray(data) ? data : [];
if (newsGroup) map.removeLayer(newsGroup); if (newsGroup) map.removeLayer(newsGroup);
const col = '#35e0ff'; const col = '#35e0ff';
// One persistent canvas renderer shared by ALL pins: L.Canvas
// hit-tests every drawn path, so each pin stays clickable (a fresh
// canvas per pin stacked empty siblings over all but the last), and
// reusing it across refreshes leaks no full-viewport containers.
const renderer = newsRenderer || (newsRenderer = L.canvas({ padding: 0.5 }));
const pins = items.filter(it => Number.isFinite(Number(it.lat)) && Number.isFinite(Number(it.lon))); const pins = items.filter(it => Number.isFinite(Number(it.lat)) && Number.isFinite(Number(it.lon)));
newsGroup = L.layerGroup(pins.map(it => { newsGroup = L.layerGroup(pins.map(it => {
return L.circleMarker([it.lat, it.lon], { return L.circleMarker([it.lat, it.lon], {
radius: 6, weight: 1, color: col, fillColor: col, fillOpacity: 0.7, radius: 6, weight: 1, color: col, fillColor: col, fillOpacity: 0.7,
renderer: pointCanvas(), renderer,
}) })
.bindPopup(`<div class="blip-pop">` + .bindPopup(`<div class="blip-pop">` +
`<div class="blip-src">◈ ${esc(it.importance || '')}${it.category ? ' · ' + esc(it.category) : ''}</div>` + `<div class="blip-src">◈ ${esc(it.importance || '')}${it.category ? ' · ' + esc(it.category) : ''}</div>` +