reactjsgatsbyprismjsprism-react-renderer

Issue with prism-react-renderer highlighting in production build in Gatsby


I am facing an issue with code highlighting when using prism-react-renderer component in Gatsby. I have configured it as per instructions in FormidableLabs/prism-react-renderer.

Everything works in development mode, but when I build a production version with the gatsby build command, the pre tag is not decorated with prism classes (class="prism-code language-python" is missing). My guess is that the production build does not include the right component or CSS, but I don't know where to start looking.

Also, this issue was not present when I used the same code 7-8 months back in another project. So it may be a problem introduced in the past few months because of a plugin/component update.

Attached are two screenshots - one with gatsby develop and another with gatsby build.

Commands I ran to setup and install the requisites:

> gatsby new test-mdx-prism-rendering https://github.com/gatsbyjs/gatsby-starter-hello-world
> npm install --save gatsby-plugin-mdx @mdx-js/mdx @mdx-js/react
> npm install --save mdx-utils
> npm install --save prism-react-renderer

Sample GitHub project is at subhashb/test-mdx-prism-rendering

Screenshot in development mode Screenshot in production build


Solution

  • Found the answer as I tried to dig deeper.

    The workaround is discussed here: https://github.com/ChristopherBiscardi/gatsby-mdx/issues/394

    Apparently, the wrapRootElement defined in gatsby-browser.js should be made available in gatsby-ssr.js as well.