Skip to content

Commit 2d8cb5a

Browse files
authored
fix(search): replace stale records when saving an index (#2817)
1 parent 7272bfa commit 2d8cb5a

2 files changed

Lines changed: 72 additions & 6 deletions

File tree

‎src/plugins/search/search.js‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ db.version(1).stores({
2020
expires: 'key, value',
2121
});
2222

23-
async function saveData(maxAge, expireKey) {
23+
async function saveData(maxAge, expireKey, indexKey) {
2424
const records = [];
2525

2626
Object.values(INDEXES).forEach(entry => {
@@ -43,10 +43,15 @@ async function saveData(maxAge, expireKey) {
4343
});
4444

4545
INDEXES = records;
46-
await /** @type {any} */ (db).search.bulkPut(records);
47-
await /** @type {any} */ (db).expires.put({
48-
key: expireKey,
49-
value: Date.now() + maxAge,
46+
const searchTable = db.table('search');
47+
const expiresTable = db.table('expires');
48+
await db.transaction('rw', searchTable, expiresTable, async () => {
49+
await searchTable.where({ indexKey }).delete();
50+
await searchTable.bulkPut(records);
51+
await expiresTable.put({
52+
key: expireKey,
53+
value: Date.now() + maxAge,
54+
});
5055
});
5156
}
5257

@@ -449,7 +454,7 @@ export async function init(config, vm) {
449454

450455
const markComplete = async () => {
451456
if (len === ++count) {
452-
await saveData(config.maxAge, expireKey);
457+
await saveData(config.maxAge, expireKey, indexKey);
453458
}
454459
};
455460

‎test/e2e/search.test.js‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,67 @@ test.describe('Search Plugin Tests', () => {
340340
await expect(resultsHeadingElm).toHaveText('EmptyContent');
341341
});
342342

343+
test('does not restore deleted sections after rebuilding an expired cache', async ({
344+
page,
345+
}) => {
346+
const options = {
347+
config: {
348+
search: { paths: ['/'], namespace: 'cache-refresh', maxAge: -1 },
349+
},
350+
markdown: { homepage: '# Obsolete\n\nretiredkeyword' },
351+
scriptURLs: ['/dist/plugins/search.js'],
352+
};
353+
const searchField = page.locator('input[type=search]');
354+
const results = page.locator('.results-panel .title');
355+
356+
await docsifyInit(options);
357+
await searchField.fill('retiredkeyword');
358+
await expect(results).toHaveText('Obsolete');
359+
360+
options.config.search.maxAge = 60000;
361+
options.markdown.homepage = '# Current\n\ncurrentkeyword';
362+
await docsifyInit(options);
363+
await searchField.fill('currentkeyword');
364+
await expect(results).toHaveText('Current');
365+
await searchField.fill('retiredkeyword');
366+
await expect(page.getByText('No Results!', { exact: true })).toBeVisible();
367+
368+
await docsifyInit(options);
369+
await searchField.fill('currentkeyword');
370+
await expect(results).toHaveText('Current');
371+
await searchField.fill('retiredkeyword');
372+
await expect(page.getByText('No Results!', { exact: true })).toBeVisible();
373+
});
374+
375+
test('preserves another namespace when saving a search index', async ({
376+
page,
377+
}) => {
378+
const otherSite = {
379+
config: { search: { paths: ['/'], namespace: 'other-site' } },
380+
markdown: { homepage: '# Other site\n\nprotectedkeyword' },
381+
scriptURLs: ['/dist/plugins/search.js'],
382+
};
383+
const searchField = page.locator('input[type=search]');
384+
const results = page.locator('.results-panel .title');
385+
386+
await docsifyInit(otherSite);
387+
await searchField.fill('protectedkeyword');
388+
await expect(results).toHaveText('Other site');
389+
390+
await docsifyInit({
391+
config: { search: { paths: ['/'], namespace: 'current-site' } },
392+
markdown: { homepage: '# Current site\n\ncurrentkeyword' },
393+
scriptURLs: ['/dist/plugins/search.js'],
394+
});
395+
await searchField.fill('currentkeyword');
396+
await expect(results).toHaveText('Current site');
397+
398+
otherSite.markdown.homepage = '# Changed\n\nchangedkeyword';
399+
await docsifyInit(otherSite);
400+
await searchField.fill('protectedkeyword');
401+
await expect(results).toHaveText('Other site');
402+
});
403+
343404
test('keeps saving index when one auto path request fails with cached records', async ({
344405
page,
345406
}) => {

0 commit comments

Comments
 (0)