diff --git a/test/e2e/onTTFB-test.js b/test/e2e/onTTFB-test.js index 07e34fb1..76d9983e 100644 --- a/test/e2e/onTTFB-test.js +++ b/test/e2e/onTTFB-test.js @@ -398,6 +398,32 @@ 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?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.