Skip to content

Commit 1e7f55d

Browse files
rviscomitunetheweb
andauthored
Upgrade to web-vitals v4 (#3661)
* Upgrade to web-vitals v4 * Update src/static/js/send-web-vitals.js Co-authored-by: Barry Pollard <barrypollard@google.com> --------- Co-authored-by: Barry Pollard <barrypollard@google.com>
1 parent 768a6ce commit 1e7f55d

4 files changed

Lines changed: 47 additions & 65 deletions

File tree

src/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"run-script-os": "1.1.6",
4848
"showdown": "2.1.0",
4949
"smartypants": "0.2.2",
50-
"web-vitals": "3.5.2",
50+
"web-vitals": "4.0.0",
5151
"xml-js": "1.6.11"
5252
}
5353
}

src/static/js/send-web-vitals.js

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,45 @@
11
function sendWebVitals() {
2-
function getLoafAttribution(attribution) {
3-
if (!attribution) {
4-
return {};
5-
}
6-
7-
const entry = attribution.eventEntry;
82

9-
if (!entry) {
10-
return {};
11-
}
12-
13-
if (!PerformanceObserver.supportedEntryTypes.includes('long-animation-frame')) {
3+
function getLoafAttribution(attribution) {
4+
if (!attribution?.longAnimationFrameEntries) {
145
return {};
156
}
167

178
let loafAttribution = {
189
debug_loaf_script_total_duration: 0
1910
};
2011

21-
const longAnimationFrames = performance.getEntriesByType('long-animation-frame');
22-
longAnimationFrames.filter(loaf => {
23-
// LoAFs that intersect with the event.
24-
return entry.startTime < (loaf.startTime + loaf.duration) && loaf.startTime < (entry.startTime + entry.duration);
25-
}).forEach(loaf => {
26-
const loafEndTime = loaf.startTime + loaf.duration;
27-
loaf.scripts.forEach(script => {
28-
if (script.duration <= loafAttribution.debug_loaf_script_total_duration) {
29-
return;
30-
}
31-
loafAttribution = {
32-
// Stats for the LoAF entry itself.
33-
debug_loaf_entry_start_time: loaf.startTime,
34-
debug_loaf_entry_end_time: loafEndTime,
35-
debug_loaf_entry_work_duration: loaf.renderStart ? loaf.renderStart - loaf.startTime : loaf.duration,
36-
debug_loaf_entry_render_duration: loaf.renderStart ? loafEndTime - loaf.renderStart : 0,
37-
debug_loaf_entry_total_forced_style_and_layout_duration: loaf.scripts.reduce((sum, script) => sum + script.forcedStyleAndLayoutDuration, 0),
38-
debug_loaf_entry_pre_layout_duration: loaf.styleAndLayoutStart ? loaf.styleAndLayoutStart - loaf.renderStart : 0,
39-
debug_loaf_entry_style_and_layout_duration: loaf.styleAndLayoutStart ? loafEndTime - loaf.styleAndLayoutStart : 0,
40-
41-
// Stats for the longest script in the LoAF entry.
42-
debug_loaf_script_total_duration: script.duration,
43-
debug_loaf_script_compile_duration: script.executionStart - script.startTime,
44-
debug_loaf_script_exec_duration: script.startTime + script.duration - script.executionStart,
45-
debug_loaf_script_source: script.sourceLocation || script.invoker || script.name, // TODO: remove after Chrome 123
46-
debug_loaf_script_type: script.invokerType || script.type, // TODO: remove `|| script.type` after Chrome 123
47-
// New in Chrome 122/123 (will be null until then)
48-
debug_loaf_script_invoker: script.invoker,
49-
debug_loaf_script_source_url: script.sourceURL,
50-
debug_loaf_script_source_function_name: script.sourceFunctionName,
51-
debug_loaf_script_source_char_position: script.sourceCharPosition,
52-
53-
// LoAF metadata.
54-
debug_loaf_meta_length: longAnimationFrames.length,
55-
}
56-
});
12+
// The last LoAF entry is usually the most relevant.
13+
const loaf = attribution.longAnimationFrameEntries.at(-1)
14+
const loafEndTime = loaf.startTime + loaf.duration;
15+
loaf.scripts.forEach(script => {
16+
if (script.duration <= loafAttribution.debug_loaf_script_total_duration) {
17+
return;
18+
}
19+
loafAttribution = {
20+
// Stats for the LoAF entry itself.
21+
debug_loaf_entry_start_time: loaf.startTime,
22+
debug_loaf_entry_end_time: loafEndTime,
23+
debug_loaf_entry_work_duration: loaf.renderStart ? loaf.renderStart - loaf.startTime : loaf.duration,
24+
debug_loaf_entry_render_duration: loaf.renderStart ? loafEndTime - loaf.renderStart : 0,
25+
debug_loaf_entry_total_forced_style_and_layout_duration: loaf.scripts.reduce((sum, script) => sum + script.forcedStyleAndLayoutDuration, 0),
26+
debug_loaf_entry_pre_layout_duration: loaf.styleAndLayoutStart ? loaf.styleAndLayoutStart - loaf.renderStart : 0,
27+
debug_loaf_entry_style_and_layout_duration: loaf.styleAndLayoutStart ? loafEndTime - loaf.styleAndLayoutStart : 0,
28+
29+
// Stats for the longest script in the LoAF entry.
30+
debug_loaf_script_total_duration: script.duration,
31+
debug_loaf_script_compile_duration: script.executionStart - script.startTime,
32+
debug_loaf_script_exec_duration: script.startTime + script.duration - script.executionStart,
33+
debug_loaf_script_forced_style_and_layout_duration: script.forcedStyleAndLayoutDuration,
34+
debug_loaf_script_type: script.invokerType,
35+
debug_loaf_script_invoker: script.invoker,
36+
debug_loaf_script_source_url: script.sourceURL,
37+
debug_loaf_script_source_function_name: script.sourceFunctionName,
38+
debug_loaf_script_source_char_position: script.sourceCharPosition,
39+
40+
// LoAF metadata.
41+
debug_loaf_meta_length: attribution.longAnimationFrameEntries.length,
42+
}
5743
});
5844

5945
if (!loafAttribution.debug_loaf_script_total_duration) {
@@ -91,18 +77,15 @@ function sendWebVitals() {
9177
case 'INP':
9278
const loafAttribution = getLoafAttribution(attribution);
9379
overrides = {
94-
debug_event: attribution.eventType,
95-
debug_time: Math.round(attribution.eventTime),
80+
debug_event: attribution.interactionType,
81+
debug_time: Math.round(attribution.interactionTime),
9682
debug_load_state: attribution.loadState,
97-
debug_target: attribution.eventTarget || '(not set)',
83+
debug_target: attribution.interactionTarget || '(not set)',
84+
debug_interaction_delay: Math.round(attribution.inputDelay),
85+
debug_processing_duration: Math.round(attribution.processingDuration),
86+
debug_presentation_delay: Math.round(attribution.presentationDelay),
9887
...loafAttribution
9988
};
100-
if (!attribution.eventEntry) {
101-
break;
102-
}
103-
overrides.debug_interaction_delay = Math.round(attribution.eventEntry.processingStart - attribution.eventEntry.startTime);
104-
overrides.debug_processing_time = Math.round(attribution.eventEntry.processingEnd - attribution.eventEntry.processingStart);
105-
overrides.debug_presentation_delay = Math.round(attribution.eventEntry.duration + attribution.eventEntry.startTime - attribution.eventEntry.processingEnd);
10689
break;
10790
case 'LCP':
10891
overrides = {

src/templates/base.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<head>
55
<meta charset="UTF-8">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<meta http-equiv="origin-trial" content="AqqBKXKVFUu6Op5aphalxMRl8CECiZ0BVlee+zWMM1E9R6oLnNQPRIXd5F8gEYxahMwEzEjOYKzA3Nf6jIc08AEAAAB0eyJvcmlnaW4iOiJodHRwczovL2h0dHBhcmNoaXZlLm9yZzo0NDMiLCJmZWF0dXJlIjoiTG9uZ0FuaW1hdGlvbkZyYW1lVGltaW5nIiwiZXhwaXJ5IjoxNzE2OTQwNzk5LCJpc1N1YmRvbWFpbiI6dHJ1ZX0=">
87
{% block head %}
98
<title>{% block title %}The Web Almanac{% endblock %}</title>
109
<link rel="stylesheet" href="{{ get_versioned_filename('/static/css/normalize.css') }}">

0 commit comments

Comments
 (0)