Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(search): replace stale records when saving an index
  • Loading branch information
Your Name
Your Name committed Sep 28, 2026
commit e9059a6db341a6439c8640c5c8a351ec3486082d
17 changes: 11 additions & 6 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ db.version(1).stores({
expires: 'key, value',
});

async function saveData(maxAge, expireKey) {
async function saveData(maxAge, expireKey, indexKey) {
const records = [];

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

INDEXES = records;
await /** @type {any} */ (db).search.bulkPut(records);
await /** @type {any} */ (db).expires.put({
key: expireKey,
value: Date.now() + maxAge,
const searchTable = db.table('search');
const expiresTable = db.table('expires');
await db.transaction('rw', searchTable, expiresTable, async () => {
await searchTable.where({ indexKey }).delete();
await searchTable.bulkPut(records);
await expiresTable.put({
key: expireKey,
value: Date.now() + maxAge,
});
});
}

Expand Down Expand Up @@ -448,7 +453,7 @@ export async function init(config, vm) {

const markComplete = async () => {
if (len === ++count) {
await saveData(config.maxAge, expireKey);
await saveData(config.maxAge, expireKey, indexKey);
}
};

Expand Down
61 changes: 61 additions & 0 deletions test/e2e/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,67 @@ test.describe('Search Plugin Tests', () => {
await expect(resultsHeadingElm).toHaveText('EmptyContent');
});

test('does not restore deleted sections after rebuilding an expired cache', async ({
page,
}) => {
const options = {
config: {
search: { paths: ['/'], namespace: 'cache-refresh', maxAge: -1 },
},
markdown: { homepage: '# Obsolete\n\nretiredkeyword' },
scriptURLs: ['/dist/plugins/search.js'],
};
const searchField = page.locator('input[type=search]');
const results = page.locator('.results-panel .title');

await docsifyInit(options);
await searchField.fill('retiredkeyword');
await expect(results).toHaveText('Obsolete');

options.config.search.maxAge = 60000;
options.markdown.homepage = '# Current\n\ncurrentkeyword';
await docsifyInit(options);
await searchField.fill('currentkeyword');
await expect(results).toHaveText('Current');
await searchField.fill('retiredkeyword');
await expect(page.getByText('No Results!', { exact: true })).toBeVisible();

await docsifyInit(options);
await searchField.fill('currentkeyword');
await expect(results).toHaveText('Current');
await searchField.fill('retiredkeyword');
await expect(page.getByText('No Results!', { exact: true })).toBeVisible();
});

test('preserves another namespace when saving a search index', async ({
page,
}) => {
const otherSite = {
config: { search: { paths: ['/'], namespace: 'other-site' } },
markdown: { homepage: '# Other site\n\nprotectedkeyword' },
scriptURLs: ['/dist/plugins/search.js'],
};
const searchField = page.locator('input[type=search]');
const results = page.locator('.results-panel .title');

await docsifyInit(otherSite);
await searchField.fill('protectedkeyword');
await expect(results).toHaveText('Other site');

await docsifyInit({
config: { search: { paths: ['/'], namespace: 'current-site' } },
markdown: { homepage: '# Current site\n\ncurrentkeyword' },
scriptURLs: ['/dist/plugins/search.js'],
});
await searchField.fill('currentkeyword');
await expect(results).toHaveText('Current site');

otherSite.markdown.homepage = '# Changed\n\nchangedkeyword';
await docsifyInit(otherSite);
await searchField.fill('protectedkeyword');
await expect(results).toHaveText('Other site');
});

test('keeps saving index when one auto path request fails with cached records', async ({
page,
}) => {
Expand Down
Loading