From 577a96658dc42de26516f851e7b51fa235827346 Mon Sep 17 00:00:00 2001 From: Sirius DevOps Date: Thu, 19 Nov 2026 23:29:48 -0500 Subject: [PATCH] fix(map): one persistent canvas renderer for critical-news pins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/static/index.html | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/static/index.html b/app/static/index.html index 0a359a6..3b9b430 100644 --- a/app/static/index.html +++ b/app/static/index.html @@ -2349,7 +2349,7 @@ async function saveSummaryModel() { let map = null, mapLayer = null, mapInitStarted = false; let firesHeat = null, camsGroup = null, firesOn = false, camsOn = false; 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 firesOpacity = 0.65, firesColor = 'brightness', firesSince = '24h'; let camsOpacity = 1.0, baseOpacity = 1.0; @@ -2514,6 +2514,17 @@ function overlayFetch(url) { return fetch(url, overlayAbort ? { signal: overlayAbort.signal } : {}); } 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 }); async function initMap() { @@ -3281,6 +3292,7 @@ async function toggleNewsPins() { } newsReq++; if (newsGroup && map) { map.removeLayer(newsGroup); newsGroup = null; } + if (newsRenderer && map) { map.removeLayer(newsRenderer); newsRenderer = null; } hudNewsCount = null; setLayerCount('lp-news-count', '0'); syncHud(); @@ -3296,11 +3308,16 @@ async function loadNewsPins() { const items = Array.isArray(data) ? data : []; if (newsGroup) map.removeLayer(newsGroup); 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))); newsGroup = L.layerGroup(pins.map(it => { return L.circleMarker([it.lat, it.lon], { radius: 6, weight: 1, color: col, fillColor: col, fillOpacity: 0.7, - renderer: pointCanvas(), + renderer, }) .bindPopup(`
` + `
◈ ${esc(it.importance || '')}${it.category ? ' · ' + esc(it.category) : ''}
` + -- 2.52.0