Skip to content

Speed up search index loading #2633

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions src/front-end/searcher/searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* global Mark, elasticlunr, path_to_root */

window.search = window.search || {};
(function search(search) {
(function search() {
// Search functionality
//
// You can use !hasFocus() to prevent keyhandling in your key
Expand Down Expand Up @@ -288,6 +288,9 @@ window.search = window.search || {};

// If reloaded, do the search or mark again, depending on the current url parameters
doSearchOrMarkFromUrl();

// Exported functions
config.hasFocus = hasFocus;
}

function unfocusSearchbar() {
Expand Down Expand Up @@ -521,6 +524,4 @@ window.search = window.search || {};

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

// Exported functions
search.hasFocus = hasFocus;
})(window.search);
8 changes: 7 additions & 1 deletion src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ pub fn create_files(
if search_config.copy_js {
static_files.add_builtin(
"searchindex.js",
format!("Object.assign(window.search, {});", index).as_bytes(),
// To reduce the size of the generated JSON by preventing all `"` characters to be
// escaped, we instead surround the string with much less common `'` character.
format!(
"window.search = JSON.parse('{}');",
index.replace("\\", "\\\\").replace("'", "\\'")
)
.as_bytes(),
);
static_files.add_builtin("searcher.js", searcher::JS);
static_files.add_builtin("mark.min.js", searcher::MARK_JS);
Expand Down
7 changes: 4 additions & 3 deletions tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,10 @@ mod search {
fn read_book_index(root: &Path) -> serde_json::Value {
let index = root.join("book/searchindex.js");
let index = fs::read_to_string(index).unwrap();
let index = index.trim_start_matches("Object.assign(window.search, ");
let index = index.trim_end_matches(");");
serde_json::from_str(index).unwrap()
let index = index.trim_start_matches("window.search = JSON.parse('");
let index = index.trim_end_matches("');");
// We need unescape the string as it's supposed to be an escaped JS string.
serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap()
}

#[test]
Expand Down