Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent TTFB from reporting after bfcache restore #201

Merged
merged 1 commit into from
Jan 21, 2022
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: 2 additions & 2 deletions src/getTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const afterLoad = (callback: () => void) => {
// Queue a task so the callback runs after `loadEventEnd`.
setTimeout(callback, 0);
} else {
// Use `pageshow` so the callback runs after `loadEventEnd`.
addEventListener('pageshow', callback);
// Queue a task so the callback runs after `loadEventEnd`.
addEventListener('load', () => setTimeout(callback, 0));
}
}

Expand Down
33 changes: 32 additions & 1 deletion test/e2e/getTTFB-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

const assert = require('assert');
const {beaconCountIs, clearBeacons, getBeacons} = require('../utils/beacons.js');
const {afterLoad} = require('../utils/afterLoad.js');
const {beaconCountIs, clearBeacons, getBeacons} = require('../utils/beacons.js');
const {stubForwardBack} = require('../utils/stubForwardBack.js');

/**
* Accepts a PerformanceNavigationTimingEntry (or shim) and asserts that it
Expand Down Expand Up @@ -109,6 +110,36 @@ describe('getTTFB()', async function() {

assertValidEntry(ttfb.entries[0]);
});

it('does not report after a bfcache restore', async function() {
await browser.url('/test/ttfb');

const ttfb = await getTTFBBeacon();

if (browser.capabilities.browserName === 'firefox' && !ttfb) {
// Skipping test in Firefox due to entry not reported.
this.skip();
}

assert(ttfb.value >= 0);
assert(ttfb.value >= ttfb.entries[0].requestStart);
assert(ttfb.value <= ttfb.entries[0].loadEventEnd);
assert(ttfb.id.match(/^v2-\d+-\d+$/));
assert.strictEqual(ttfb.name, 'TTFB');
assert.strictEqual(ttfb.value, ttfb.delta);
assert.strictEqual(ttfb.entries.length, 1);

assertValidEntry(ttfb.entries[0]);

await clearBeacons();
await stubForwardBack();

// Wait a bit to ensure no beacons were sent.
await browser.pause(1000);

const bfcacheRestoreBeacons = await getBeacons();
assert.strictEqual(bfcacheRestoreBeacons.length, 0);
});
});

const getTTFBBeacon = async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/utils/afterLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function afterLoad() {
if (document.readyState === 'complete') {
setTimeout(done, 0);
} else {
addEventListener('pageshow', done);
addEventListener('load', () => setTimeout(done, 0));
}
});
}
Expand Down