bright icon indicating copy to clipboard operation
bright copied to clipboard

Bright can't be used from dynamically imported module

Open altano opened this issue 2 years ago • 1 comments

Here's a repro of the issue: https://codesandbox.io/p/sandbox/2spsii?file=%2Fapp%2Fmdx%2F%5Bslug%5D%2Fpage.tsx%3A10%2C19

(click the link when it's opened)

Error:

Error: This module cannot be imported from a Client Component module. It should only be used from a Server Component.

The problem appears to be that the Bright component doesn't work when dynamically imported from a server component. It thinks it is in a client component even though it isn't.

My code works fine without Bright. I can dynamically import the mdx component from my server component.

altano avatar May 21 '23 21:05 altano

Strange. That error comes from the server-only package (a dependency of Bright) developed by the React team. For me, your code makes sense and it should work, but maybe there is some limitation that we are not seeing.

This, for example, works:

export default async function Article() {
  const { Code } = await import(`bright`);

  return (
    <article>
      <Code lang="js">console.log(1)</Code>
    </article>
  );
}

So I'm suspecting that it's some problem with nextjs' mdx plugin. Need to investigate more.

pomber avatar May 22 '23 08:05 pomber