Budget gate: Next's idle link prefetches counted apart, third parties recorded then aborted, spec filter safe for any checkout path; zero counted queries fail

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 14:19:26 +01:00
co-authored by Claude Opus 5.5
parent beef517c23
commit 3b32e659c5
7 changed files with 43 additions and 20 deletions
+1 -1
View File
@@ -1561,7 +1561,7 @@ block library — [doctor.md](../doctor.md)):
bash .agents/skills/iapl-audit/scripts/audit.sh
pnpm exec iapl-doctor # + --deep --url http://localhost:3000 on the built site
node .agents/skills/iapl-seo-check/scripts/seo-check.mjs http://localhost:3000
pnpm test:e2e performance # the budget gate (iapl-performance) on the built site
pnpm test:e2e e2e/performance.spec # the budget gate (iapl-performance) on the built site
pnpm generate:types && pnpm tsc --noEmit
pnpm build # --webpack in the script; locally, not only dev
# hardcoding and the field zoo in blocks
+1 -1
View File
@@ -34,7 +34,7 @@ pnpm exec iapl-doctor --deep --only perf-images # stored
```
Lab numbers per page (phone on Slow 4G with a 4× slower CPU, and desktop): copy `resources/perf-lab.mjs` and
`resources/performance.spec.template.ts` (→ `e2e/performance.spec.ts`), list one page of each template, run
`pnpm test:e2e performance`, then set each budget to what you measured plus ~20 %. From then on the test fails when
`pnpm test:e2e e2e/performance.spec`, then set each budget to what you measured plus ~20 %. From then on the test fails when
a change makes a page slower. PageSpeed Insights on the deployed site shows the field data (real visitors).
## 3. Read the result
@@ -13,7 +13,10 @@
* inp (the longest Event Timing entry of one scripted interaction: a click on the page's first visible button),
* scriptMs (CDP Performance.ScriptDuration: parse, compile, run and hydrate), requests, bytes per type
* (transfer size, compressed), renderBlocking (resource timing renderBlockingStatus), thirdParty (requests to
* another origin before any consent is given).
* another origin before any consent is given). Next's link prefetches (`?_rsc=`, made when the browser is idle) are
* counted apart (`prefetches`): they are not part of the page's load and come at a different moment each run.
* Option `blockThirdParty` (the gate): requests to another origin are recorded, then aborted — a slow external host
* cannot hold the page's load event, and the numbers are the site's own.
*/
import { chromium } from '@playwright/test'
@@ -45,7 +48,7 @@ const OBSERVERS = `(() => {
* @typedef {{ bytes: { css: number, font: number, html: number, image: number, js: number, other: number }, cls: number,
* fcp: null | number, inp: null | number, interaction: null | string, lcp: null | number,
* lcpElement: null | { fetchpriority: null | string, loading: null | string, src: null | string, tag: null | string },
* load: null | number, profile: string, renderBlocking: string[], requests: number, runs?: number, scriptMs: null | number,
* load: null | number, prefetches: number, profile: string, renderBlocking: string[], requests: number, runs?: number, scriptMs: null | number,
* status: null | number, tbt: null | number, thirdParty: string[], ttfb: null | number, url: string }} PageMeasurement
*/
@@ -62,7 +65,7 @@ const typeOf = (resourceType, mime = '') => {
* @param {import('@playwright/test').Browser} browser
* @param {string} url
* @param {'mobile'|'desktop'} profileName
* @param {{ interact?: boolean, settleMs?: number }} [options]
* @param {{ blockThirdParty?: boolean, interact?: boolean, settleMs?: number }} [options]
* @returns {Promise<PageMeasurement>}
*/
export async function measurePage(browser, url, profileName, options = {}) {
@@ -78,13 +81,19 @@ export async function measurePage(browser, url, profileName, options = {}) {
await page.addInitScript(OBSERVERS)
const origin = new URL(url).origin
if (options.blockThirdParty) {await context.route((u) => u.origin !== origin, (route) => route.abort())}
const requests = new Map()
cdp.on('Network.requestWillBeSent', (e) => { if (!e.request.url.startsWith('data:')) {requests.set(e.requestId, { bytes: 0, type: typeOf(e.type), url: e.request.url })} })
let prefetches = 0
cdp.on('Network.requestWillBeSent', (e) => {
if (e.request.url.startsWith('data:')) {return}
if (/[?&]_rsc=/.test(e.request.url)) {prefetches++; return}
requests.set(e.requestId, { bytes: 0, type: typeOf(e.type), url: e.request.url })
})
cdp.on('Network.responseReceived', (e) => { const r = requests.get(e.requestId); if (r) {r.type = typeOf(e.type, e.response.mimeType); r.status = e.response.status} })
cdp.on('Network.loadingFinished', (e) => { const r = requests.get(e.requestId); if (r) {r.bytes = e.encodedDataLength} })
const response = await page.goto(url, { waitUntil: 'load' })
await page.waitForLoadState('networkidle').catch(() => {})
const response = await page.goto(url, { timeout: 60_000, waitUntil: 'load' })
await page.waitForLoadState('networkidle', { timeout: 15_000 }).catch(() => {})
await page.waitForTimeout(options.settleMs ?? 1000)
let interaction = null
@@ -124,6 +133,7 @@ export async function measurePage(browser, url, profileName, options = {}) {
lcp: round(vitals.lcp?.at),
lcpElement: vitals.lcp ? { fetchpriority: vitals.lcp.fetchpriority, loading: vitals.lcp.loading, src: vitals.lcp.src ? vitals.lcp.src.replace(origin, '') : null, tag: vitals.lcp.tag } : null,
load: round(nav.load),
prefetches,
profile: profileName,
renderBlocking: nav.blocking.map((u) => u.replace(origin, '')),
requests: list.length,
@@ -149,7 +159,7 @@ export const median = (values) => {
* @param {string} url
* @param {'mobile'|'desktop'} profileName
* @param {number} [runs]
* @param {{ interact?: boolean, settleMs?: number }} [options]
* @param {{ blockThirdParty?: boolean, interact?: boolean, settleMs?: number }} [options]
* @returns {Promise<PageMeasurement>}
*/
export async function measureMedian(browser, url, profileName, runs = 3, options = {}) {
@@ -38,7 +38,8 @@ for (const page of PAGES) {
for (const profile of ['mobile', 'desktop'] as const) {
test(`${page.name} (${profile}) stays within its performance budget`, async ({ baseURL }) => {
test.setTimeout(120_000)
const measured = await measureMedian(browser, `${baseURL}${page.path}`, profile, runs)
// third-party requests are recorded, then aborted: an external host cannot fail or hang the test
const measured = await measureMedian(browser, `${baseURL}${page.path}`, profile, runs, { blockThirdParty: true })
test.info().annotations.push({ description: JSON.stringify({ bytes: measured.bytes, cls: measured.cls, inp: measured.inp, lcp: measured.lcp, requests: measured.requests }), type: 'measured' })
expect(overBudget(measured, { ...budgets[profile], ...page.budget }), `${page.path} ${profile}`).toEqual([])
if (measured.lcpElement?.tag === 'img') {expect(measured.lcpElement.loading, 'the LCP image is lazy').not.toBe('lazy')}