Skip to content

fix(search): restore lazy-loaded search bar - #107

Merged
Smana merged 1 commit into
mainfrom
fix/searchbar
Jul 18, 2026
Merged

fix(search): restore lazy-loaded search bar#107
Smana merged 1 commit into
mainfrom
fix/searchbar

Conversation

@Smana

@Smana Smana commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Problem

The site search was completely broken. The lazy-load rework in 8f554ae ("perf: lazy-load search bundle") introduced three bugs in layouts/partials/search/scripts.html:

  1. Double-encoded asset URLs. {{ … | jsonify }} inside a <script> block double-encodes, because Hugo's html/template already JS-escapes values in that context. src/integrity ended up wrapped in literal quotes ("\"http://…\""), so the bundle URL was malformed → 404 → the script never loaded.
  2. Init never fired. search.js self-initializes on the window load event, but the bundle is now injected on first focus/input — after load has already fired — so its handler never ran. Typing did nothing.
  3. Dedicated /search/ page broke. It renders results from the ?query= URL param via passiveSearch, which needs the bundle on page load, not on focus. Deep-links (search submit, result clicks) showed no results.

Fix

  • Drop the redundant jsonify — let Hugo's context-aware escaping produce clean JS strings.
  • Kick off init from the script's onload (guarded by readyState === 'complete'), and replay the current input value since the listener attaches late.
  • Eager-load the bundle on the /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):

Flow Result
Live search (focus + type) — EN home, FR home, EN post list ✅ 8 results each
Search page deep-link — /search/?query=, /fr/search/?query= ✅ 12 results each

The ~30 KB fuse+search bundle still only downloads when the user intends to search (or lands on the search page).

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.
@Smana
Smana merged commit 4973fe4 into main Jul 18, 2026
1 check passed
@Smana
Smana deleted the fix/searchbar branch July 18, 2026 11:32
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.

1 participant