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

Report TTFB after a bfcache restore #220

Merged
merged 1 commit into from Apr 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/getTTFB.ts
Expand Up @@ -16,6 +16,7 @@

import {bindReporter} from './lib/bindReporter.js';
import {initMetric} from './lib/initMetric.js';
import {onBFCacheRestore} from './lib/bfcache.js';
import {getNavigationEntry} from './lib/getNavigationEntry.js';
import {ReportHandler} from './types.js';

Expand All @@ -31,8 +32,8 @@ const afterLoad = (callback: () => void) => {
}

export const getTTFB = (onReport: ReportHandler, reportAllChanges?: boolean) => {
const metric = initMetric('TTFB');
const report = bindReporter(onReport, metric, reportAllChanges);
let metric = initMetric('TTFB');
let report = bindReporter(onReport, metric, reportAllChanges);

afterLoad(() => {
const navigationEntry = getNavigationEntry();
Expand All @@ -51,4 +52,11 @@ export const getTTFB = (onReport: ReportHandler, reportAllChanges?: boolean) =>
report(true);
}
});

onBFCacheRestore((event) => {
metric = initMetric('TTFB');
report = bindReporter(onReport, metric, reportAllChanges);
metric.value = performance.now() - event.timeStamp;
report(true);
});
};
35 changes: 19 additions & 16 deletions test/e2e/getTTFB-test.js
Expand Up @@ -113,35 +113,38 @@ describe('getTTFB()', async function() {
assertValidEntry(ttfb.entries[0]);
});

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

const ttfb = await getTTFBBeacon();
const ttfb1 = await getTTFBBeacon();

if (browser.capabilities.browserName === 'firefox' && !ttfb) {
if (browser.capabilities.browserName === 'firefox' && !ttfb1) {
// 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.navigationType, 'navigate');
assert.strictEqual(ttfb.entries.length, 1);
assert(ttfb1.value >= 0);
assert(ttfb1.value >= ttfb1.entries[0].requestStart);
assert(ttfb1.value <= ttfb1.entries[0].loadEventEnd);
assert(ttfb1.id.match(/^v2-\d+-\d+$/));
assert.strictEqual(ttfb1.name, 'TTFB');
assert.strictEqual(ttfb1.value, ttfb1.delta);
assert.strictEqual(ttfb1.navigationType, 'navigate');
assert.strictEqual(ttfb1.entries.length, 1);

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

await clearBeacons();
await stubForwardBack();

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

const bfcacheRestoreBeacons = await getBeacons();
assert.strictEqual(bfcacheRestoreBeacons.length, 0);
assert(ttfb2.value >= 0);
assert(ttfb2.id.match(/^v2-\d+-\d+$/));
assert.strictEqual(ttfb2.name, 'TTFB');
assert.strictEqual(ttfb2.value, ttfb2.delta);
assert.strictEqual(ttfb2.navigationType, 'back_forward_cache');
assert.strictEqual(ttfb2.entries.length, 0);
});
});

Expand Down