Skip to content

Commit

Permalink
fix: better handle TTFB from BFCache
Browse files Browse the repository at this point in the history
  • Loading branch information
o.drapeza committed Feb 3, 2023
1 parent 1f9d829 commit 6ede48c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const perfume = new Perfume({
myAnalyticsTool.track('storageEstimate', data);
break;
case 'TTFB':
myAnalyticsTool.track('timeToFirstByte', { duration: data });
// Zero value mean than page restored from Back-Forward Cache, and you can ignore it before reporting
if (data > 0) {
myAnalyticsTool.track('timeToFirstByte', { duration: data });
}
break;
case 'RT':
myAnalyticsTool.track('redirectTime', { duration: data });
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export const initPerformanceObserver = (): void => {
onTTFB(report => {
// Calculate the request time by subtracting from TTFB
// everything that happened prior to the request starting.
//
// report.entries[0] will be missing when page was restored from Back-Forward Cache.
// In this case, `0` value will be reported, and perfume.js users can decide how to handle it (report `0` or ignore it)
//
// @ts-ignore
report.value = report.value - report.entries[0].requestStart;
report.value = report.entries[0] ? report.value - report.entries[0].requestStart : report.value;
// @ts-ignore
logMetric(report);
}, config.reportOptions.ttfb);
Expand Down

0 comments on commit 6ede48c

Please sign in to comment.