question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js not applying styles to MDX components

See original GitHub issue

Describe the bug I ran into the same issue described here and here

When rendering MDX pages in Next.js, the styles defined in theme.js don’t get applied.

The styles get applied perfectly well when using Styled.h1 and its sibling components in a JSX file, but the MDX parser seems to render plain HTML tags without the classes needed to apply the styles defined the theme.js

To Reproduce

The issue is present in the next example in this repo. Run the example and you’ll see that only the root styles are applied, but the MDX components are rendered plain, without styles.

The H1 should be color: 'primary' (#07c) but gets rendered with the black (#000) inherited from the root styles applied to the body.

Change the first few lines in index.mdx to make it easier to see:

import { Styled } from 'theme-ui'

# MD H1

<Styled.h1>Styled H1</Styled.h1>

Expected behavior

I’d expect MDX files to render the MD components with all the ThemeUI styles defined in the styles object of theme.js or in other words to # Title should render identically to <Styled.h1>Title</Styled.h1>, etc.

Screenshots

Screen Shot 2020-08-14 at 18 45 14 Screen Shot 2020-08-14 at 18 45 23

Additional context It looks like it might be a next.js bug, but all the issues related to it have been closed.

I tried to debug it for two days now. I changed the versions for next and @next/mdx to

  • 9.3.3
  • 9.3.6
  • 9.5.1
  • 9.5.2

All of which were suggested should work at some point in issue comments, but none of them seem to work in the next example or my project (which is more complex, so I tried to isolate the issue).

I appreciate this might not be a theme-ui issue, but thought maybe someone has already solved it.:

Edit: I also tried to swap @next/mdx for https://github.com/hashicorp/next-mdx-enhanced (latest) but that didn’t help either.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
cwgwcommented, Aug 23, 2020

For what it’s worth, I was able to solve my problem (#1148) using the suggestion above from @jxnblk. His guess that it’s a dependency conflict appears to be correct.

Adding this to my package.json fixed me:

  "resolutions": {
    "@mdx-js/react": "2.0.0-next.7"
  },

It might be a different offender for you. Try yarn list --pattern mdx and look for multiple versions of the same package.

2reactions
pixelmordcommented, Aug 25, 2020

Resolving to the version of @mdx-js/react that is used by the mdx loader in my nextjs project (next-mdx-enhanced) also solved the issue for me: THX @cwgw and @jxnblk @jxnblk maybe upgrading theme-ui deps to @mdx-js/react@1.6.16 could prevent others from having that issue…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Using MDX - Next.js
When you want to style your own elements to give a custom feel to your website or application, you can pass in shortcodes....
Read more >
Create a Next.js and MDX blog - LogRocket Blog
Learn how to build a blog in Next.js using MDX to allow for the creation of interactive content, along with adding styles and...
Read more >
MDX styling (next-mdx-remote) fails after I install Tailwind css ...
I'm using next-mdx-remote to make use of MDX in my Next.js project. I've been following JetBrains WebStorm guide to build this, here they've ......
Read more >
MDX with Next.js: The Easiest Way to Build a Static Content Site
Getting MDX up and running with Next. js has never been easier thanks to Contentlayer: a content toolkit that makes working with content...
Read more >
How to setup MDX in Next.js - jmagrippis
MDX is markdown with JSX support, which pretty much means you can embed React components in your posts! But why?! The why. A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found