diff --git a/AGENT_NOTES.md b/AGENT_NOTES.md
index 5739a5f..b42f458 100644
--- a/AGENT_NOTES.md
+++ b/AGENT_NOTES.md
@@ -78,6 +78,13 @@
- Prevents queries like `amp` from breaking `&` entities.
- Entities are matched by regex `&(?:#\d+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);` and skipped during highlighting.
+### 2026-06-30 — Router Event Delegation + Listener Leak Fix
+- Replaced per-page `a.pwa` click interceptors in `WeatherPage.js`, `AboutPage.js`, `ListPage.js`, and `HomePage.js` with a single `document`-level event delegation handler in `Router.js`.
+- `Router.init()` now uses `document.addEventListener("click", ...)` with `event.target.closest("a")` to catch all clicks on `` tags — static and dynamically added.
+- Filters external links (`http://`, `tel:`, `mailto:`, `#`) to avoid intercepting non-app navigation.
+- Removed dead `Router` imports from pages that no longer reference it directly.
+- Also resolves TODO item: "Router not intercepting dynamically added `` tags after init".
+
---
## Key Files
diff --git a/TODO.md b/TODO.md
index 753d5c0..2f20682 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,7 +2,7 @@
## Active (Max 3)
- [x] Fix SearchPopover highlighting corrupting HTML entities (e.g. `&` → broken)
-- [ ] Fix WeatherPage leaking duplicate click listeners on every visit
+- [x] Fix WeatherPage / AboutPage / ListPage / HomePage leaking duplicate click listeners + Router dynamic link interception
- [ ] Fix token/ package log.Fatalf crashing server on invalid JWT (DoS vector)
## Next Up
@@ -13,7 +13,7 @@
## Backlog - Bugs
- [ ] Fix ImportExportService revoking object URL before download starts (mobile)
- [ ] Fix ShareView passing 3 args to DialogService.confirm (3rd arg ignored)
-- [ ] Fix Router not intercepting dynamically added `` tags after init
+- [x] Fix Router not intercepting dynamically added `` tags after init
- [ ] Fix showError() alert-modal missing — error display is unreliable
- [ ] Fix DBManager.getDeletionToken not awaiting cleanup of expired tokens
- [ ] Fix _clearCompleted saving list before archiving items (orphaned items on failure)
diff --git a/public/components/AboutPage.js b/public/components/AboutPage.js
index 8fc3bd2..9b8c65f 100644
--- a/public/components/AboutPage.js
+++ b/public/components/AboutPage.js
@@ -1,19 +1,8 @@
-import { Router } from "../services/Router.js";
-
export class AboutPage extends HTMLElement {
connectedCallback() {
const template = document.getElementById("about-page");
const content = template.content.cloneNode(true);
this.appendChild(content);
-
- // Set up PWA navigation links
- document.querySelectorAll("a.pwa").forEach((a) => {
- a.addEventListener("click", (event) => {
- event.preventDefault();
- const href = a.getAttribute("href");
- Router.go(href);
- });
- });
}
}
diff --git a/public/components/HomePage.js b/public/components/HomePage.js
index fee1685..d31d8ad 100644
--- a/public/components/HomePage.js
+++ b/public/components/HomePage.js
@@ -1,6 +1,5 @@
import { getDefaultLayout, getWidgetById } from "./WidgetRegistry.js";
import { DBManager } from "../services/DBManager.js";
-import { Router } from "../services/Router.js";
import './weather/WeatherCurrent.js';
import './List.js';
import './ListWidget.js';
@@ -13,14 +12,12 @@ export class HomePage extends HTMLElement {
this.appendChild(content);
this.#renderHomepage();
- this.#attachPwaLinks();
this._onLayoutChange = async () => {
this.innerHTML = '';
const fresh = template.content.cloneNode(true);
this.appendChild(fresh);
await this.#renderHomepage();
- this.#attachPwaLinks();
};
window.addEventListener('homepage-layout-changed', this._onLayoutChange);
}
@@ -80,15 +77,6 @@ export class HomePage extends HTMLElement {
}
}
- #attachPwaLinks() {
- this.querySelectorAll("a.pwa").forEach(a => {
- a.addEventListener("click", event => {
- event.preventDefault();
- const href = a.getAttribute("href");
- Router.go(href);
- });
- });
- }
}
customElements.define("home-page", HomePage);
diff --git a/public/components/ListPage.js b/public/components/ListPage.js
index a4c8c3e..7d435f8 100644
--- a/public/components/ListPage.js
+++ b/public/components/ListPage.js
@@ -1,4 +1,3 @@
-import { Router } from "../services/Router.js";
import "./List.js";
export class ListPage extends HTMLElement {
@@ -6,14 +5,6 @@ export class ListPage extends HTMLElement {
const template = document.getElementById("pockist-list-page");
const content = template.content.cloneNode(true);
this.appendChild(content);
-
- document.querySelectorAll("a.pwa").forEach((a) => {
- a.addEventListener("click", (event) => {
- event.preventDefault();
- const href = a.getAttribute("href");
- Router.go(href);
- });
- });
}
}
diff --git a/public/components/WeatherPage.js b/public/components/WeatherPage.js
index 367e88a..952a709 100644
--- a/public/components/WeatherPage.js
+++ b/public/components/WeatherPage.js
@@ -1,4 +1,3 @@
-import { Router } from "../services/Router.js";
import { weatherService } from "../services/WeatherService.js";
// Import weather components
@@ -15,23 +14,12 @@ export class WeatherPage extends HTMLElement {
}
connectedCallback() {
- // Add the weather class to this component for CSS styling
this.classList.add('weather');
const template = document.getElementById("weather-page");
const content = template.content.cloneNode(true);
this.appendChild(content);
- // Set up PWA navigation links
- document.querySelectorAll("a.pwa").forEach(a=>{
- a.addEventListener("click", event => {
- event.preventDefault();
- const href = a.getAttribute("href");
- Router.go(href);
- })
- });
-
- // Initialize weather functionality
this.initializeWeatherComponent();
}
diff --git a/public/services/Router.js b/public/services/Router.js
index dfde817..6e6722b 100644
--- a/public/services/Router.js
+++ b/public/services/Router.js
@@ -5,13 +5,17 @@ export const Router = {
window.addEventListener("popstate",()=>{
Router.go(location.pathname, false); // pathname is the path portion of url
})
- // enhance current links
- document.querySelectorAll("a").forEach(a=>{
- a.addEventListener("click", event => {
- event.preventDefault();
- const href = a.getAttribute("href");
- Router.go(href);
- })
+ // Intercept all internal link clicks via event delegation
+ // This catches dynamically added tags without per-page listener leaks.
+ document.addEventListener("click", (event) => {
+ const a = event.target.closest("a");
+ if (!a) return;
+
+ const href = a.getAttribute("href");
+ if (!href || href.startsWith("http") || href.startsWith("tel:") || href.startsWith("mailto:") || href.startsWith("#")) return;
+
+ event.preventDefault();
+ Router.go(href);
});
// go to the initial route
Router.go(location.pathname + location.search) // search is the query ?