diff --git a/.changeset/lazy-hounds-tan.md b/.changeset/lazy-hounds-tan.md new file mode 100644 index 0000000000..87071abf9e --- /dev/null +++ b/.changeset/lazy-hounds-tan.md @@ -0,0 +1,6 @@ +--- +'nextra': patch +'nextra-theme-docs': patch +--- + +fix(nextra/docs): fallback search to `en-US` instead `default` diff --git a/packages/nextra-theme-docs/src/flexsearch.js b/packages/nextra-theme-docs/src/flexsearch.js index 0db270a94f..93581738e5 100644 --- a/packages/nextra-theme-docs/src/flexsearch.js +++ b/packages/nextra-theme-docs/src/flexsearch.js @@ -6,7 +6,7 @@ import React, { useEffect, Fragment } from 'react' -import Router, { useRouter } from 'next/router' +import { useRouter } from 'next/router' import cn from 'classnames' import Link from 'next/link' import FlexSearch from 'flexsearch' @@ -34,7 +34,7 @@ const Item = ({ {page} ) : null} - + { if (!search) return - const localeCode = Router.locale || 'default' + const localeCode = router.locale const index = indexes[localeCode] if (!index) return @@ -251,11 +251,11 @@ export default function Search() { ) const load = async () => { - const localeCode = Router.locale || 'default' + const localeCode = router.locale if (!indexes[localeCode] && !loading) { setLoading(true) const response = await fetch( - `${Router.basePath}/_next/static/chunks/nextra-data-${localeCode}.json` + `${router.basePath}/_next/static/chunks/nextra-data-${localeCode}.json` ) const data = await response.json() @@ -430,7 +430,7 @@ export default function Search() { key={`search-item-${i}`} page={res.page} title={res.title} - href={res.route} + href={router.basePath + res.route} excerpt={res.excerpt} active={i === active} onHover={() => setActive(i)} diff --git a/packages/nextra/src/constants.ts b/packages/nextra/src/constants.ts index 1054286461..2857b72603 100644 --- a/packages/nextra/src/constants.ts +++ b/packages/nextra/src/constants.ts @@ -3,3 +3,5 @@ export const MARKDOWN_EXTENSION_REGEX = /\.mdx?$/ export const IS_PRODUCTION = process.env.NODE_ENV === 'production' export const LOCALE_REGEX = /\.([a-z]{2}(-[A-Z]{2})?)$/ + +export const DEFAULT_LOCALE = 'en-US' diff --git a/packages/nextra/src/index.ts b/packages/nextra/src/index.ts index 931671a5d9..2558ad9939 100644 --- a/packages/nextra/src/index.ts +++ b/packages/nextra/src/index.ts @@ -1,5 +1,5 @@ import { NextraPlugin, pageMapCache } from './plugin' -import { MARKDOWN_EXTENSION_REGEX } from './constants' +import { DEFAULT_LOCALE, MARKDOWN_EXTENSION_REGEX } from './constants' import { LoaderOptions, Nextra } from './types' const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] as const @@ -25,12 +25,12 @@ const nextra: Nextra = (...args) => } else if (!i18n?.defaultLocale) { // If `i18n.locales` and `i18n.defaultLocale` were not specified, // client will receive error - Text content does not match server-rendered HTML. - // Due to `const { locales } = useRouter()` where `locales` will be `undefined` + // Due to `const { locale } = useRouter()` where `locale` will be `undefined` // To fix it we need to explicitly specify `i18n.locales` and `i18n.defaultLocale` nextConfig.i18n = { ...i18n, - locales: ['en-US'], - defaultLocale: 'en-US' + locales: [DEFAULT_LOCALE], + defaultLocale: DEFAULT_LOCALE } } diff --git a/packages/nextra/src/loader.ts b/packages/nextra/src/loader.ts index d8813c3c0a..66624ce9e4 100644 --- a/packages/nextra/src/loader.ts +++ b/packages/nextra/src/loader.ts @@ -11,7 +11,7 @@ import { parseFileName } from './utils' import { compileMdx } from './compile' import { getPageMap, findPagesDir } from './page-map' import { collectFiles, collectMdx } from './plugin' -import { MARKDOWN_EXTENSION_REGEX, IS_PRODUCTION } from './constants' +import { MARKDOWN_EXTENSION_REGEX, IS_PRODUCTION, DEFAULT_LOCALE } from './constants' // TODO: create this as a webpack plugin. const indexContentEmitted = new Set() @@ -130,7 +130,7 @@ async function loader( // We only add .MD and .MDX contents if (MARKDOWN_EXTENSION_REGEX.test(filename) && data.searchable !== false) { addPage({ - fileLocale: fileLocale || 'default', + fileLocale: fileLocale || DEFAULT_LOCALE, route, title, data,