Skip to content

Commit db9e223

Browse files
authored
Misc perf improvements (#3570)
* Misc perf improvements * Add LoAF * Spacing * Remove debug
1 parent 147e5c0 commit db9e223

7 files changed

Lines changed: 116 additions & 25 deletions

File tree

src/static/css/almanac.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,16 @@ p.copyright a {
966966
display: none !important;
967967
}
968968

969+
.novisibility-until-js {
970+
visibility: hidden;
971+
}
972+
973+
@media (scripting: none) {
974+
.novisibility-until-js {
975+
display: none;
976+
}
977+
}
978+
969979
.nav-dropdown-btn.js-enable,
970980
.nav-dropdown-btn.js-enable:hover {
971981
opacity: 0.5;

src/static/js/almanac.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ function setDiscussionCount() {
476476
return;
477477
}
478478
document.querySelectorAll('.num-comments').forEach(el => {
479-
el.innerText = comments;
479+
el.textContent = comments;
480480
});
481481

482482
if (comments === 1) {
@@ -625,7 +625,7 @@ function toggleDescription(event) {
625625

626626
description.hidden = !description.hidden;
627627
event_button.setAttribute('aria-expanded', event_button.getAttribute('aria-expanded') == 'true' ? 'false' : 'true');
628-
event_button.innerHTML = event_button.getAttribute('aria-expanded') == 'true' ? event_button.getAttribute('data-hide-text') : event_button.getAttribute('data-show-text');
628+
event_button.textContent = event_button.getAttribute('aria-expanded') == 'true' ? event_button.getAttribute('data-hide-text') : event_button.getAttribute('data-show-text');
629629

630630
}
631631

@@ -635,7 +635,7 @@ function addShowDescription() {
635635
for (var index = 0; index < all_desc_buttons.length; ++index) {
636636
var desc_button = all_desc_buttons[index];
637637
desc_button.addEventListener('click', toggleDescription);
638-
desc_button.hidden = false;
638+
desc_button.classList.remove('novisibility-until-js');
639639
var description = document.querySelector('#' + desc_button.getAttribute('aria-controls'));
640640
if (description) {
641641
description.classList.remove('visually-hidden');

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

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,68 @@
11
function sendWebVitals() {
2+
function getLoafAttribution(attribution) {
3+
if (!attribution) {
4+
return {};
5+
}
6+
7+
const entry = attribution.eventEntry;
8+
9+
if (!entry) {
10+
return {};
11+
}
12+
13+
if (!PerformanceObserver.supportedEntryTypes.includes('long-animation-frame')) {
14+
return {};
15+
}
16+
17+
let loafAttribution = {
18+
debug_loaf_script_total_duration: 0
19+
};
20+
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+
loaf.scripts.forEach(script => {
27+
const totalDuration = script.startTime + script.duration - script.desiredExecutionStart;
28+
if (totalDuration > loafAttribution.debug_loaf_script_total_duration) {
29+
loafAttribution = {
30+
// Stats for the LoAF entry itself.
31+
debug_loaf_entry_start_time: loaf.startTime,
32+
debug_loaf_entry_end_time: loaf.startTime + loaf.duration,
33+
debug_loaf_entry_work_duration: loaf.renderStart ? loaf.renderStart - loaf.startTime : loaf.duration,
34+
debug_loaf_entry_render_duration: loaf.renderStart ? loaf.startTime + loaf.duration - loaf.renderStart : 0,
35+
debug_loaf_entry_total_forced_style_and_layout_duration: loaf.scripts.reduce((sum, script) => sum + script.forcedStyleAndLayoutDuration, 0),
36+
debug_loaf_entry_pre_layout_duration: loaf.styleAndLayoutStart ? loaf.styleAndLayoutStart - loaf.renderStart : 0,
37+
debug_loaf_entry_style_and_layout_duration: loaf.styleAndLayoutStart ? loaf.startTime + loaf.duration - loaf.styleAndLayoutStart : 0,
38+
39+
// Stats for the longest script in the LoAF entry.
40+
debug_loaf_script_total_duration: totalDuration,
41+
debug_loaf_script_compile_duration: script.executionStart - script.startTime,
42+
debug_loaf_script_exec_duration: script.startTime + script.duration - script.executionStart,
43+
debug_loaf_script_invoker: script.invoker,
44+
debug_loaf_script_type: script.invokerType,
45+
debug_loaf_script_source_url: script.sourceURL,
46+
debug_loaf_script_source_function_name: script.sourceFunctionName,
47+
debug_loaf_script_source_char_position: script.sourceCharPosition,
48+
49+
// LoAF metadata.
50+
debug_loaf_meta_length: longAnimationFrames.length,
51+
}
52+
}
53+
});
54+
});
55+
56+
if (!loafAttribution.debug_loaf_script_total_duration) {
57+
return {};
58+
}
59+
60+
// The LoAF script with the single longest total duration.
61+
return Object.fromEntries(Object.entries(loafAttribution).map(([k, v]) => {
62+
// Convert all floats to ints.
63+
return [k, typeof v == 'number' ? Math.floor(v) : v];
64+
}));
65+
}
266

367
function sendWebVitalsGAEvents({name, delta, id, attribution, navigationType}) {
468

@@ -22,11 +86,13 @@ function sendWebVitals() {
2286
break;
2387
case 'FID':
2488
case 'INP':
89+
const loafAttribution = getLoafAttribution(attribution);
2590
overrides = {
2691
debug_event: attribution.eventType,
2792
debug_time: Math.round(attribution.eventTime),
2893
debug_load_state: attribution.loadState,
2994
debug_target: attribution.eventTarget || '(not set)',
95+
...loafAttribution
3096
};
3197
if (!attribution.eventEntry) {
3298
break;
@@ -79,22 +145,20 @@ function sendWebVitals() {
79145
prefersColorScheme = 'not supported';
80146
}
81147

82-
gtag('event', name, Object.assign(
83-
{
84-
event_category: 'Web Vitals',
85-
value: Math.round(name === 'CLS' ? delta * 1000 : delta),
86-
event_label: id,
87-
non_interaction: true,
88-
89-
//GA4
90-
effective_type: effectiveType,
91-
data_saver: dataSaver,
92-
device_memory: deviceMemory,
93-
prefers_reduced_motion: prefersReducedMotion,
94-
prefers_color_scheme: prefersColorScheme,
95-
navigation_type: navigationType,
96-
}, overrides)
97-
);
148+
const params = Object.assign({
149+
event_category: 'Web Vitals',
150+
value: Math.round(name === 'CLS' ? delta * 1000 : delta),
151+
event_label: id,
152+
non_interaction: true,
153+
effective_type: effectiveType,
154+
data_saver: dataSaver,
155+
device_memory: deviceMemory,
156+
prefers_reduced_motion: prefersReducedMotion,
157+
prefers_color_scheme: prefersColorScheme,
158+
navigation_type: navigationType,
159+
}, overrides);
160+
161+
gtag('event', name, params);
98162

99163
}
100164

src/static/js/webmentions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function renderWebmentions(webmentions) {
235235

236236
// Show count of reactions (except if 0)
237237
if (webmentions.length > 0) {
238-
document.querySelectorAll('.num-reactions').forEach(t => t.innerText = webmentions.length);
238+
document.querySelectorAll('.num-reactions').forEach(t => t.textContent = webmentions.length);
239239
document.querySelectorAll('.reactions-label').forEach(t => setReactionsLabel(webmentions.length, t));
240240
document.querySelector('.webmentions-cta').classList.remove('hidden');
241241
}

src/templates/base/base.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@
808808
</a>
809809
{% if not ebook %}{{ figure_dropdown(metadata, chapter_config, id, sheets_gid, sql_file, image) }}{% endif %}
810810
</div>
811-
<button type="button" hidden="" class="fig-description-button" aria-expanded="false" aria-controls="{{ anchor }}-description" data-show-text="{{ show_description(metadata,chapter_config,id) }}" data-hide-text="{{ hide_description(metadata,chapter_config,id) }}">{{ show_description(metadata,chapter_config,id) }}</button>
812-
<div id="{{ anchor }}-description" class="visually-hidden">{{ description|safe }}</div>
811+
<button type="button" class="fig-description-button novisibility-until-js" aria-expanded="false" aria-controls="{{ anchor }}-description" data-show-text="{{ show_description(metadata,chapter_config,id) }}" data-hide-text="{{ hide_description(metadata,chapter_config,id) }}">{{ show_description(metadata,chapter_config,id) }}</button>
812+
<div id="{{ anchor }}-description" class="hidden">{{ description|safe }}</div>
813813
{%- elif content != "" %}
814814
<div class="figure-wrapper">
815815
<div class="{{ classes }}">{{ content|safe }}</div>
@@ -910,4 +910,21 @@
910910
<!-- A small optimisation but hey, every little helps! -->
911911
<link rel="prefetch" href="{{ get_versioned_filename('/static/css/page.css') }}">
912912
{% endif %}
913+
914+
<script type="speculationrules" nonce="{{ csp_nonce() }}">
915+
{
916+
"prerender": [
917+
{
918+
"source": "document",
919+
"where": {
920+
"and": [
921+
{"href_matches": "/*"},
922+
{"not": {"href_matches": "/static/*"}}
923+
]
924+
},
925+
"eagerness": "moderate"
926+
}
927+
]
928+
}
929+
</script>
913930
{% endblock %}

src/templates/base/base_chapter.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ <h2 id="explore-results">
181181
// Set date format to page's language locale to avoid incorrect date translation.
182182
dateFormat = new Intl.DateTimeFormat("{{ lang }}", options)
183183
}
184-
publishedDateElement.innerHTML = dateFormat.format(publishedDate);
184+
publishedDateElement.textContent = dateFormat.format(publishedDate);
185185

186186
} else {
187187
console.log("Could not format date");

src/templates/base/contributors.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ <h1 class="title title-lg">
483483
}
484484

485485
var filteredCount = getFilteredContributorCount();
486-
counter.innerText = filteredCount;
487-
if ( counter.innerText === totalContributors.innerText ) {
486+
counter.textContent = filteredCount;
487+
if ( counter.textContent === totalContributors.textContent ) {
488488
totalText.classList.add('hidden')
489489
if (totalTextNonFiltered) totalTextNonFiltered.classList.remove('hidden')
490490
} else {

0 commit comments

Comments
 (0)