Skip to content
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
37 changes: 28 additions & 9 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try-finally block will not catch errors thrown by genIndex. If genIndex throws an error (e.g., if window.marked.lexer fails or other processing errors occur), the error will propagate as an unhandled promise rejection. This should be a try-catch-finally block instead, with the catch block logging the error similar to how fetch errors are handled in the rejection handler below (lines 351-359). The catch block should log a warning message like "Search plugin: failed to generate index for file" and then the finally block should call saveIfCompleted to ensure indexing completes for other pages.

Suggested change
);
);
} catch (err) {
// eslint-disable-next-line no-console
console.warn(
'Search plugin: failed to generate index for file',
path,
err,
);

Copilot uses AI. Check for mistakes.
} 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();
},
);
});
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});