diff --git a/src/detection/resources.ts b/src/detection/resources.ts index 0050b0a..8e3bf65 100644 --- a/src/detection/resources.ts +++ b/src/detection/resources.ts @@ -89,6 +89,10 @@ export function heuristicFailures(win: Window): { failures: FailedResource[]; re 400, ); for (const script of scripts) { + // A browser that runs modules never fetches a nomodule script: it is a + // fallback for old browsers (Next.js ships one on every page), so its + // absence from resource timing is not a blocked request. + if (script.hasAttribute('nomodule')) continue; const type = (script.getAttribute('type') ?? '').trim().toLowerCase(); if (!EXECUTABLE_SCRIPT_TYPES.has(type)) continue; let abs: string; diff --git a/tests/unit/resources.test.ts b/tests/unit/resources.test.ts index 1bda87b..3c689e7 100644 --- a/tests/unit/resources.test.ts +++ b/tests/unit/resources.test.ts @@ -1,8 +1,37 @@ import { describe, expect, it } from 'vitest'; -import { summarizeFailures } from '../../src/detection/resources'; +import { heuristicFailures, summarizeFailures } from '../../src/detection/resources'; import { pageIssuesFor } from '../../src/content/report'; import type { ScanResult } from '../../src/detection/engine'; +describe('heuristicFailures', () => { + // A page whose resource timing lists only the scripts that actually loaded. + function pageWith(html: string, loaded: string[]): Window { + document.body.innerHTML = html; + return { + document, + location: { href: 'https://example.com/page' }, + performance: { getEntriesByType: () => loaded.map((name) => ({ name })) }, + } as unknown as Window; + } + + it('reports a script that never loaded', () => { + const win = pageWith('', [ + 'https://example.com/app.js', + ]); + expect(heuristicFailures(win).failures).toEqual([{ host: 'ads.example.net', kind: 'script' }]); + }); + + it('does not report a nomodule script, which module-capable browsers never fetch', () => { + // Next.js and Vite's legacy plugin ship one on every page. + const win = pageWith( + '' + + '', + ['https://example.com/app.js'], + ); + expect(heuristicFailures(win).failures).toEqual([]); + }); +}); + describe('summarizeFailures', () => { it('groups by host and classifies first-party, third-party and ad/tracking resources', () => { const s = summarizeFailures(