Skip to content

Commit

Permalink
fix 404 from search result (#2181)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Aug 19, 2023
1 parent cf5f91e commit ddddce9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/kind-clouds-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'nextra-theme-docs': patch
'nextra': patch
---

skip search indexing for 404/500 pages
5 changes: 5 additions & 0 deletions docs/pages/404.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NotFoundPage } from 'nextra-theme-docs'

# 404: Page Not Found

<NotFoundPage />
7 changes: 7 additions & 0 deletions docs/pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@
"theme": {
"typesetting": "article"
}
},
"404": {
"type": "page",
"theme": {
"timestamp": false,
"typesetting": "article"
}
}
}
6 changes: 6 additions & 0 deletions docs/pages/docs/guide.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BoxIcon,
BrushIcon,
DropperIcon,
FilesIcon,
Expand Down Expand Up @@ -34,4 +35,9 @@ available in all themes.
<Card icon={<GlobeIcon />} title="Next.js I18n" href="/docs/guide/i18n" />
<Card icon={<BrushIcon />} title="Custom CSS" href="/docs/guide/custom-css" />
<Card icon={<DropperIcon />} title="Advanced" href="/docs/guide/advanced" />
<Card
icon={<BoxIcon />}
title="Built-in Components"
href="/docs/guide/built-ins"
/>
</Cards>
4 changes: 2 additions & 2 deletions packages/nextra-theme-docs/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export { Input } from './input'
export { LocaleSwitch } from './locale-switch'
export { NavLinks } from './nav-links'
export { Navbar } from './navbar'
export { NotFoundPage } from './not-found'
export { NotFoundPage } from './404'
export { Search } from './search'
export { Select } from './select'
export { ServerSideErrorPage } from './server-side-error'
export { ServerSideErrorPage } from './500'
export { Sidebar } from './sidebar'
export { SkipNavContent, SkipNavLink } from './skip-nav'
export { ThemeSwitch } from './theme-switch'
Expand Down
47 changes: 26 additions & 21 deletions packages/nextra/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CODE_BLOCK_FILENAME_REGEX,
CWD,
DEFAULT_LOCALE,
ERROR_ROUTES,
MARKDOWN_URL_EXTENSION_REGEX
} from './constants'
import {
Expand Down Expand Up @@ -90,22 +91,35 @@ export async function compileMdx(

const structurizedData = Object.create(null)

const {
staticImage,
flexsearch,
readingTime,
latex,
codeHighlight,
defaultShowCopyCode,
route = '',
locale,
mdxOptions
} = loaderOptions

let searchIndexKey: string | null = null
if (typeof loaderOptions.flexsearch === 'object') {
if (loaderOptions.flexsearch.indexKey) {
searchIndexKey = loaderOptions.flexsearch.indexKey(
filePath,
loaderOptions.route || '',
loaderOptions.locale
)
if (
ERROR_ROUTES.has(route) ||
route === '/_app' /* remove this check in v3 */
) {
/* skip */
} else if (typeof flexsearch === 'object') {
if (flexsearch.indexKey) {
searchIndexKey = flexsearch.indexKey(filePath, route, locale)
if (searchIndexKey === '') {
searchIndexKey = loaderOptions.locale || DEFAULT_LOCALE
searchIndexKey = locale || DEFAULT_LOCALE
}
} else {
searchIndexKey = loaderOptions.locale || DEFAULT_LOCALE
searchIndexKey = locale || DEFAULT_LOCALE
}
} else if (loaderOptions.flexsearch) {
searchIndexKey = loaderOptions.locale || DEFAULT_LOCALE
} else if (flexsearch) {
searchIndexKey = locale || DEFAULT_LOCALE
}

const {
Expand All @@ -116,23 +130,14 @@ export async function compileMdx(
rehypePlugins,
rehypePrettyCodeOptions
}: MdxOptions = {
...loaderOptions.mdxOptions,
...mdxOptions,
// You can override MDX options in the frontMatter too.
...frontMatter.mdxOptions
}

const format =
_format === 'detect' ? (filePath.endsWith('.mdx') ? 'mdx' : 'md') : _format

const {
staticImage,
flexsearch,
readingTime,
latex,
codeHighlight,
defaultShowCopyCode
} = loaderOptions

// https://github.com/shuding/nextra/issues/1303
const isFileOutsideCWD =
!isPageImport && path.relative(CWD, filePath).startsWith('..')
Expand Down

1 comment on commit ddddce9

@vercel
Copy link

@vercel vercel bot commented on ddddce9 Aug 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.