Skip to content

Commit

Permalink
fix: Search index not generating in certain Next.js versions (#1914)
Browse files Browse the repository at this point in the history
* fix search

* add changeset

* prettier
  • Loading branch information
shuding committed May 29, 2023
1 parent 6148790 commit 9c9625e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/gorgeous-walls-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'nextra': patch
'nextra-theme-blog': patch
'nextra-theme-docs': patch
---

Fix search not working in certain Next.js versions
16 changes: 15 additions & 1 deletion packages/nextra/src/search/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ export class NextraSearchPlugin {

for (const [, entry] of compilation.entries.entries()) {
const entryDependency = entry.dependencies?.[0]
const entryModule =

// There're some Next.js refactors that might cause the MDX module
// to be a dependency of the entry module, instead of the entry
// itself. This is a workaround to find the MDX module loaded by
// Nextra.
let entryModule =
compilation.moduleGraph.getResolvedModule(entryDependency)
if (!entryModule?.buildInfo?.nextraSearch) {
for (const dependency of entryModule.dependencies) {
const mod =
compilation.moduleGraph.getResolvedModule(dependency)
if (mod?.buildInfo?.nextraSearch) {
entryModule = mod
}
}
}

if (entryModule?.buildInfo?.nextraSearch) {
const { title, data, indexKey, route } =
Expand Down

1 comment on commit 9c9625e

@vercel
Copy link

@vercel vercel bot commented on 9c9625e May 29, 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.