Repository navigation
fix(build): emit search index as a script so file:// search works - #482
Merged
Merged
Conversation
Browsers give every file:// resource its own opaque origin, so search.js could not fetch() static/search-index.json when the generated site was opened from disk: the request was blocked and search silently returned no results. Browsing was unaffected because links are page-relative. Emit the index as static/search-index.js assigning a global, and load it by injecting a classic <script> on first search. Classic scripts are not CORS-gated, so this restores search over file:// while keeping the index lazy on every other host. Memoising the promise also collapses the duplicate-load race the fetch version had. Closes cooklang#471
The existing e2e suite runs entirely against `cook server`, which takes the /api/search branch in base.html, so the static search path shipped with no browser coverage at all — which is how cooklang#471 reached 0.34.0. Build the seed site to a temp dir and drive search from a file:// URL.
Swap the substring ban on `fetch(` for positive assertions that search.js references search-index.js and reads the global. They catch a revert just as well, cannot misfire on some future legitimate fetch, and remove the hand-rolled comment stripper the negative assertion needed. Parse the emitted assignment tolerantly rather than pinning its exact whitespace, so a formatting change in the producer fails with a useful message instead of "index should assign window.__SEARCH_INDEX__". Collapse the file:// rationale, which had been restated at six sites, to docs/build.md plus short pointers where someone might undo the fix.
An index script that loads but throws fires onload with the global unset, which would reproduce the same silent no-results failure this fix exists to remove. Log it instead, and name the src in both error paths. The empty-state e2e assertion passed against a broken index, since a failed load renders "No recipes found" too; search for a real term first so the empty state means something. docs/build.md claimed file:// works unconditionally, but --base-url emits absolute links that only resolve over HTTP — the whole site, not just search, is unbrowsable from disk with it set.
Member
|
cheers! |
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.
cook build webproduces a site the docs call browsable from disk, but search there returns nothing.static/js/search.jsloads the index withfetch(prefix + "/static/search-index.json"). A page opened overfile://gets the opaque originnull, and Chromium blocks the request:The catch substitutes an empty index, so every query shows "No recipes found". Browsing itself works because page links are relative. Reproduced on 0.34.0 and on current main, with and without
--base-url.Launching with
--allow-file-access-from-filesdoes restore it. That is not something you can ask a reader of a recipe site to do. The index needs a transport that is not origin-gated.Fix
src/build/mod.rswritesstatic/search-index.jsinstead ofstatic/search-index.json. Same JSON payload, wrapped aswindow.__SEARCH_INDEX__ = [...].search.jsappends a classic<script>for it and resolves on load or on error. Script elements are not origin-gated, so the index loads from disk.It has to stay a classic script. Module scripts are CORS-gated and fail over
file://the same way.loadIndexnow caches the in-flight promise rather than the resolved value. The oldindex !== nullguard let two quick keystrokes start two loads.docs/build.mdrecords the new artifact. It also fixes the offline claim two lines above it: with--base-urlevery asset reference is absolute, so from disk the page gets no stylesheet and no search. That was already true before this PR.Result
Search over a site opened straight from disk, seed recipes, no console errors:
The first hit opens over
file://too:Verify
Test plan
tests/e2e/static-file-search.spec.ts, which drives search overfile://: title match ranks first, the result link opens the recipe page, an absent term reports no matches. Fails 3/3 against the old loader, passes 3/3 here.build_writes_search_indexasserts the global assignment and thatsearch-index.jsonis gone; the gzip test follows the renamed artifactsearch_js_loads_index_as_scriptcargo testgreen,cargo clippyclean,cargo fmtCloses #471