perf(paste): skip the mode bundle on plaintext pastes, lazy-load the QR lib - #9
Merged
Conversation
β¦QR lib The paste page scored 87 with LCP at 4.0s, shipping 515 KB. Three fixes, none of which change what a reader sees. The ~190 KB CodeMirror mode bundle loaded on every paste, including pastes with no language β which have no mode to apply β and languages with no CodeMirror mode at all, like makefile. It is now conditional on the paste actually having a mode. Deferring the remaining scripts is safe because content is decrypted client-side after load, so no mode is needed at parse time. qrcode.min.js loaded on every paste for a modal most readers never open. It is now fetched on first click, carrying the same SRI hash it had as a static tag. The modal opens immediately and fills in when the lib lands. Two dead files removed from static/, both served publicly by the StaticFiles mount and referenced by nothing: highlight.min.js (125 KB, superseded by CodeMirror) and purify.min.js.sha256, which despite its name is not a checksum but DOMPurify's 95 KB source map, exposing the full unminified source. Measured with Lighthouse mobile, median of 3 runs: plaintext paste 87 -> 95 LCP 4045ms -> 2961ms 515 KB -> 307 KB python paste 87 -> 88 LCP 4076ms -> 3913ms 515 KB -> 495 KB Verified in a real browser on both paste types: content decrypts, python still highlights while plaintext correctly does not, the mode bundle is fetched only for python, the QR lib is fetched only on click and renders, and no CSP or SRI error is raised.
Both branches appended tests to test_api.py; kept both sets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contexte
The paste page scored 87 with LCP at 4.0 s, shipping 515 KB β the same profile as the landing page fixed in #7, and arguably the more visited page.
Changements
The mode bundle is now conditional.
codemirror-modes.min.js(~190 KB) loaded on every paste, including pastes with no language β which have no mode to apply β and languages that map to no CodeMirror mode at all, such asmakefile. It now loads only when the paste actually has a mode. The remaining scripts pick updefer, which is safe because content is decrypted client-side after load, so no mode is needed at parse time.The QR library is lazy.
qrcode.min.js(21 KB) loaded on every paste for a modal most readers never open. It is now fetched on first click, carrying the same SRI hash it had as a static tag. The modal opens immediately and fills in when the lib lands; on failure it stays empty rather than blocking the reader.Two dead files removed from
static/, both referenced by nothing and both served publicly by theStaticFilesmount:highlight.min.jsβ 125 KB, superseded by CodeMirror.purify.min.js.sha256β despite the name this is not a checksum. It is DOMPurify's source map ({"version":3,"file":"purify.min.js","sources":[...]}), 95 KB, exposing the full unminified source at a predictable URL. Worth a look at how it got committed under that name, in case the same slip affects a vendored file elsewhere.Tests
Lighthouse mobile, median of 3 runs, same paste URL before and after:
The plaintext case is where the win lands, which is the point: a python paste genuinely needs the bundle, a plaintext one never did.
Browser-verified on both paste types:
116 tests pass. One existing test caught the QR change and was updated rather than deleted β it now asserts the lib is not loaded eagerly and that the injected URL still carries SRI. One test added for the conditional mode bundle.
ruff check/ruff format --checkclean.Risques
The conditional mode bundle is the change to review: if
cm_mode_mapever gains an entry whose value is falsy for a language that does need highlighting, that paste silently renders unhighlighted. Today the map has exactly two such entries (""andmakefile), both correct, and the new test pins the behaviour at both ends.Deleting
purify.min.js.sha256assumes nothing external links to it. It is unreferenced in this repo; if some deployment or doc points at it, that link breaks.