Skip to content

Commit

Permalink
fix(nextra/docs): fallback search to en-US instead default (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Jul 23, 2022
1 parent af72f85 commit 97e6141
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .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`
12 changes: 6 additions & 6 deletions packages/nextra-theme-docs/src/flexsearch.js
Expand Up @@ -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'
Expand Down Expand Up @@ -34,7 +34,7 @@ const Item = ({
{page}
</div>
) : null}
<Link href={Router.basePath + href}>
<Link href={href}>
<a
className="block no-underline"
onMouseMove={onHover}
Expand Down Expand Up @@ -113,7 +113,7 @@ export default function Search() {
const doSearch = () => {
if (!search) return

const localeCode = Router.locale || 'default'
const localeCode = router.locale
const index = indexes[localeCode]

if (!index) return
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)}
Expand Down
2 changes: 2 additions & 0 deletions packages/nextra/src/constants.ts
Expand Up @@ -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'
8 changes: 4 additions & 4 deletions 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
Expand All @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/nextra/src/loader.ts
Expand Up @@ -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<string>()
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 97e6141

Please sign in to comment.