Skip to content

Stop search from replacing window.postMessage without Web Workers - #106

Merged
shenxianpeng merged 2 commits into
mainfrom
fix/search-postmessage-fallback
Sep 24, 2026
Merged

shenxianpeng merged 2 commits into
mainfrom
fix/search-postmessage-fallback

Conversation

@shenxianpeng

Copy link
Copy Markdown
Member

In browsers without Web Worker support, the built-in search loads worker.js into the page itself. There, worker.js's bare postMessage(...) calls resolve to window.postMessage, so main.js replaced the page's global window.postMessage to route them. That breaks any other script on the page that uses it, for as long as the page is open.

Fix: worker.js sends its messages through a new sendMessage():

  • inside a Web Worker, it calls postMessage as before;
  • in the page, it calls main.js's onWorkerMessage directly.

window.postMessage is no longer touched.

This fallback path didn't work anyway, so without further changes the fix couldn't be verified. Two more changes make it work, and they only affect the fallback path:

  • scripts are loaded with a <script> element instead of jQuery's $.getScript, because the mkdocs theme doesn't include jQuery;
  • search files are resolved against the theme's base_url instead of a hardcoded /search/, so sites served from a subdirectory work. This also fixes the //search_index.json double slash.

The regular Web Worker path is unchanged.

Verification: tested in a browser with sites served from a subdirectory. To force the fallback, window.Worker was disabled through extra_javascript. There is no JS test harness in the repository, so there's no automated test.

Scenario Path Search for "while" window.postMessage
mkdocs theme, no Worker fallback results native
readthedocs theme, no Worker fallback results native
mkdocs theme, with Worker worker results native

Related Issue

No issue in this repository.

Checklist

  • New tests added for new behavior (if applicable) — no JS test harness; verified in a browser
  • Documentation updated (if applicable)
  • Release notes docs/about/release-notes.md updated (if applicable)

🤖 Generated with Claude Code

shenxianpeng and others added 2 commits September 24, 2026 21:04
In browsers without Web Worker support, search loads worker.js into the
page itself. worker.js reported back with a bare `postMessage(...)`,
which there resolves to `window.postMessage`, so main.js replaced the
page's global `window.postMessage` to route those calls. That broke any
other script on the page relying on it.

worker.js now sends messages through `sendMessage()`, which uses
`postMessage` inside a worker and calls main.js's `onWorkerMessage`
directly in the page. `window.postMessage` is left alone.

The fallback didn't work anyway, so this also makes it work:

- load scripts with a `<script>` element instead of jQuery's
  `$.getScript` (the `mkdocs` theme doesn't include jQuery);
- resolve the search files against the theme's `base_url` instead of a
  hardcoded `/search/`, so sites served from a subdirectory work.

The regular Web Worker path is unchanged.

Co-Authored-By: Claude <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Sep 24, 2026
@codspeed

codspeed Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 28 untouched benchmarks


Comparing fix/search-postmessage-fallback (bfe83e7) with main (d18aa42)

Open in CodSpeed

@shenxianpeng
shenxianpeng merged commit 5fd5096 into main Sep 24, 2026
23 checks passed
@shenxianpeng
shenxianpeng deleted the fix/search-postmessage-fallback branch September 24, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant