From a0eb8c0a0e8ac883860a2e7342c39563d4827192 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 20 Feb 2025 23:07:16 +0100 Subject: [PATCH 1/3] Remove JSON search file --- src/renderer/html_handlebars/search.rs | 1 - src/theme/searcher/searcher.js | 24 +++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index d3f67fed13..a47b6d2a53 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -64,7 +64,6 @@ pub fn create_files( } 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(), diff --git a/src/theme/searcher/searcher.js b/src/theme/searcher/searcher.js index a2392f3d17..bb2cf0b10b 100644 --- a/src/theme/searcher/searcher.js +++ b/src/theme/searcher/searcher.js @@ -363,7 +363,7 @@ window.search = window.search || {}; } } } - + function showSearch(yes) { if (yes) { search_wrap.classList.remove('hidden'); @@ -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; } @@ -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; From 418d6775849a004e2fe10786b14630b48dc8be31 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 22 Mar 2025 17:49:30 +0100 Subject: [PATCH 2/3] Improve warning message when search index is too big --- src/renderer/html_handlebars/search.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index a47b6d2a53..ffa104b3e6 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -60,7 +60,7 @@ 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 { From f54356da1067d39a041c92516ee0b17d4a8702cd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 22 Mar 2025 17:51:07 +0100 Subject: [PATCH 3/3] Remove `fail-on-request-error` in GUI tests as they are not needed anymore --- tests/gui/move-between-pages.goml | 3 +-- tests/gui/search.goml | 3 --- tests/gui/sidebar-nojs.goml | 3 --- tests/gui/sidebar.goml | 3 --- 4 files changed, 1 insertion(+), 11 deletions(-) diff --git a/tests/gui/move-between-pages.goml b/tests/gui/move-between-pages.goml index 9177b36433..2c70730029 100644 --- a/tests/gui/move-between-pages.goml +++ b/tests/gui/move-between-pages.goml @@ -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" diff --git a/tests/gui/search.goml b/tests/gui/search.goml index dd386d0d44..dd362bd138 100644 --- a/tests/gui/search.goml +++ b/tests/gui/search.goml @@ -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: ( diff --git a/tests/gui/sidebar-nojs.goml b/tests/gui/sidebar-nojs.goml index 9a8622f94a..05bbd65a52 100644 --- a/tests/gui/sidebar-nojs.goml +++ b/tests/gui/sidebar-nojs.goml @@ -2,9 +2,6 @@ // an iframe (because of JS disabled). // Regression test for . -// 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" diff --git a/tests/gui/sidebar.goml b/tests/gui/sidebar.goml index 7e4ec32b45..61dded10c0 100644 --- a/tests/gui/sidebar.goml +++ b/tests/gui/sidebar.goml @@ -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.