Skip to content

Remove JSON search file #2552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ pub fn create_files(
let index = write_to_json(index, search_config, doc_urls)?;
debug!("Writing search index ✓");
if index.len() > 10_000_000 {
warn!("searchindex.json is very large ({} bytes)", index.len());
warn!("search index is very large ({} bytes)", index.len());
}

if search_config.copy_js {
static_files.add_builtin("searchindex.json", index.as_bytes());
static_files.add_builtin(
"searchindex.js",
format!("Object.assign(window.search, {});", index).as_bytes(),
Expand Down
24 changes: 13 additions & 11 deletions src/theme/searcher/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ window.search = window.search || {};
}
}
}

function showSearch(yes) {
if (yes) {
search_wrap.classList.remove('hidden');
Expand Down Expand Up @@ -441,7 +441,6 @@ window.search = window.search || {};
}

function doSearch(searchterm) {

// Don't search the same twice
if (current_searchterm == searchterm) { return; }
else { current_searchterm = searchterm; }
Expand All @@ -468,15 +467,18 @@ window.search = window.search || {};
showResults(true);
}

fetch(path_to_root + '{{ resource "searchindex.json" }}')
.then(response => response.json())
.then(json => init(json))
.catch(error => { // Try to load searchindex.js if fetch failed
var script = document.createElement('script');
script.src = path_to_root + '{{ resource "searchindex.js" }}';
script.onload = () => init(window.search);
document.head.appendChild(script);
});
function loadScript(url, id) {
const script = document.createElement('script');
script.src = url;
script.id = id;
script.onload = () => init(window.search);
script.onerror = error => {
console.error(`Failed to load \`${url}\`: ${error}`);
};
document.head.append(script);
}

loadScript(path_to_root + '{{ resource "searchindex.js" }}', 'search-index');

// Exported functions
search.hasFocus = hasFocus;
Expand Down
3 changes: 1 addition & 2 deletions tests/gui/move-between-pages.goml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This tests pressing the left and right arrows moving to previous and next page.

// We disable the requests checks because `searchindex.json` will always fail
// locally (due to CORS), but the searchindex.js will succeed.
// We disable the requests checks because `mode-rust.js` is not found.
fail-on-request-error: false

go-to: |DOC_PATH| + "index.html"
Expand Down
3 changes: 0 additions & 3 deletions tests/gui/search.goml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// This tests basic search behavior.

// We disable the requests checks because `searchindex.json` will always fail
// locally (due to CORS), but the searchindex.js will succeed.
fail-on-request-error: false
go-to: |DOC_PATH| + "index.html"

define-function: (
Expand Down
3 changes: 0 additions & 3 deletions tests/gui/sidebar-nojs.goml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// an iframe (because of JS disabled).
// Regression test for <https://github.com/rust-lang/mdBook/issues/2528>.

// We disable the requests checks because `searchindex.json` will always fail
// locally.
fail-on-request-error: false
// We disable javascript
javascript: false
go-to: |DOC_PATH| + "index.html"
Expand Down
3 changes: 0 additions & 3 deletions tests/gui/sidebar.goml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// This GUI test checks sidebar hide/show and also its behaviour on smaller
// width.

// We disable the requests checks because `searchindex.json` will always fail
// locally.
fail-on-request-error: false
go-to: |DOC_PATH| + "index.html"
set-window-size: (1100, 600)
// Need to reload for the new size to be taken account by the JS.
Expand Down