Skip to content

Commit

Permalink
feat: add experimental support for soft navigations
Browse files Browse the repository at this point in the history
Summary:

Recent versions of Chromium have added support for tracking web vitals metrics
across soft navigations. This change adds support for tracking these metrics in
the extension.

TODO:

- [ ] document the new `reportSoftNavs` option in the README
  `web-vitals` package
- [ ] Hide the option in the options page if one of the following is false:
  1. The browser supports the `soft-navigation` entry type
  2. The `web-vitals` package has been built with the `soft-navs` branch
  • Loading branch information
vegerot committed Aug 19, 2024
1 parent 5c638ab commit 6ee5a07
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
15 changes: 8 additions & 7 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
"private": true,
"scripts": {
"lint": "npx eslint src --fix",
"build": "npm install; cp node_modules/web-vitals/dist/web-vitals.attribution.js src/browser_action/web-vitals.js"
"build": "npm install && cp node_modules/web-vitals/dist/web-vitals.attribution.js src/browser_action/web-vitals.js"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-google": "^0.14.0"
},
"dependencies": {
"web-vitals": "^4.0.0"
"web-vitals": "4.2.3-soft-navs"
}
}
13 changes: 7 additions & 6 deletions src/browser_action/vitals.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
let latestCLS = {};
let enableLogging = localStorage.getItem('web-vitals-extension-debug')=='TRUE';
let enableUserTiming = localStorage.getItem('web-vitals-extension-user-timing')=='TRUE';
let reportSoftNavs = (await chrome.storage.sync.get('reportSoftNavs')).reportSoftNavs ?? false;
let tabLoadedInBackground;

const COLOR_GOOD = '#0CCE6A';
Expand Down Expand Up @@ -514,15 +515,15 @@
// debounce the broadcast of the metric.
latestCLS = metric;
debouncedCLSBroadcast();
}, { reportAllChanges: true });
}, { reportAllChanges: true, reportSoftNavs });

webVitals.onLCP(broadcastMetricsUpdates, { reportAllChanges: true });
webVitals.onFID(broadcastMetricsUpdates, { reportAllChanges: true });
webVitals.onLCP(broadcastMetricsUpdates, { reportAllChanges: true, reportSoftNavs: reportSoftNavs });
webVitals.onFID(broadcastMetricsUpdates, { reportAllChanges: true, reportSoftNavs: reportSoftNavs });
webVitals.onINP((metric) => {
broadcastMetricsUpdates(metric)
}, { reportAllChanges: true });
webVitals.onFCP(broadcastMetricsUpdates, { reportAllChanges: true });
webVitals.onTTFB(broadcastMetricsUpdates, { reportAllChanges: true });
}, { reportAllChanges: true, reportSoftNavs: true });
webVitals.onFCP(broadcastMetricsUpdates, { reportAllChanges: true, reportSoftNavs: reportSoftNavs });
webVitals.onTTFB(broadcastMetricsUpdates, { reportAllChanges: true, reportSoftNavs: reportSoftNavs });

if (enableLogging) {
onEachInteraction((metric) => {
Expand Down
2 changes: 1 addition & 1 deletion src/browser_action/web-vitals.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<input type="checkbox" id="noBadgeAnimation">
Only show overall status in badge (no animation of failing metrics)
</label>
<br/>
<label>
<input type="checkbox" id="reportSoftNavs">
<strong>Experimental:</strong> Report <a href="https://developer.chrome.com/docs/web-platform/soft-navigations-experiment">soft navigations</a> for LCP, INP, and CLS
</label>
<div id="status"></div>
<button id="save">Save</button>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const optionsConsoleLoggingNode = document.getElementById('consoleLogging');
const optionsNoBadgeAnimation = document.getElementById('noBadgeAnimation');
const optionsUserTimingNode = document.getElementById('userTiming');
const optionsPreferPhoneFieldNode = document.getElementById('preferPhoneField');
const optionsSoftNav = document.getElementById('reportSoftNavs');
const optionsSaveBtn = document.getElementById('save');
const optionsStatus = document.getElementById('status');

Expand All @@ -16,6 +17,7 @@ function saveOptions() {
userTiming: optionsUserTimingNode.checked,
preferPhoneField: optionsPreferPhoneFieldNode.checked,
noBadgeAnimation: optionsNoBadgeAnimation.checked,
reportSoftNavs: optionsSoftNav.checked,
}, () => {
// Update status to let user know options were saved.
optionsStatus.textContent = 'Options saved.';
Expand All @@ -36,12 +38,14 @@ function restoreOptions() {
userTiming: false,
preferPhoneField: false,
noBadgeAnimation: false,
}, ({enableOverlay, debug, userTiming, preferPhoneField, noBadgeAnimation}) => {
reportSoftNavs: false,
}, ({enableOverlay, debug, userTiming, preferPhoneField, noBadgeAnimation, reportSoftNavs}) => {
optionsOverlayNode.checked = enableOverlay;
optionsConsoleLoggingNode.checked = debug;
optionsUserTimingNode.checked = userTiming;
optionsPreferPhoneFieldNode.checked = preferPhoneField;
optionsNoBadgeAnimation.checked = noBadgeAnimation;
optionsSoftNav.checked = reportSoftNavs;
});
}
document.addEventListener('DOMContentLoaded', restoreOptions);
Expand Down

0 comments on commit 6ee5a07

Please sign in to comment.