Skip to content

Hydration mismatch because of different "newline" #2000

Closed

Description

@stenet

Environment

  • Operating System: Windows_NT
  • Node Version: v18.15.0
  • Nuxt Version: 3.3.3
  • Nitro Version: 2.3.2
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: modules, tailwindcss, content, routeRules
  • Runtime Modules: @nuxtjs/[email protected], @nuxt/[email protected]
  • Build Modules: -

Reproduction

image

Describe the bug

test.md was created in Windows 11 + Rider.
New lines are represented by \r\n.

On hydration this causes a mismatch because \r\n != \n.

Additional context

No response

Logs

No response

Activity

  1. bjerggaard commented on Apr 20, 2023

    @bjerggaard

    I experienced this as well

  2. skoenfaelt commented on Apr 20, 2023

    @skoenfaelt

    A workaround for that until a fix arrives is to use lf as end of line

    I setup it in .editorconfig and .prettierrc so it gets automatically "fixed" by the ide and commands.

  3. nobkd commented on Jun 17, 2023

    @nobkd
    Contributor

    I tried to reproduce this issue, and as far as I can see, the hydration mismatch only happens with code blocks.
    Does that match your experience?

  4. nobkd commented on Jun 24, 2023

    @nobkd
    Contributor

    Thanks for the information. I'm currently trying to find the location in the code where the file is read / before Vue gets the data.

    As a temporary fix, that's probably much simpler than a custom content renderer I suggest you to use the content:file:beforeParse hook. To get it to work, you have to remove the .nuxt directory of your project once to delete cached files.

    Here'd be the code snippet to replace CRLF line endings of Markdown files with LF:

    // e.g. server/plugins/crlf-to-lf.ts
    export default defineNitroPlugin((nitroApp) => {
      nitroApp.hooks.hook('content:file:beforeParse', (file) => {
        if (file._id.endsWith('.md')) {
          file.body = file.body.replace(/\r\n|\r/g, '\n')
        }
      })
    })
  5. dreamgear commented on May 6, 2025

    @dreamgear

    This is still happening in Nuxt 3.17.1. It's not code or md, but rather the data being fetched from my api:

    `runtime-core.esm-bundler.js:51 [Vue warn]: Hydration text mismatch in <td data-pinned=​"false" class=​"...">​…​​

    • rendered on server: "9# GREEN FRUIT TISSUE 55" \n"
    • expected on client: "9# GREEN FRUIT TISSUE 55" \r\n" `

    I've gotten various suggestions on how to fix it, but I think the warnings would then resume when I deploy on Linux.

  6. Rednas83 commented on Jun 12, 2025

    @Rednas83

    Just stumbled on the issue that newLine is not displayed in the browser.

    Solution is to set the whitespace to pre-line in css
    <ContentRenderer :value="data || {}" :style="{ whiteSpace: 'pre-line' }" />
    or with tailwind if you use that
    <ContentRenderer :value="data || {}" class="whitespace-pre-line" />

    To fix the hydration issue just change the server's newLine to \n to match the browser
    Image
    Whereby LF is \n and CRLF is \r\n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions