Skip to content

fix(build): emit search index as a script so file:// search works - #482

Merged
dubadub merged 4 commits into
cooklang:mainfrom
sripwoud:471
Sep 3, 2026
Merged

dubadub merged 4 commits into
cooklang:mainfrom
sripwoud:471

Conversation

@sripwoud

@sripwoud sripwoud commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

cook build web produces a site the docs call browsable from disk, but search there returns nothing. static/js/search.js loads the index with fetch(prefix + "/static/search-index.json"). A page opened over file:// gets the opaque origin null, and Chromium blocks the request:

Access to fetch at 'file:///…/static/search-index.json' from origin 'null' has been
blocked by CORS policy: Cross origin requests are only supported for protocol schemes:
chrome, chrome-extension, chrome-untrusted, data, http, https, isolated-app.

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-files does 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.rs writes static/search-index.js instead of static/search-index.json. Same JSON payload, wrapped as window.__SEARCH_INDEX__ = [...]. search.js appends 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.

loadIndex now caches the in-flight promise rather than the resolved value. The old index !== null guard let two quick keystrokes start two loads.

docs/build.md records the new artifact. It also fixes the offline claim two lines above it: with --base-url every 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:

search results over file://

The first hit opens over file:// too:

recipe page over file://

Verify

cook build web dist --base-path ./seed
chromium file://$PWD/dist/index.html
# search "Risotto" → Classic Risotto alla Milanese, no console error

Test plan

  • Added tests/e2e/static-file-search.spec.ts, which drives search over file://: 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_index asserts the global assignment and that search-index.json is gone; the gzip test follows the renamed artifact
  • Added search_js_loads_index_as_script
  • cargo test green, cargo clippy clean, cargo fmt
  • Rebased on main after the axum 0.8 and fluent 0.17 upgrades

Closes #471

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.
@dubadub
dubadub merged commit 0234e89 into cooklang:main Sep 3, 2026
6 checks passed
@dubadub

dubadub commented Sep 3, 2026

Copy link
Copy Markdown
Member

cheers!

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.

fix(build): search returns nothing over file:// (search.js fetches index at runtime)

2 participants