From 52b0a80810bd620bfcdc2df243ed2ff5a4a08f70 Mon Sep 17 00:00:00 2001 From: Dmitry Kokovtsev Date: Thu, 5 Feb 2026 12:06:33 +0300 Subject: [PATCH] fix: gracefully handle page loading failures during search indexing --- src/plugins/search/search.js | 37 +++++++++++++++++++++++++++--------- test/e2e/search.test.js | 32 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 6e3b2e0704..027ff2c5e1 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -325,18 +325,37 @@ export async function init(config, vm) { return count++; } + const saveIfCompleted = () => { + if (len === ++count) { + saveData(config.maxAge, expireKey).catch(err => { + // eslint-disable-next-line no-console + console.warn('Search plugin: failed to save index data', err); + }); + } + }; + Docsify.get(vm.router.getFile(path), false, vm.config.requestHeaders).then( - async result => { - INDEXES[path] = genIndex( + result => { + try { + INDEXES[path] = genIndex( + path, + result, + vm.router, + config.depth, + indexKey, + ); + } finally { + saveIfCompleted(); + } + }, + err => { + // eslint-disable-next-line no-console + console.warn( + 'Search plugin: failed to load a file for indexing', path, - result, - vm.router, - config.depth, - indexKey, + err, ); - if (len === ++count) { - await saveData(config.maxAge, expireKey); - } + saveIfCompleted(); }, ); }); diff --git a/test/e2e/search.test.js b/test/e2e/search.test.js index a99e0121a2..7aaff67705 100644 --- a/test/e2e/search.test.js +++ b/test/e2e/search.test.js @@ -333,4 +333,36 @@ console.log('Hello World'); '...SearchHere to check it!...', ); }); + + test('search should work if some page failed to load', async ({ page }) => { + const docsifyInitConfig = { + markdown: { + homepage: ` + # Hello World + + This is the homepage. + `, + sidebar: ` + - [Test Page](test) + - [Broken Page](broken) + `, + }, + routes: { + '/test.md': ` + # Test Page + + This is a custom route. + `, + }, + scriptURLs: ['/dist/plugins/search.js'], + }; + + const searchFieldElm = page.locator('input[type=search]'); + const resultsHeadingElm = page.locator('.results-panel .title'); + + await docsifyInit(docsifyInitConfig); + + await searchFieldElm.fill('hello'); + await expect(resultsHeadingElm).toHaveText('Hello World'); + }); });