Skip to content

Commit e59321b

Browse files
committed
fix: locale error in production
1 parent 91e41e6 commit e59321b

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

src/shared/types/default-theme.ts

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export namespace DefaultTheme {
8989
export interface LocaleConfig {
9090
lang?: string;
9191
title?: string;
92+
routePrefix?: string;
9293
description?: string;
9394
head?: HeadConfig[];
9495
label: string;

src/theme-default/components/Nav/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export function Nav() {
178178
items-center=""
179179
>
180180
<div className="search" flex="sm:1" pl="sm:8">
181-
<Search __island />
181+
<Search __island langRoutePrefix={localeData.routePrefix || ''} />
182182
</div>
183183
<NavMenu menuItems={menuItems} />
184184
{hasMultiLanguage && (

src/theme-default/components/Search/index.tsx

+19-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ChangeEvent, useCallback, useRef, useState } from 'react';
22
import { MatchResultItem, PageSearcher } from '../../logic/search';
33
import SearchSvg from './icons/search.svg';
44
import { ComponentPropsWithIsland } from '../../../shared/types/index';
5-
import { useLocaleSiteData } from '../../logic';
65
import { throttle } from 'lodash-es';
76

87
function SuggestionContent(props: {
@@ -65,8 +64,9 @@ function SuggestionContent(props: {
6564
}
6665

6766
// eslint-disable-next-line @typescript-eslint/no-unused-vars
68-
export function Search(_props: ComponentPropsWithIsland) {
69-
const localeData = useLocaleSiteData();
67+
export function Search(
68+
props: ComponentPropsWithIsland & { langRoutePrefix: string }
69+
) {
7070
const [suggestions, setSuggestions] = useState<MatchResultItem[]>([]);
7171
const [query, setQuery] = useState('');
7272
const [focused, setFocused] = useState(false);
@@ -76,25 +76,29 @@ export function Search(_props: ComponentPropsWithIsland) {
7676
const initPageSearcher = useCallback(() => {
7777
if (!psRef.current) {
7878
return import('../../logic/search').then(({ PageSearcher }) => {
79-
psRef.current = new PageSearcher(localeData.lang || 'en');
79+
psRef.current = new PageSearcher(props.langRoutePrefix);
8080
psRef.current.init();
8181
});
8282
}
8383
return Promise.resolve();
84-
}, [localeData.lang]);
84+
}, [props.langRoutePrefix]);
8585

8686
// eslint-disable-next-line react-hooks/exhaustive-deps
8787
const onQueryChanged = useCallback(
88-
throttle(async (e: ChangeEvent<HTMLInputElement>) => {
89-
await initPageSearcher();
90-
const newQuery = e.target.value;
91-
setQuery(newQuery);
92-
if (psRef.current) {
93-
psRef.current.match(newQuery).then((matched) => {
94-
setSuggestions(matched);
95-
});
96-
}
97-
}, 300),
88+
throttle(
89+
async (e: ChangeEvent<HTMLInputElement>) => {
90+
await initPageSearcher();
91+
const newQuery = e.target.value;
92+
setQuery(newQuery);
93+
if (psRef.current) {
94+
psRef.current.match(newQuery).then((matched) => {
95+
setSuggestions(matched);
96+
});
97+
}
98+
},
99+
200,
100+
{ leading: true, trailing: true }
101+
),
98102
[initPageSearcher]
99103
);
100104
return (

src/theme-default/logic/search.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ export class PageSearcher {
4141
#index?: SearchIndex<PageDataForSearch[]>;
4242
#cjkIndex?: SearchIndex<PageDataForSearch[]>;
4343
#headerToIdMap: Record<string, string> = {};
44-
#lang: string;
44+
#langRoutePrefix: string;
4545

46-
constructor(lang: string) {
47-
this.#lang = lang;
46+
constructor(langRoutePrefix: string) {
47+
this.#langRoutePrefix = langRoutePrefix;
4848
}
4949

5050
async init(options: CreateOptions = {}) {
5151
// Initial pages data and create index
5252
const pages = await getAllPages((route) =>
53-
route.path.startsWith(`/${this.#lang}`)
53+
route.path.startsWith(this.#langRoutePrefix)
5454
);
5555
const pagesForSearch: PageDataForSearch[] = pages
5656
.filter((page) => {

src/theme-default/logic/useLocaleSiteData.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { usePageData } from '../../runtime';
44
import { useLocation } from 'react-router-dom';
55

66
export function useLocaleSiteData(): DefaultTheme.LocaleConfig {
7-
const { siteData } = usePageData();
8-
const { pathname } = useLocation();
9-
const { themeConfig } = siteData;
10-
const locales = themeConfig.locales;
7+
const pageData = usePageData();
8+
// eslint-disable-next-line react-hooks/rules-of-hooks
9+
const { pathname } = import.meta.env.SSR ? useLocation() : location;
10+
const themeConfig = pageData?.siteData?.themeConfig ?? {};
11+
const locales = themeConfig?.locales;
1112
if (!locales || Object.keys(locales).length === 0) {
1213
return {
1314
nav: themeConfig.nav,
@@ -20,5 +21,8 @@ export function useLocaleSiteData(): DefaultTheme.LocaleConfig {
2021
return pathname.startsWith(addLeadingSlash(removeTrailingSlash(locale)));
2122
}) || localeKeys[0];
2223

23-
return locales[localeKey];
24+
return {
25+
...locales[localeKey],
26+
routePrefix: localeKey
27+
};
2428
}

0 commit comments

Comments
 (0)