Merge pull request 'fix(perf): BIT-28145 follow-up — wire sanitizer tests, defer non-critical scripts' (#48) from fix/BIT-28145-tbt-followup into main
Tests / writer-pipeline tests (push) Successful in 43s
Tests / bridge tests (push) Successful in 43s
Tests / frontend lib tests (push) Successful in 42s

This commit was merged in pull request #48.
This commit is contained in:
spark committed 2026-08-07 10:33:25 +01:00
commit 71e7697c29
7 files changed
+82 -42

No files matched your search

+2 -2
View File
@@ -9,8 +9,8 @@
"build": "tsc",
"start": "node dist/server.js",
"typecheck": "tsc --noEmit",
"test": "npm run build && node --test dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js",
"test:coverage": "npm run build && node --experimental-test-coverage --test dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js"
"test": "npm run build && node --test dist/lib/sanitize-html.test.js dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js",
"test:coverage": "npm run build && node --experimental-test-coverage --test dist/lib/sanitize-html.test.js dist/services/ranking.test.js dist/services/aggregator-mapper.test.js dist/services/andeboltv.test.js dist/services/fpa-stream-match.test.js"
},
"dependencies": {
"@fastify/cors": "^9.0.1",
+15 -6
View File
@@ -18,6 +18,7 @@ type SlugParams = {
type RelatedQuery = {
limit?: string;
category_id?: string;
};
async function resolve_category_id(
@@ -92,6 +93,8 @@ export async function articles_routes(app: FastifyInstance): Promise<void> {
);
// GET /articles/:slug/related — related articles (same category, excluding current)
// If ?category_id= is provided, skip the article fetch and use it directly.
// This avoids a redundant WP API call when the caller already has the article.
app.get(
'/articles/:slug/related',
async (req: FastifyRequest<{ Params: SlugParams; Querystring: RelatedQuery }>, reply) => {
@@ -101,16 +104,22 @@ export async function articles_routes(app: FastifyInstance): Promise<void> {
const { slug } = req.params;
const limit = Math.min(parseInt(req.query.limit ?? '4', 10), 10);
// Fetch the article first to get its category
const article = await swr_fetch(
`${host}:wp:post:${slug}`,
() => client.get_post_by_slug(slug),
);
let category_id: number;
if (req.query.category_id) {
category_id = parseInt(req.query.category_id, 10);
} else {
const article = await swr_fetch(
`${host}:wp:post:${slug}`,
() => client.get_post_by_slug(slug),
);
category_id = article.category.id;
}
const related = await swr_fetch<ArticleListItem[]>(
`${host}:wp:related:${slug}:${limit}`,
async () => {
const posts = await client.get_posts_by_category_id(article.category.id, limit + 1);
const posts = await client.get_posts_by_category_id(category_id, limit + 1);
return posts.filter(p => p.slug !== slug).slice(0, limit);
},
);
+27
View File
@@ -126,6 +126,33 @@ The article page (`/article/[slug]`) uses `set:html={sanitizeArticleHtml(article
- Lighthouse Performance score should exceed 90 on article pages.
- LCP remains unaffected (hero images already use `loading="eager"`).
### Deploy & Re-Verification — 2026-08-07
**Auditor:** Miguel Teixeira (Database Engineer Agent)
Fix merged to `main` via PR [#47](https://git.bitreport.pt/BitReport.pt/news-platform/pulls/47) on 2026-08-05 (3 approvals, CI green) and confirmed live on `newsproject.bitreport.pt`. An earlier deploy-verification confirmation was rejected on 2026-08-05 with reason "`https://newsproject.bitreport.pt/article/` 404" — that URL has no slug and is not a valid route; a real article URL returns `200`.
Lighthouse CLI 13.4.0 (Chrome for Testing 151.0.7922.71, headless, `--no-sandbox`), 5 runs against `newsproject.bitreport.pt/article/benfica-confirma-contratacao-do-guarda-redes-sueco-tobias-thulin`, plus a same-session home-page control (methodology per [[lighthouse-measurement-host-notes]] — host runs persistent background load, so multiple samples + a control page are required rather than trusting one run):
| Run | Performance | TBT | LCP |
|---|---|---|---|
| 1 | 86 | 348ms | 2769ms |
| 2 | 99 | 0ms | 1894ms |
| 3 | 99 | 0ms | 1886ms |
| 4 | 99 | 0ms | 1910ms |
| 5 | 99 | 0ms | 2051ms |
| Home (control) | 83 | 0ms | 4669ms |
Median article Performance 99 (target ≥90), 4/5 runs ≥90; the one 86 run and the control's 83 both track back to LCP variance from host contention (TBT is 0ms across every run), not the TBT fix. **TBT regression is resolved**: 7416ms → 0-348ms.
Follow-up hardening committed under `fix/BIT-28145-tbt-followup` to widen the pass margin and close a test-coverage gap found during verification:
- `bridge/package.json` `test`/`test:coverage` scripts did not actually run `sanitize-html.test.ts` (added in the original fix commit but never wired into the npm script) — the "28 bridge sanitizer tests pass" claim in PR #47 wasn't exercised by `npm test`. Fixed.
- Deferred non-critical mobile-menu and share-button click handlers to `requestIdleCallback` (institutional/sports headers, `ArticleShareBar`) so they no longer compete with hydration-critical work.
- Split the theme-flash-prevention script in `Base.astro` into a minimal inline `is:inline` snippet (runs before paint) and the click-handler logic (deferred), rather than one blocking script.
- `bridge/src/routes/articles.ts` `/articles/:slug/related` accepts an optional `?category_id=` to skip a redundant WP post-by-slug fetch when the caller already has it.
Acceptance criteria for BIT-28145 are met: root cause documented, fix implemented and deployed, Lighthouse Performance ≥90 confirmed on live article pages (median 99).
---
## Root Cause, Fix & Deploy — BIT-28144 — andebol.net LCP ~8s / Performance ~26-38
@@ -61,17 +61,19 @@ const encoded_title = encodeURIComponent(title);
</div>
<script>
document.querySelectorAll('[data-share="copy"]').forEach(btn => {
btn.addEventListener('click', async () => {
const url = btn.getAttribute('data-url');
if (!url) return;
try {
await navigator.clipboard.writeText(url);
btn.classList.add('article-share__btn--copied');
setTimeout(() => btn.classList.remove('article-share__btn--copied'), 2000);
} catch {
/* clipboard unavailable */
}
(typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : setTimeout)(() => {
document.querySelectorAll('[data-share="copy"]').forEach(btn => {
btn.addEventListener('click', async () => {
const url = btn.getAttribute('data-url');
if (!url) return;
try {
await navigator.clipboard.writeText(url);
btn.classList.add('article-share__btn--copied');
setTimeout(() => btn.classList.remove('article-share__btn--copied'), 2000);
} catch {
/* clipboard unavailable */
}
});
});
});
</script>
+5 -7
View File
@@ -12,7 +12,7 @@ interface Props {
og_image?: string | null;
og_type?: string;
canonical?: string;
schema?: object;
schema?: object | object[];
}
const {
@@ -92,13 +92,11 @@ const { Header, Footer } = getThemeComponents(config.theme);
</main>
<Footer config={config} />
<!-- Theme toggle script — no flash -->
<!-- Theme toggle — critical: set theme immediately to prevent flash -->
<script is:inline>
(function(){var s=localStorage.getItem("theme");var p=window.matchMedia("(prefers-color-scheme:dark)").matches?"dark":"light";document.documentElement.setAttribute("data-theme",s||p)})();
</script>
<script>
const saved = localStorage.getItem('theme');
const preferred = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const theme = saved ?? preferred;
document.documentElement.setAttribute('data-theme', theme);
document.addEventListener('click', (e) => {
const btn = (e.target as Element).closest('[data-theme-toggle]');
if (!btn) return;
+11 -9
View File
@@ -609,15 +609,17 @@ const news_archive_active =
</style>
<script>
const btn = document.querySelector('.inst-header__menu-btn') as HTMLButtonElement | null;
const menu = document.getElementById('inst-mobile-menu');
const labelOpen = btn?.dataset.menuLabelOpen ?? 'Open menu';
const labelClose = btn?.dataset.menuLabelClose ?? 'Close menu';
(typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : setTimeout)(() => {
const btn = document.querySelector('.inst-header__menu-btn') as HTMLButtonElement | null;
const menu = document.getElementById('inst-mobile-menu');
const labelOpen = btn?.dataset.menuLabelOpen ?? 'Open menu';
const labelClose = btn?.dataset.menuLabelClose ?? 'Close menu';
btn?.addEventListener('click', () => {
const is_open = menu?.getAttribute('aria-hidden') === 'false';
menu?.setAttribute('aria-hidden', is_open ? 'true' : 'false');
btn.setAttribute('aria-expanded', is_open ? 'false' : 'true');
btn.setAttribute('aria-label', is_open ? labelOpen : labelClose);
btn?.addEventListener('click', () => {
const is_open = menu?.getAttribute('aria-hidden') === 'false';
menu?.setAttribute('aria-hidden', is_open ? 'true' : 'false');
btn.setAttribute('aria-expanded', is_open ? 'false' : 'true');
btn.setAttribute('aria-label', is_open ? labelOpen : labelClose);
});
});
</script>
+9 -7
View File
@@ -285,13 +285,15 @@ const auth_labels = show_auth ? authNavLabels(config) : null;
</style>
<script>
const btn = document.querySelector('.btn-menu') as HTMLButtonElement | null;
const menu = document.getElementById('mobile-menu');
(typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : setTimeout)(() => {
const btn = document.querySelector('.btn-menu') as HTMLButtonElement | null;
const menu = document.getElementById('mobile-menu');
btn?.addEventListener('click', () => {
const is_open = menu?.getAttribute('aria-hidden') === 'false';
menu?.setAttribute('aria-hidden', is_open ? 'true' : 'false');
btn.setAttribute('aria-expanded', is_open ? 'false' : 'true');
btn.setAttribute('aria-label', is_open ? 'Open menu' : 'Close menu');
btn?.addEventListener('click', () => {
const is_open = menu?.getAttribute('aria-hidden') === 'false';
menu?.setAttribute('aria-hidden', is_open ? 'true' : 'false');
btn.setAttribute('aria-expanded', is_open ? 'false' : 'true');
btn.setAttribute('aria-label', is_open ? 'Open menu' : 'Close menu');
});
});
</script>