Repository navigation
Stop search from replacing window.postMessage without Web Workers - #106
Merged
Merged
Conversation
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]>
Contributor
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.
In browsers without Web Worker support, the built-in search loads
worker.jsinto the page itself. There,worker.js's barepostMessage(...)calls resolve towindow.postMessage, somain.jsreplaced the page's globalwindow.postMessageto route them. That breaks any other script on the page that uses it, for as long as the page is open.Fix:
worker.jssends its messages through a newsendMessage():postMessageas before;main.js'sonWorkerMessagedirectly.window.postMessageis 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:
<script>element instead of jQuery's$.getScript, because themkdocstheme doesn't include jQuery;base_urlinstead of a hardcoded/search/, so sites served from a subdirectory work. This also fixes the//search_index.jsondouble slash.The regular Web Worker path is unchanged.
Verification: tested in a browser with sites served from a subdirectory. To force the fallback,
window.Workerwas disabled throughextra_javascript. There is no JS test harness in the repository, so there's no automated test.window.postMessagemkdocstheme, no Workerreadthedocstheme, no Workermkdocstheme, with WorkerRelated Issue
No issue in this repository.
Checklist
docs/about/release-notes.mdupdated (if applicable)🤖 Generated with Claude Code