Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/detection/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/resources.test.ts
Original file line number Diff line number Diff line change
@@ -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('<script src="https://ads.example.net/ad.js"></script>', [
'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(
'<script type="module" src="https://example.com/app.js"></script>' +
'<script nomodule src="https://example.com/polyfills.js"></script>',
['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(
Expand Down