Skip to content

Commit dfd445a

Browse files
authored
Add 103 Early Hints test (#609)
* Add 103 Early Hints test * Update test/e2e/onTTFB-test.js * Update test/e2e/onTTFB-test.js
1 parent 3b9acdb commit dfd445a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test/e2e/onTTFB-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,32 @@ describe('onTTFB()', async function () {
398398
assert.strictEqual(ttfb.attribution.requestDuration, 0);
399399
assert.strictEqual(ttfb.attribution.navigationEntry, undefined);
400400
});
401+
402+
it('reports the correct value for Early Hints', async function () {
403+
await navigateTo('/test/ttfb?earlyHintsDelay=50&attribution=1');
404+
405+
const ttfb = await getTTFBBeacon();
406+
407+
if ('finalResponseHeadersStart' in ttfb.attribution.navigationEntry) {
408+
assert.strictEqual(
409+
ttfb.value,
410+
ttfb.attribution.navigationEntry.responseStart,
411+
);
412+
assert.strictEqual(
413+
ttfb.value,
414+
ttfb.attribution.navigationEntry.firstInterimResponseStart,
415+
);
416+
assert(
417+
ttfb.value <
418+
ttfb.attribution.navigationEntry.finalResponseHeadersStart,
419+
);
420+
} else {
421+
assert.strictEqual(
422+
ttfb.value,
423+
ttfb.attribution.navigationEntry.responseStart,
424+
);
425+
}
426+
});
401427
});
402428
});
403429

test/server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ app.use((req, res, next) => {
3939
}
4040
});
4141

42+
// Allow the use of a `earlyHintsDelay` query param to delay any response
43+
// after sending an early hints
44+
app.use((req, res, next) => {
45+
if (req.query && req.query.earlyHintsDelay) {
46+
res.writeEarlyHints({
47+
'link': '</styles.css>; rel=preload; as=style',
48+
});
49+
setTimeout(next, req.query.earlyHintsDelay);
50+
} else {
51+
next();
52+
}
53+
});
54+
4255
// Add a "collect" endpoint to simulate analytics beacons.
4356
app.post('/collect', bodyParser.text(), (req, res) => {
4457
// Uncomment to log the metric when manually testing.

0 commit comments

Comments
 (0)