fix(search): restore lazy-loaded search bar - #107
Merged
Merged
Conversation
The lazy-load rework (8f554ae) broke search in three ways: - `jsonify` inside a <script> double-encodes: Hugo's html/template already JS-escapes there, so src/integrity ended up wrapped in literal quotes and the bundle 404'd. - search.js self-initializes on window 'load', but the bundle is now injected on first focus/input, after that event has fired, so init never ran. Trigger init from the script's onload instead, and replay the current input value since the listener attaches late. - The dedicated /search/ page renders results from the ?query= param via passiveSearch and needs the bundle on page load, not on focus. Eager-load it there; keep lazy-loading everywhere else.
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.
Problem
The site search was completely broken. The lazy-load rework in
8f554ae("perf: lazy-load search bundle") introduced three bugs inlayouts/partials/search/scripts.html:{{ … | jsonify }}inside a<script>block double-encodes, because Hugo'shtml/templatealready JS-escapes values in that context.src/integrityended up wrapped in literal quotes ("\"http://…\""), so the bundle URL was malformed → 404 → the script never loaded.search.jsself-initializes on thewindowloadevent, but the bundle is now injected on first focus/input — afterloadhas already fired — so its handler never ran. Typing did nothing./search/page broke. It renders results from the?query=URL param viapassiveSearch, which needs the bundle on page load, not on focus. Deep-links (search submit, result clicks) showed no results.Fix
jsonify— let Hugo's context-aware escaping produce clean JS strings.onload(guarded byreadyState === 'complete'), and replay the current input value since the listener attaches late./search/page (detected via#searchpage); keep lazy-loading everywhere else, preserving the original perf goal.Verification
Driven end-to-end in headless Chromium against a production build (cold cache):
/search/?query=,/fr/search/?query=The ~30 KB fuse+search bundle still only downloads when the user intends to search (or lands on the search page).