Skip to content

Defer jQuery, and run the inline code that needs it from whenReady - #304

Open
r0ny123 wants to merge 31 commits into
familiary:masterfrom
r0ny123:perf/defer-jquery
Open

r0ny123 wants to merge 31 commits into
familiary:masterfrom
r0ny123:perf/defer-jquery

Conversation

@r0ny123

@r0ny123 r0ny123 commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #245, which is stacked on #126. Merge those first; until then this diff also shows their commits. This PR's own are the last two: 5f2f996, which merges #245's new head, and 901ac33.

The push that opens this PR also moves #126 from e712634 to 4c80af2, and #245 from 05a082e to 8cba370, which merges it. 4c80af2 renames #126's second ADR to 0017, since master has its own 0016, and points ADR-0015's status line at the new number. This PR's ADR is 0018.

Closes #247, and is part of #63. jQuery was the one script left in every page's <head> that held back the first paint. #126 and #245 deferred Bootstrap and the project's own scripts, but jQuery stayed a plain script because inline blocks called $ while the page parsed. This defers jQuery, jQuery UI and SortableJS, and runs the inline code that needs them from a helper that waits for them. ADR-0018 records the audit, the rule for templates and what it costs.

Change.

  • base.html defines whenReady(fn) above every other script. It waits for DOMContentLoaded and then hands fn to jQuery, so ready handlers keep the timing and order they had. The upload pages depend on that: mcritInitDropzone needs Dropzone's own DOMContentLoaded listener to have run first.
  • 21 places in 15 templates called $ while the page parsed. They now run from whenReady: 16 $(document).ready, 2 $(fn), and 3 top-level bindings. So do the settings page's new Sortable(...) calls.
  • jquery.js, and the jQuery UI and SortableJS that result_cross and settings load in {% block style %}, are deferred. Bootstrap moves below that block, so it still runs after jQuery UI and owns $.fn.tooltip, which copy_to_clipboard relies on.
  • Between the first paint and DOMContentLoaded, a click can now reach a page whose jQuery has not run:
    • The pickers' search forms (compare, compare_versus's two, cross_compare, unique_blocks) are bound to document while the page parses. An early Enter keeps the selection and the settings instead of submitting the bare form.
    • fetchState reads its checkboxes with getElementById, and deleteSample finds its row with closest("tr"), so the pagination, sort, Add, Compare and remove links work before jQuery has.
    • The sample list's ready handler starts from a cleared Analyze flag. One set before it existed used to turn the next row click into a menu.
    • The upload radios leave their fields alone until jQuery has run. The ready handler then shows the fields for SMDA as well as Dumped.
    • The navbar's Analyze and Data menus link a signed-in user to #, as Explore and the user menu do. They linked to "", so an early click reloaded the page.
  • What still waits for DOMContentLoaded is listed in the ADR. A click on a menu, tab, accordion or dialog there does nothing and is not replayed; the row clicks and the type-ahead are not there yet either.

Verification. On the testbed, #245's new head (8cba370) and this branch as side instances against the live backend, logged in as analyst1. Apart from the login, every request was a GET.

Lighthouse 12.8.2, mobile, the two sides run in turn page by page, with nothing else running. The branch side was 63b75c7, which 901ac33 amends only in the ADR's text. Each figure is the median of the runs:

page runs Performance FCP LCP TBT
/ 3 96 -> 96 1.66 -> 1.39 s 2.73 -> 2.74 s 3 -> 5 ms
/explore/samples 3 91 -> 92 2.09 -> 1.84 s 3.18 -> 3.18 s 9 -> 15 ms
/explore/samples/1 3 92 -> 93 1.83 -> 1.85 s 3.09 -> 3.03 s 0 -> 5 ms
/explore/functions/83 7 80 -> 79 1.94 -> 1.87 s 4.76 -> 4.74 s 161 -> 196 ms
1vN result 6ab3b4455ccfc2538ffea9b6 7 86 -> 85 2.59 -> 2.76 s 3.63 -> 3.78 s 21 -> 7 ms

jquery.js leaves the render-blocking list on all five pages, and the first paint of the index and the samples list comes about a quarter of a second earlier. The Performance score stays within the spread of its runs, which is up to six points. The work jQuery did before the first paint now happens after it, where Lighthouse counts it as blocking time, most visibly on the function page. Desktop is 98-100 on these pages on both sides. The other three categories do not change. #245 had named jQuery as what kept mobile from 100; this does not get there on its own, and the ADR lists what the score still loses.

