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.js hydration issue #80

Open
4 tasks done
nguyenvukhang opened this issue Dec 22, 2022 · 8 comments
Open
4 tasks done

Next.js hydration issue #80

nguyenvukhang opened this issue Dec 22, 2022 · 8 comments
Labels
🤞 phase/open Post is being triaged manually

Comments

@nguyenvukhang
Copy link

nguyenvukhang commented Dec 22, 2022

Initial checklist

Affected packages and versions

remark-math, rehype-mathjax, all latest versions

Link to runnable example

No response

Steps to reproduce

yarn create next-app
yarn add @next/mdx @mdx-js/loader @mdx-js/react remark-math rehype-mathjax

create a next.config.mjs with the contents:

import mdx from "@next/mdx";
import remarkMath from "remark-math";
import rehypeMathJax from "rehype-mathjax";

const withMDX = mdx({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkMath],
    rehypePlugins: [rehypeMathJax],
  },
});

export default withMDX({
  reactStrictMode: true,
  pageExtensions: ["tsx", "jsx", "mdx"],
});

Expected behavior

Should render without errors.

Actual behavior

Renders correctly, but with the error
Error: Text content does not match server-rendered HTML.

Runtime

Node v16

Package manager

yarn 1

OS

macOS

Build and bundle tools

Next.js (bug is seen on both v13 and v12)

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Dec 22, 2022
@nguyenvukhang nguyenvukhang changed the title Next JS 13 hydration issue Next.js hydration issue Dec 22, 2022
@nguyenvukhang
Copy link
Author

nguyenvukhang commented Dec 22, 2022

This error disappears when commenting out lines 113-115 in https://github.com/remarkjs/remark-math/blob/7e82f934e77d62b7366825633bc7f1d5d5e5a861/packages/rehype-mathjax/lib/create-plugin.js

      if (found && renderer.styleSheet) {
        context.children.push(renderer.styleSheet())
      }

Though it's still unclear where the root cause is

@wooorm
Copy link
Member

wooorm commented Dec 22, 2022

well, the root cause is that there is different handling on the server (JSDOM) and a browser (depending on which one your users use). Not sure it can be change,d but feel free to investigate it.

@hkhanna
Copy link

hkhanna commented Jan 8, 2023

I'm having this problem too. One thing I've noticed is that if you inspect a mathjax-generated element, you'll see something like this: <mjx-container classname="MathJax ...>

It should say class=, not classname= when you're inspecting in the browser. For some reason, when combined with MDX, it doesn't properly convert that attribute. If I don't use MDX and use Next.js directly, it works fine, so it has something to do with remark-math and MDX together, I think.

@wooorm
Copy link
Member

wooorm commented Jan 9, 2023

That seems like a very different problem? The above seems to talk about CSS, you seem to talk about (MathML) attribute conversion?

@hkhanna
Copy link

hkhanna commented Jan 9, 2023

I'm not talking about MathML, at least I don't think I am.

I'm talking about how rehype-mathjax renders the HTML. Just suggesting a possible cause of the hydration error. If somehow it was being rendered as "class" on the server but "classname" on the client, I think that could cause the hydration error.

Interestingly, when I commented out the lines OP suggested, I still got the error. If I switch to rehype-katex the problem goes away.

@wooorm
Copy link
Member

wooorm commented Jan 18, 2023

oh sorry yeah you’re right, mjx-container is a custom element, not mathml.

when I commented out the lines OP suggested, I still got the error

Yeah, I thought it was a different problem, and this seems to indicate that.

If I switch to rehype-katex the problem goes away.

It does thing completely differently!

dylanhu7 added a commit to dylanhu7/remark-math that referenced this issue Feb 10, 2023
@fullykubed
Copy link

Can confirm this issue still exists with Next.js 14

@fullykubed
Copy link

Can confirm that this solution that avoids adding the css appears to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤞 phase/open Post is being triaged manually
Development

No branches or pull requests

4 participants