nextjs - 💡(How to fix) Fix Bug when trying to import a MDX page in a server component [3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#87717Fetched 2026-04-08 02:06:27
View on GitHub
Comments
3
Participants
3
Timeline
9
Reactions
0
Author
Assignees
Timeline (top)
commented ×3labeled ×2assigned ×1closed ×1

Error Message

Hello, I'm trying to build a blog with Next.js and MDX. I followed the tutorial on Next.js documentation and during the step to render MDX dynamically I get this error: I tried to create a client component where I import the MDX page the same way, but get an error because async await is not available in client components.

Root Cause

I tried to create a client component where I import the MDX page the same way, but get an error because async await is not available in client components.

Code Example

createContext only works in Client Components. Add the "use client" directive at the top of the file to use it.

---

export default async function Page({
	params,
}: {
	params: Promise<{ slug: string }>
}) {
	const { slug } = await params
	const { default: Post } = await import(`@/md/${slug}.mdx`)

	return <Post />
}

export function generateStaticParams() {
	return [{ slug: "hello" }]
}

export const dynamicParams = false

---

Operating System:
  Platform: MacOS
  Arch: arm64
  Version: 26.0.1
Binaries:
  node 24.4.0
  pnpm 10.17.0
Relevant packages:
  next: 16.1.1
  typescript: 5
  react: 19.2.3 
  react-dom: 19.2.3
  @next/mdx: 16.1.1
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/JulSeb42/next-mdx

To Reproduce

  1. Start application
  2. Navigate to https://localhost:3000/hello

Current vs. Expected behavior

Hello, I'm trying to build a blog with Next.js and MDX. I followed the tutorial on Next.js documentation and during the step to render MDX dynamically I get this error:

createContext only works in Client Components. Add the "use client" directive at the top of the file to use it.

I tried to create a client component where I import the MDX page the same way, but get an error because async await is not available in client components.

This is my code:

export default async function Page({
	params,
}: {
	params: Promise<{ slug: string }>
}) {
	const { slug } = await params
	const { default: Post } = await import(`@/md/${slug}.mdx`)

	return <Post />
}

export function generateStaticParams() {
	return [{ slug: "hello" }]
}

export const dynamicParams = false

How can I fix this issue?

Thanks!

Provide environment information

Operating System:
  Platform: MacOS
  Arch: arm64
  Version: 26.0.1
Binaries:
  node 24.4.0
  pnpm 10.17.0
Relevant packages:
  next: 16.1.1
  typescript: 5
  react: 19.2.3 
  react-dom: 19.2.3
  @next/mdx: 16.1.1

Which area(s) are affected? (Select all that apply)

Dynamic Routes

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

extent analysis

TL;DR

  • Add the "use client" directive to the top of the file and refactor the code to handle asynchronous operations outside of client components.

Guidance

  • Identify the components that require the use client directive and add it to the top of those files.
  • Refactor the Page component to handle asynchronous operations, such as importing MDX pages, outside of the client component.
  • Consider using a separate function or hook to handle the asynchronous import of MDX pages.
  • Review the Next.js documentation on client components and server components to understand the limitations and best practices.

Example

// Use a separate function to handle the asynchronous import
const importMdxPage = async (slug: string) => {
  const { default: Post } = await import(`@/md/${slug}.mdx`)
  return Post
}

// Add the "use client" directive to the top of the file
'use client'

export default function Page({ params }: { params: { slug: string } }) {
  const { slug } = params
  const Post = importMdxPage(slug)

  return <Post />
}

However, the above example will still not work as expected because importMdxPage is an async function and Post will be a promise. A better approach would be to use a React state to store the imported component and update it when the import is complete.

Notes

  • The provided code snippet is a simplified example and may not work as-is in the user's application.
  • The use client directive is required for client components, but it also imposes limitations on the use of asynchronous operations.

Recommendation

  • Apply workaround: Refactor the code to handle asynchronous operations outside of client components, and use a separate function or hook to handle the import of MDX pages. This will allow the user to work around the limitations of client components in Next.js.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - 💡(How to fix) Fix Bug when trying to import a MDX page in a server component [3 comments, 3 participants]