Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing client components in MDX fails in dev mode #56153

Closed
1 task done
pomber opened this issue Sep 28, 2023 · 17 comments
Closed
1 task done

Importing client components in MDX fails in dev mode #56153

pomber opened this issue Sep 28, 2023 · 17 comments
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Markdown (MDX) Related to Markdown with Next.js.

Comments

@pomber
Copy link

pomber commented Sep 28, 2023

Link to the code that reproduces this issue

https://github.com/pomber/nextjs-mdx-use-client-bug

To Reproduce

  1. Start the app with next dev (it only happens in development mode)

Current vs. Expected behavior

Fails with this error:

Unhandled Runtime Error
Error: Cannot access Test.propTypes on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.

Call Stack
Object.get
/workspace/nextjs-mdx-use-client-bug/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (22:23981)
React
_createMdxContent
webpack-internal:///(rsc)/./app/hello.mdx (25:74)
MDXContent
webpack-internal:///(rsc)/./app/hello.mdx (45:16)
ej
/workspace/nextjs-mdx-use-client-bug/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js (22:35053)

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

$ npx --no-install next info

Operating System:
  Platform: linux
  Arch: x64
  Version: #202309200834 SMP PREEMPT_DYNAMIC Wed Sep 20 09:14:42 UTC 2023
Binaries:
  Node: 18.18.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.7.6
Relevant Packages:
  next: 13.5.4-canary.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

App Router, MDX (@next/mdx)

Additional context

I'm having problems using any client component directly from mdx files:

import { Test } from "./test";

# Hello

<Test />

If ./test.js has "use client" this fails in dev mode.

A workaround is to re-export the Test component from a file without "use client".

The same example works with next@13.4.19 and fails with next@13.5.1.

Similar to #52415 but now in v13.5 it happens for every client component.

NEXT-1666

@pomber pomber added the bug Issue was opened via the bug report template. label Sep 28, 2023
@github-actions github-actions bot added the Markdown (MDX) Related to Markdown with Next.js. label Sep 28, 2023
@balazsorban44
Copy link
Member

Seems like a regression in v13.4.20-canary.31...v13.4.20-canary.32

I think this is related to #55362, we will have a look!

@balazsorban44 balazsorban44 added the linear: next Confirmed issue that is tracked by the Next.js team. label Sep 29, 2023
@mnlfischer
Copy link

I run into that issue using custom components in the useMDXComponents hook.

import * as mdxComponents from '@/components/mdx'
import { type MDXComponents } from 'mdx/types'

export function useMDXComponents(components: MDXComponents) {
  return {
    ...components,
    ...mdxComponents,
  }
}

If I declare custom components inside the file it works.

@ealexhaywood
Copy link

ealexhaywood commented Oct 16, 2023

This is happening in v13.5.5 for any client component in MDX for me too. Downgrading to 13.4 for now

@MananTank
Copy link

I'm also facing the same issue. Downgrading to 13.4 fixed it

@ealexhaywood
Copy link

ealexhaywood commented Oct 25, 2023

This may be fixed in the latest canary release of 13.5.7. So far so good!

I'm also using the experimental rust compiler in my next config

const nextConfig = {
  experimental: {
    mdxRs: true
  }
}

Edit: things also seem to work well on the default compiler

@pomber
Copy link
Author

pomber commented Nov 3, 2023

Just tried v14.0.1 and looks like it is fixed.

@GrowthWizard
Copy link

GrowthWizard commented Nov 3, 2023

Sadly still run into the same problem, even after the v14.0.1 next.js Update :-/

Bildschirmfoto 2023-11-03 um 20 03 40

Only reverting to "next": "13.4.12", solves the issue.

@azdanov
Copy link

azdanov commented Nov 4, 2023

I'm using this custom component instead of Image in mdx files. Hacky, but works.

import Image, { ImageProps } from "next/image";

export function MdxImage(props: ImageProps) {
  return <Image {...props} />;
}

@t6adev
Copy link

t6adev commented Nov 29, 2023

I tried some versions with this issue in my case. (With App Router)

13.4.12 ~ 13.4.19: No error
13.5.1 ~ 13.5.6 : Error only dev mode
14.0.0 ~ 14.0.1 : No error
14.0.2 ~ : *Another error...

*) Internal error: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

@victorteokw
Copy link

This bug still exists in 14.1

@hbmuller
Copy link

hbmuller commented Mar 19, 2024

I can still reproduce it on the latest version of next.

Edit: actually, it happens to me, but only when consuming components from other parts of my monorepo 🤔

I'll try to isolate the issue later today

@bilal-baig-dev
Copy link

This may be fixed in the latest canary release of 13.5.7. So far so good!

I'm also using the experimental rust compiler in my next config

const nextConfig = {
  experimental: {
    mdxRs: true
  }
}

Edit: things also seem to work well on the default compiler

Hey @ealexhaywood, I was using next js v14.1.4 with app router and configured MDX as per the documentation but was getting error while importing MDX file in a component - after adding mdxRs:true it worked. Thanks for sharing

Lakuna added a commit to Lakuna/Lakuna.github.io that referenced this issue Jul 6, 2024
@devjiwonchoi
Copy link
Member

devjiwonchoi commented Aug 4, 2024

@pomber Hi, PR #57301 was landed at 13.5.7-canary.22 and is the exact fix of this issue. It should've worked on v14.

As there were a few changes in using MDX, opened a PR for the fix to your repro branch.

The main changes:

  1. Use hello/page.mdx instead of hello.mdx just like the migration from Pages Router to App Router.
  2. Configure pageExtensions at the next config. It is also documented here.

Please check out the MDX docs as it had several updates!
Doc: https://nextjs.org/docs/app/building-your-application/configuring/mdx

@devjiwonchoi
Copy link
Member

@victorteokw @t6adev @GrowthWizard @MananTank @ealexhaywood
Were you guys able to manage it to work?

@MananTank
Copy link

@devjiwonchoi Update to latest version, The issue is fixed

@devjiwonchoi
Copy link
Member

Closing as #57301 fixed this issue.
For future readers, please open a new issue with the latest repro. Thank you!

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Markdown (MDX) Related to Markdown with Next.js.
Projects
None yet
Development

No branches or pull requests