In the browser, with jquery.js held back for three seconds:

  • On both sides, about 430 controls on 26 pages were clicked or typed into before jQuery ran. The sweep could not find some 45 of them on either side's page, and it covers only the controls it found on both. The searches, sort links, remove buttons and Add kept the pickers' state on every picker, and none ran twice after release.
  • Two controls misbehaved, and each now has a fix and a test: an early Dumped or SMDA radio threw $ is not defined, and an early click on the navbar's Analyze or Data menu reloaded the page.
  • After a normal load, Bootstrap owns $.fn.tooltip on the cross-compare result (20 of 20 clipboard buttons), the sortable list reorders, the drop overlay opens, and the type-ahead works.

Tests.

  • tests/testParseTimeScripts.py (210 tests) reads every template, and every page the accessibility checks render, for an inline script that reaches $, jQuery, bootstrap, Sortable, Autocomplete, csrfToken, postTo or mcritwebPageLoading while the page parses. It also fails on a script in <head> that is not deferred, and on a plain script that needs jQuery. Its scanner knows strings, comments, regex literals and which function bodies run at once; it follows a name only to a function declaration, and its own tests hold it to that.
  • tests/testDeferredJqueryBrowser.py (56 tests) loads those pages in Chromium and fails on any page error, and on Bootstrap not owning $.fn.tooltip. With jquery.js held back, it checks:
    • the pagination and sort links, the remove buttons and the Analyze flag;
    • the SMDA fields and the navbar menus;
    • a search on each of the five picker forms, before jQuery has run and after, counting navigations and helper calls so that a handler bound twice shows.
  • tests/testPageAssets.py pins the defers and the order of the deferred scripts, and tests/testAssetDiet.py checks that the upload pages wire Dropzone from whenReady.
  • Taking out a fix makes a test fail: fetchState back on jQuery, deleteSample back on jQuery, the flag reset, the SMDA check, a search listener bound through jQuery or bound twice, the radios' jQuery guard, and href="" on the navbar menus.
  • On Stop the remaining head scripts and help screenshots from delaying first paint (#63, stacked on #126) #245's code (8cba370), 95 of the 307 new or changed tests fail. Those that pass there too are guards: five pages load without an error with Bootstrap owning its plugins, and a dragged file opens the drop overlay.
  • Full suite with Chromium on 901ac33: 2417 passed, none skipped; ruff check . is clean.

Relation to open PRs. A trial merge (git merge-tree) with each open PR head. #245's head already conflicts with #130, #176, #253, #254, #264, #266 and the rebuilt #160. This PR adds these conflicts:

A local build of master with #126, #245, #253, #302, #303, the rebuilt #160 and this PR passes testParseTimeScripts.py and testUiParity.py, with these resolutions and the two testUiParity.py changes #160's text describes. It still passes once #268, #291 and #292 are added.

r0ny123 added 27 commits August 30, 2026 04:44
Issue #63, first bullet: "remove data-tables js/css once it is not needed
anymore". base.html loaded 623 KB of render-blocking JavaScript and 41 KB of
CSS on every page in the app, for two calls.

DataTables is removed outright, not moved. Its one initialiser selected
`#job-table`, and the jobs page renders its table with `table_id=active` -
the job method - so the selector matched nothing on every request. Confirmed
against the running app: `id="job-table"` appears in zero of /data/jobs,
?active=..., ?state=... and /. It was also configured with paging, searching
and info all off, so even where it had matched it would only have restyled
the table.

jQuery UI moves to result_cross.html, the only page that calls `.sortable()`.
{% block style %} runs after base.html's scripts, so jQuery is in place first.

Measured with Chromium, total decoded bytes per page:

    /                     1667 KB -> 1017 KB
    /explore/samples      1667 KB -> 1017 KB
    /data/jobs            1667 KB -> 1017 KB
    /data/result/<cross>  1691 KB -> 1595 KB   (keeps jQuery UI)

Behaviour is unchanged: `.sortable()` still applies on the cross compare, the
clipboard tooltips still get Bootstrap instances rather than jQuery UI's -
both libraries define `.tooltip()`, and removing jQuery UI makes which one
wins deterministic rather than a load-order race - and no page logs a console
error.

Deliberately not done: `defer` on the remaining scripts, and removing jQuery.
Both need an audit of 23 inline blocks and 36 onclick handlers, several of
which call functions declared at the top level of those blocks; that is a
separate change. The remaining majority of the render-blocking weight is CSS,
not JS - bootstrap.css alone is 196 KB and is the unminified build.
Every unit job on every Python version fails with:

    /opt/hostedtoolcache/Python/3.11.16/x64/bin/python: No module named pytest

The Makefile said "pytest and pytest-cov arrive with mcrit, so requirements.txt
covers both". mcrit declares them under its `dev` extra:

    $ python -c "import importlib.metadata as m; print(m.requires('mcrit'))"
    ... 'pytest; extra == "dev"', 'pytest-cov; extra == "dev"' ...

so a plain `pip install -r requirements.txt` does not bring them in, and the
suite has not actually run in CI since mcrit moved them there. Ruff is green
throughout, which is why this looked like a working pipeline.

Installed in the workflow rather than added to requirements.txt: neither is a
runtime dependency and a deployment should not get them. Pinned, for the same
reason the lint job pins ruff. The two stale comments that said otherwise are
corrected.
`init` still ran `pip install -r requirements.txt` alone, so a fresh checkout
that follows the README lands without pytest and `make test` fails with
"No module named pytest" - the same failure the CI change on this branch fixes
for the runner, left unfixed for a contributor.

pytest and pytest-cov are not runtime dependencies and are not in
requirements.txt; mcrit declares them under its `dev` extra, which nothing
here requests, so they do not arrive transitively either. Install them from
`init`, pinned to the versions CI uses, and say so once above the target
rather than in a comment further down that describes a step that never ran.

Reported by Codex review.
…rst one was safe

The previous commit did #63's first bullet. The other four are "consider", "enable
text compression" and "what to inline"; a recorded, measured decision is the
resolution for those, so ADR-0015 makes them. Numbers first, all from Chromium
driving the app over real HTTP against the captured corpus.

  * text compression is the largest win left, and it is not ours. Nothing is
    compressed anywhere: Flask's static handler does not, and neither does the
    reference deployment - nginx.conf, mcritweb_plain.conf and mcritweb_ssl.conf in
    danielplohmann/docker-mcrit at 7e9501a carry no gzip directive, and location /
    proxies /static/ straight through. Simulating `gzip on;` in front of this branch,
    cold Fast-3G load of /, median of 7: first paint 3872 ms -> 1024 ms, wire
    1,069,548 B -> 354,002 B. That is more than the whole of the first bullet, and it
    belongs in docker-mcrit.

  * lazy dropzone: right target, wrong shape today. dropzone.js is 353,924 B, 38% of
    every page, but it loads at the end of <body>, so blocking it outright - an upper
    bound - moves first paint by 8 ms. It buys DOMContentLoaded (1585 -> 1009 ms) and
    23% of the transfer. The cost is Flask-Dropzone: dropzone.config() emits
    Dropzone.options.myDropzone *after* the library and only works because discovery
    waits for DOMContentLoaded. Injected late, contentLoaded() sees readyState
    "complete" and binds the element with default options - no CSRF header, no
    autoProcessQueue: false. Doing it right means replacing create()/config() by hand
    in the one place static/dropzone.js's local patch has to keep working.
    "autocomplete on mouseover" is 4,621 B, 1,507 gzipped: no.

  * removing jQuery: no. 15 of 26 inline blocks call $, 36 inline event attributes
    hang off them, trace_CFG/main.js and main_duo.js expect it from base.html, jQuery
    UI cannot load without it, and .tooltip()/.modal() go through Bootstrap's jQuery
    bridge. 89,503 B, 30,882 gzipped - under 9% of a compressed page.

  * inline vs reference is already right: 7,141 B of inline JS against 902,940 B of
    referenced library. The remaining CSS weight (bootstrap.css is the unminified
    build) compresses 8x, so gzip answers that too.

ADR-0004 asked #63 to assert rather than assume which library owns $.fn.tooltip, and
called the current arrangement a load-order race. Measured, it is not one. Bootstrap
5.0.2 defines its jQuery plugins on DOMContentLoaded, after every plain <script> in
the document, so it wins the name in both arrangements - jQuery UI before it, as
base.html used to, and after it, as result_cross.html now does. Checked on every page
that renders a [data-toggle="tooltip"] target: $.fn.tooltip.Constructor.NAME is
"tooltip", bootstrap.Tooltip.getInstance(el) is set, $(el).data("ui-tooltip") is not.
What does flip it is loading jQuery UI *after* DOMContentLoaded, which is exactly what
a lazy loader would do - so the hazard is lazy loading, not ordering.
test_bootstrap_is_loaded_synchronously pins that.

The one factual correction: the previous commit said DataTables' initialiser "selected
an element the app never renders". The app does render id="job-table" - job_table
still defaults to it and job_overview and single_sample take the default, confirmed on
/explore/samples/1. What made DataTables dead is narrower: the initialiser lived on
the jobs page, which passes table_id=active. The removal stands; the reason in the
comments and the test docstring did not, and now says what was measured.

New tests, each mutation-checked by breaking the code it guards:

  TestTheJobsPage             - none of the three jobs views renders id="job-table"
  TestTheCrossComparePage     - the rendered page ships jquery-ui, jquery.js reaches
                                it first, and no other page ships it at all
  test_only_base_html_loads_a_jquery      - both CFG templates' second jQuery stays
                                            commented out
  test_bootstrap_is_loaded_synchronously  - no defer/async on the bundle

code_of() now strips HTML comments as well as Jinja ones, which is what makes the
second-jQuery check see the commented-out includes as absent rather than present.
…zone, gzip

Font Awesome is reduced to the icons the templates use, Bootstrap ships
minified and deferred, the Dropzone script and stylesheet load on the first
drag or on opening the modal, and responses over 1 KiB of compressible types
leave gzipped (MCRITWEB_COMPRESS_RESPONSES turns it off behind a proxy that
compresses itself).
ADR-0015 had left compression to the proxy and the other bullets to a later
audit. Both happened; 0015 is marked superseded and 0016 holds the
measurements and the rules that follow from them.
The type-ahead conflicts are the ones that matter. This branch's side of
js/ac_family_names.html and the submit dropzone still carried the old
family-name code - names shipped with |tojson and pushed unescaped into a
widget that renders suggestions through innerHTML, which is the injection
master closed in familiary#168 (and familiary#146 moved to a JSON endpoint). Master's
escaped code is kept in both places; what this branch contributes there
is only its reason to change them, that Bootstrap is now deferred, so the
widgets are built once the deferred scripts have run. In
ac_family_names.html the DOMContentLoaded listener is also the scope,
replacing master's IIFE, so this branch's timing test can see it. The
Playwright tests that drive the real widget pass with Bootstrap deferred.

The rest:
- base.html: the minified Bootstrap and the Font Awesome subset, with
  master's signed-in font preload (#62) - the subset loads the same
  ../webfonts/fa-solid-900.woff2 the preload names; Bootstrap and
  post_action.js deferred, page_loading.js kept a plain script as master
  has it; clearDropzone keeps the lazy-load guard with master's const.
- the dropzone keeps mcritInitDropzone(), with master's const myDropzone
  (#61) - nothing outside that function uses it.
- import.html takes master's import status script, with dropzone.min.js.
- README keeps the compression paragraph and master's reverse-proxy
  section.
- the Font Awesome subset is regenerated for three icons master's
  templates added (arrows-rotate, code, diagram-project), nothing else
  changes in it; the subset script lists the webfont file stems as
  non-glyphs, since master's preload and its comment name
  fa-solid-900.woff2 and the icon scan read that as an icon.

1026 passed, ruff clean.
autocomplete.js and page_loading.js were plain scripts in <head>, so they
blocked first paint although nothing calls them while the page parses:
page_loading.js only registers event listeners, and every `new Autocomplete`
already waits for DOMContentLoaded, which fires after all deferred scripts
have run. Both are now deferred alongside Bootstrap and post_action.js.
jQuery stays a plain script because inline blocks call `$` while parsing.
The single function and function comparison pages loaded d3, graphlib,
graphlib-dot, dagre-d3 and convexhull.js as plain scripts in <head>, about
120 kB gzipped (390 kB raw) that held back the first paint although
nothing uses them before the CFG scripts at the end of the page and
window.onload. They now load immediately before main.js / main_duo.js, in
the same order, so every script that uses them still runs after them.
Lighthouse's desktop run of the comparison page no longer lists them as
render-blocking.
/help shows 21 screenshots, about 2 MB, and the browser fetched all of
them on load, so the first one - the page's largest contentful paint -
shared the connection with twenty images below the fold. Every screenshot
after the first now carries loading="lazy". The first stays eager:
lazy-loading the largest paint element would delay it.
With loading="lazy" and no width or height, a screenshot is a 2px placeholder
until it arrives. The browser scrolls to /help#jobs-queue, the screenshots now
in view load and grow, and the heading ends up 4,782px below the window (1400x900)
and stays there. Every section link past the first few screenshots missed.

manual.py reads each PNG's size from its header and writes it on the <img>.
With height: auto in style.css the space is reserved before the image loads,
and the image still scales down with the column. Every anchor now lands at the
top of the window, at 1400x900 and 800x700, and only the screenshots in view
download.
… into fix/63-page-specific-libraries

Master replaced the family names shipped with each page by a type-ahead that
fetches them as they are typed (familiary#192), and the submit dropzone now includes
js/ac_family_names.html instead of building its own list. Kept master's
widget and this branch's reason to touch it: Bootstrap is deferred, so the
widgets are built in a DOMContentLoaded listener rather than master's IIFE.
The dropzone macro takes master's signature without `families` and keeps
`lazy`, which base.html's overlay still passes.
The lazy loader, the query and submit pages and the import page loaded
static/dropzone.min.js, which is stock Dropzone 5.9.2. Only dropzone.js
carries the patch that sends the head of a dropped file, so a drop posted
no file_header and the submit form was no longer pre-filled. All three
load dropzone.js again; dropzone.min.js is the retired file now.

A failed fetch of the script left mcritDropzoneLoading set, so the overlay
never opened again on that page. It is cleared on error, and the next drag
tries again without adding a second stylesheet. A dragover while the
script is on its way no longer queues the same callback again. The two
URLs go into the script through |tojson.
send_file answers If-None-Match against the plain ETag before the
after_request hook renames it, so a client revalidating its gzipped copy
never matched and downloaded every asset again. The conditional check is
made again against the renamed ETag.

HTML is no longer compressed. A page carries the session's CSRF token, the
same in every response, next to input it reflects, which is what a BREACH
attack needs. Downloads, the /api passthrough, bodies of unknown length
and bodies over 1 MiB are left alone too: the exports and the cached
result download are streamed so that they never sit in memory whole, and
reading them in to gzip them undid that.

gzip;q=0 is a refusal; request.accept_encodings reads it as one. The
README said docker-mcrit's NGINX compresses; its configuration has no
gzip directive (ADR 0015).
…irect in its test

Master's templates now use fa-pen, which the Font Awesome subset did not carry, so
scripts/subset_fontawesome.py is rerun. /data/jobs now redirects to the URL that
names its first tab, so the DataTables id check follows the redirect to the page
that is actually rendered.
Batch C brought master docs/adr/0016-slow-function-search-is-mcrits.md,
so this branch's 0016 shared the number. The record moves to 0017 and
ADR-0015's status line points at it; nothing else names the file.
jQuery was the one script in every page's head that still held back the
first paint: inline blocks called $ while the page parsed, so 0017 left it
a plain script. base.html now defines whenReady(fn), which waits for
DOMContentLoaded and hands fn to jQuery, so ready handlers keep the timing
and order they had. The 21 parse-time uses of $ in 15 templates, and the
settings page's Sortable calls, run from it, and jQuery, jQuery UI and
SortableJS are deferred. Bootstrap loads below {% block style %}, so that it
still owns $.fn.tooltip over jQuery UI on the cross compare.

Between the first paint and DOMContentLoaded a click can now reach a page
whose jQuery has not run:
- the pickers' search forms are bound to document while the page parses,
  so an early Enter keeps the selection and the settings;
- fetchState reads its checkboxes and deleteSample finds its row without
  jQuery;
- the sample list's Analyze flag starts cleared;
- the upload radios leave their fields to the ready handler, which now
  also shows them for SMDA;
- the navbar's Analyze and Data menus link to "#" instead of reloading
  the page.

ADR-0018 records the audit, the rule for templates and what it costs.
testParseTimeScripts fails on a parse-time use of anything a deferred
script defines, and testDeferredJqueryBrowser drives the pages with
jquery.js held back.
Each screenshot carries the pixel size of its file as width and height, but the
page is box-sizing: border-box and a screenshot has a 1px border, so the
picture inside was 2px narrower than its file (1265px for a 1267px file) and
was resampled. The border now goes around the size, and the largest width the
column allows loses the 2px, so a screenshot that does not fit still shrinks to
the column. The room kept for it before it loads is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Defer jQuery so it stops blocking the first paint on mobile

1 participant