Skip to content

Commit

Permalink
Reset visibilitywatcher on soft nav
Browse files Browse the repository at this point in the history
  • Loading branch information
tunetheweb committed Dec 15, 2023
1 parent 1e72319 commit 42b8e47
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/lib/getVisibilityWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ const removeChangeListeners = () => {
removeEventListener('prerenderingchange', onVisibilityUpdate, true);
};

export const getVisibilityWatcher = () => {
export const getVisibilityWatcher = (reset = false) => {
if (reset) {
firstHiddenTime = -1;
}
if (firstHiddenTime < 0) {
// If the document is hidden when this code runs, assume it was hidden
// since navigation start. This isn't a perfect heuristic, but it's the
Expand Down
3 changes: 2 additions & 1 deletion src/onFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const onFCP = (onReport: FCPReportCallback, opts?: ReportOpts) => {
let metricNavStartTime = 0;

whenActivated(() => {
const visibilityWatcher = getVisibilityWatcher();
let visibilityWatcher = getVisibilityWatcher();
let metric = initMetric('FCP');
let report: ReturnType<typeof bindReporter>;

Expand All @@ -64,6 +64,7 @@ export const onFCP = (onReport: FCPReportCallback, opts?: ReportOpts) => {
opts!.reportAllChanges
);
if (navigation === 'soft-navigation') {
visibilityWatcher = getVisibilityWatcher(true);
const softNavEntry = navigationId
? getSoftNavigationEntry(navigationId)
: null;
Expand Down
5 changes: 4 additions & 1 deletion src/onFID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ export const onFID = (onReport: FIDReportCallback, opts?: ReportOpts) => {
const softNavsEnabled = softNavs(opts);

whenActivated(() => {
const visibilityWatcher = getVisibilityWatcher();
let visibilityWatcher = getVisibilityWatcher();
let metric = initMetric('FID');
let report: ReturnType<typeof bindReporter>;

const initNewFIDMetric = (
navigation?: Metric['navigationType'],
navigationId?: string
) => {
if (navigation === 'soft-navigation') {
visibilityWatcher = getVisibilityWatcher(true);
}
metric = initMetric('FID', 0, navigation, navigationId);
report = bindReporter(
onReport,
Expand Down
3 changes: 2 additions & 1 deletion src/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const onLCP = (onReport: LCPReportCallback, opts?: ReportOpts) => {
let metricNavStartTime = 0;

whenActivated(() => {
const visibilityWatcher = getVisibilityWatcher();
let visibilityWatcher = getVisibilityWatcher();
let metric = initMetric('LCP');
let report: ReturnType<typeof bindReporter>;

Expand All @@ -72,6 +72,7 @@ export const onLCP = (onReport: LCPReportCallback, opts?: ReportOpts) => {
);
reportedMetric = false;
if (navigation === 'soft-navigation') {
visibilityWatcher = getVisibilityWatcher(true);
const softNavEntry = getSoftNavigationEntry(navigationId);
metricNavStartTime =
softNavEntry && softNavEntry.startTime ? softNavEntry.startTime : 0;
Expand Down

0 comments on commit 42b8e47

Please sign in to comment.