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

Next Image component within MDX throws error #58888

Closed
1 task done
SnailBones opened this issue Nov 24, 2023 · 4 comments · Fixed by #64875
Closed
1 task done

Next Image component within MDX throws error #58888

SnailBones opened this issue Nov 24, 2023 · 4 comments · Fixed by #64875
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization. locked Markdown (MDX) Related to Markdown with Next.js.

Comments

@SnailBones
Copy link

SnailBones commented Nov 24, 2023

Link to the code that reproduces this issue

https://github.com/SnailBones/next-mdx-bug-demo

To Reproduce

  1. Set up an app with create-next app and install dependencies for mdx.
npm install @next/mdx @mdx-js/loader @mdx-js/react
npm run dev
  1. Set up mdx by adding mdx-components.js editing next-config.js according to next docs
  2. Add a mdx page with an image, eg:
import Image from "next/image";

# Title

This is a markdown page

<Image src="/next.svg" alt="Next.js Logo" width={180} height={37} />

Current vs. Expected behavior

Expect the page to render and display image without error. Instead, I see the following error:

image

In terminal:

⨯ Error: Cannot access Image.propTypes on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.
at _createMdxContent (./app/page.mdx:37:74)
at MDXContent (./app/page.mdx:68:16)
at stringify ()
⨯ Error: Cannot access Image.propTypes on the server. You cannot dot into a client module from a server component. You can only pass the imported name through.
at _createMdxContent (./app/page.mdx:37:74)
at MDXContent (./app/page.mdx:68:16)
at stringify ()
digest: "2940815645"

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  9 15:34:04 UTC 2
Binaries:
  Node: 20.5.1
  npm: 10.0.0
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.4-canary.15
  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)

Image optimization (next/image, next/legacy/image), MDX (@next/mdx)

Additional context

Possibly the same as hashicorp/next-mdx-remote#405, but is occurring without next-mdx-remote

@DavidBuck
Copy link

I have run into the same problem. For me, the error only occurs when running in development mode ($ npx next dev ) but not in production mode ( $npx next build && npx next start). I have found the issue from version 13.5.1 onward. The 13.4 release versions that I have checked (13.4.4 - 13.4.19) do not have this issue.

The error occurs when the image is imported locally and also when a remote image (src property) is used.

@octavioamu
Copy link

If you still ran into this issue notice if you have the image inside a map loop passing the key will solve the error on dev mode.
example:

{project.images?.map((image) => (
  <Image
    key={image} // if key is missing you get the error, be aware to use a unique key
    src={image}
    width={300}
    height={250}
    alt={project.title}
  />
))}

huozhi added a commit that referenced this issue Apr 23, 2024
### What

Apply the react aliases for app dir also to the files with
`pagesExtension`

### Why

In the page bundle of mdx page:

In RSC layer react is referencing to the insatlled react 18.2.0 's
`jsx-runtime` to create each JSX element. The react 18.2 JSX runtime
access `propTypes` of the component type and then it crashes 💥
In RSC layer it should use the built-in react canary's `jsx-runtime`.

The reason that it didn't use the built-in one is we're using customized
`pageExtension` `["mdx"]` for mdx, where we didn't apply all these rules
for the files with `pageExtension`, but only the js and ts files by
default.

For mdx specifically, we cannot only applied to
`(page|layout|route).[page extension]` cause every mdx file needs to
have proper resolution. Since this doesn't break transform, it's safe to
apply for all files with page extension.

Fixes #58888 

Closes NEXT-3187
huozhi added a commit that referenced this issue Apr 23, 2024
### What

Apply the react aliases for app dir also to the files with
`pagesExtension`

### Why

In the page bundle of mdx page:

In RSC layer react is referencing to the insatlled react 18.2.0 's
`jsx-runtime` to create each JSX element. The react 18.2 JSX runtime
access `propTypes` of the component type and then it crashes 💥
In RSC layer it should use the built-in react canary's `jsx-runtime`.

The reason that it didn't use the built-in one is we're using customized
`pageExtension` `["mdx"]` for mdx, where we didn't apply all these rules
for the files with `pageExtension`, but only the js and ts files by
default.

For mdx specifically, we cannot only applied to
`(page|layout|route).[page extension]` cause every mdx file needs to
have proper resolution. Since this doesn't break transform, it's safe to
apply for all files with page extension.

Fixes #58888 

Closes NEXT-3187
@huozhi
Copy link
Member

huozhi commented Apr 25, 2024

Fixed in 14.2.3

Copy link
Contributor

github-actions bot commented May 9, 2024

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 added the locked label May 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 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. Image (next/image) Related to Next.js Image Optimization. locked Markdown (MDX) Related to Markdown with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants