From 5c522a993f4bd8dc33f28797247004aae03ad5aa Mon Sep 17 00:00:00 2001 From: Barry Pollard Date: Tue, 15 Apr 2025 15:23:00 +0100 Subject: [PATCH 1/3] Add 103 Early Hints test --- test/e2e/onTTFB-test.js | 29 +++++++++++++++++++++++++++++ test/server.js | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/test/e2e/onTTFB-test.js b/test/e2e/onTTFB-test.js index 07e34fb1..e22affba 100644 --- a/test/e2e/onTTFB-test.js +++ b/test/e2e/onTTFB-test.js @@ -398,6 +398,35 @@ describe('onTTFB()', async function () { assert.strictEqual(ttfb.attribution.requestDuration, 0); assert.strictEqual(ttfb.attribution.navigationEntry, undefined); }); + + it('reports the correct value for Early Hints', async function () { + await navigateTo( + // '/test/ttfb?responseStart=10&earlyHintsDelay=50&attribution=1', + '/test/ttfb?earlyHintsDelay=50&attribution=1', + ); + + const ttfb = await getTTFBBeacon(); + + if ('finalResponseHeadersStart' in ttfb.attribution.navigationEntry) { + assert.strictEqual( + ttfb.value, + ttfb.attribution.navigationEntry.responseStart, + ); + assert.strictEqual( + ttfb.value, + ttfb.attribution.navigationEntry.firstInterimResponseStart, + ); + assert( + ttfb.value < + ttfb.attribution.navigationEntry.finalResponseHeadersStart, + ); + } else { + assert.strictEqual( + ttfb.value, + ttfb.attribution.navigationEntry.responseStart, + ); + } + }); }); }); diff --git a/test/server.js b/test/server.js index ff666576..5d13d4b0 100644 --- a/test/server.js +++ b/test/server.js @@ -39,6 +39,19 @@ app.use((req, res, next) => { } }); +// Allow the use of a `earlyHintsDelay` query param to delay any response +// after sending an early hints +app.use((req, res, next) => { + if (req.query && req.query.earlyHintsDelay) { + res.writeEarlyHints({ + 'link': '; rel=preload; as=style', + }); + setTimeout(next, req.query.earlyHintsDelay); + } else { + next(); + } +}); + // Add a "collect" endpoint to simulate analytics beacons. app.post('/collect', bodyParser.text(), (req, res) => { // Uncomment to log the metric when manually testing. From c701eda164bebcb96f6d94873e131be9ceaa6cd2 Mon Sep 17 00:00:00 2001 From: Barry Pollard Date: Thu, 17 Apr 2025 18:43:33 +0100 Subject: [PATCH 2/3] Update test/e2e/onTTFB-test.js --- test/e2e/onTTFB-test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/e2e/onTTFB-test.js b/test/e2e/onTTFB-test.js index e22affba..6ea396ee 100644 --- a/test/e2e/onTTFB-test.js +++ b/test/e2e/onTTFB-test.js @@ -401,7 +401,6 @@ describe('onTTFB()', async function () { it('reports the correct value for Early Hints', async function () { await navigateTo( - // '/test/ttfb?responseStart=10&earlyHintsDelay=50&attribution=1', '/test/ttfb?earlyHintsDelay=50&attribution=1', ); From e8171f30b03325f5a3d8e3f6a6c827c3873fd434 Mon Sep 17 00:00:00 2001 From: Barry Pollard Date: Thu, 17 Apr 2025 18:44:41 +0100 Subject: [PATCH 3/3] Update test/e2e/onTTFB-test.js --- test/e2e/onTTFB-test.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/e2e/onTTFB-test.js b/test/e2e/onTTFB-test.js index 6ea396ee..76d9983e 100644 --- a/test/e2e/onTTFB-test.js +++ b/test/e2e/onTTFB-test.js @@ -400,9 +400,7 @@ describe('onTTFB()', async function () { }); it('reports the correct value for Early Hints', async function () { - await navigateTo( - '/test/ttfb?earlyHintsDelay=50&attribution=1', - ); + await navigateTo('/test/ttfb?earlyHintsDelay=50&attribution=1'); const ttfb = await getTTFBBeacon();