Skip to content

Commit a2fec82

Browse files
authored
fix: failed to update search index after switching lang (#1950)
1 parent dfbd4b5 commit a2fec82

File tree

7 files changed

+96
-5
lines changed

7 files changed

+96
-5
lines changed
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Index
2+
3+
## Button en
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 首页
2+
3+
## Button 中文

e2e/fixtures/search-i18n/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@rspress-fixture/search-i18n",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"build": "rspress build",
7+
"dev": "rspress dev",
8+
"preview": "rspress preview"
9+
},
10+
"dependencies": {
11+
"rspress": "workspace:*"
12+
}
13+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as path from 'node:path';
2+
import { defineConfig } from 'rspress/config';
3+
4+
export default defineConfig({
5+
root: path.join(__dirname, 'doc'),
6+
lang: 'en',
7+
themeConfig: {
8+
locales: [
9+
{
10+
lang: 'zh',
11+
label: '简体中文',
12+
},
13+
{
14+
lang: 'en',
15+
label: 'English',
16+
},
17+
],
18+
},
19+
});

e2e/tests/search-i18n.test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import path from 'node:path';
2+
import { expect, test } from '@playwright/test';
3+
import { getPort, killProcess, runDevCommand } from '../utils/runCommands';
4+
import { searchInPage } from '../utils/search';
5+
6+
const fixtureDir = path.resolve(__dirname, '../fixtures');
7+
8+
test.describe('search i18n test', async () => {
9+
let appPort;
10+
let app;
11+
12+
test.beforeAll(async () => {
13+
const appDir = path.join(fixtureDir, 'search-i18n');
14+
appPort = await getPort();
15+
app = await runDevCommand(appDir, appPort);
16+
});
17+
18+
test.afterAll(async () => {
19+
if (app) {
20+
await killProcess(app);
21+
}
22+
});
23+
24+
test('should update search index when language changed', async ({ page }) => {
25+
await page.goto(`http://localhost:${appPort}`);
26+
27+
const suggestItems1 = await searchInPage(page, 'Button');
28+
expect(await suggestItems1[0].textContent()).toContain('Button en');
29+
30+
// close the search modal
31+
await page.click('body');
32+
33+
// Switch language to Chinese
34+
await page.click('.rspress-nav-menu-group-button');
35+
await page.click('.rspress-nav-menu-group-content a');
36+
await page.waitForLoadState();
37+
38+
const suggestItems2 = await searchInPage(page, 'Button');
39+
expect(await suggestItems2[0].textContent()).toContain('Button 中文');
40+
await page.click('body');
41+
42+
// Switch language to English
43+
await page.click('.rspress-nav-menu-group-button');
44+
await page.click('.rspress-nav-menu-group-content a');
45+
await page.waitForLoadState();
46+
47+
const suggestItems3 = await searchInPage(page, 'Button');
48+
expect(await suggestItems3[0].textContent()).toContain('Button en');
49+
});
50+
});

packages/theme-default/src/components/Search/SearchPanel.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ export function SearchPanel({ focused, setFocused }: SearchPanelProps) {
296296

297297
// init pageSearcher again when lang or version changed
298298
useEffect(() => {
299-
if (initStatus === 'initial') {
300-
return;
301-
}
302-
303299
const { currentLang, currentVersion } = pageSearcherConfigRef.current ?? {};
304300
const isLangChanged = lang !== currentLang;
305301
const isVersionChanged = versionedSearch && version !== currentVersion;
@@ -308,7 +304,8 @@ export function SearchPanel({ focused, setFocused }: SearchPanelProps) {
308304
// reset status first
309305
setInitStatus('initial');
310306
pageSearcherRef.current = null;
311-
initSearch();
307+
const searcher = createSearcher();
308+
searcher.fetchSearchIndex();
312309
}
313310
}, [lang, version, versionedSearch]);
314311

pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)