Skip to content

Commit 52b0a80

Browse files
committed
fix: gracefully handle page loading failures during search indexing
1 parent 7e29ca0 commit 52b0a80

2 files changed

Lines changed: 60 additions & 9 deletions

File tree

src/plugins/search/search.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,37 @@ export async function init(config, vm) {
325325
return count++;
326326
}
327327

328+
const saveIfCompleted = () => {
329+
if (len === ++count) {
330+
saveData(config.maxAge, expireKey).catch(err => {
331+
// eslint-disable-next-line no-console
332+
console.warn('Search plugin: failed to save index data', err);
333+
});
334+
}
335+
};
336+
328337
Docsify.get(vm.router.getFile(path), false, vm.config.requestHeaders).then(
329-
async result => {
330-
INDEXES[path] = genIndex(
338+
result => {
339+
try {
340+
INDEXES[path] = genIndex(
341+
path,
342+
result,
343+
vm.router,
344+
config.depth,
345+
indexKey,
346+
);
347+
} finally {
348+
saveIfCompleted();
349+
}
350+
},
351+
err => {
352+
// eslint-disable-next-line no-console
353+
console.warn(
354+
'Search plugin: failed to load a file for indexing',
331355
path,
332-
result,
333-
vm.router,
334-
config.depth,
335-
indexKey,
356+
err,
336357
);
337-
if (len === ++count) {
338-
await saveData(config.maxAge, expireKey);
339-
}
358+
saveIfCompleted();
340359
},
341360
);
342361
});

test/e2e/search.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,36 @@ console.log('Hello World');
333333
'...SearchHere to check it!...',
334334
);
335335
});
336+
337+
test('search should work if some page failed to load', async ({ page }) => {
338+
const docsifyInitConfig = {
339+
markdown: {
340+
homepage: `
341+
# Hello World
342+
343+
This is the homepage.
344+
`,
345+
sidebar: `
346+
- [Test Page](test)
347+
- [Broken Page](broken)
348+
`,
349+
},
350+
routes: {
351+
'/test.md': `
352+
# Test Page
353+
354+
This is a custom route.
355+
`,
356+
},
357+
scriptURLs: ['/dist/plugins/search.js'],
358+
};
359+
360+
const searchFieldElm = page.locator('input[type=search]');
361+
const resultsHeadingElm = page.locator('.results-panel .title');
362+
363+
await docsifyInit(docsifyInitConfig);
364+
365+
await searchFieldElm.fill('hello');
366+
await expect(resultsHeadingElm).toHaveText('Hello World');
367+
});
336368
});

0 commit comments

Comments
 (0)