Hydration mismatch when using text from raw file

I am trying to read out a raw text file and use it's content in a component. However this always produces a hydration text mismatch with an identical output. I am reading the files like this:
import base from '~/assets/data/compose-pieces/base.yaml?raw';
import base from '~/assets/data/compose-pieces/base.yaml?raw';
And every component using it will just get a text mismatch, like this codeblock:
<CodeBlock :content="base" :showCopy="true" />
<CodeBlock :content="base" :showCopy="true" />
The component is simply using the prop in the template. If I use the encodeURI function on the text it doesn't produce a text mismatch. If I decode it again with decodeURI it will produce a type mismatch again. My guess is that the encoding is different when in SSR. Does somebody know what to do to prevent this behavior without disabling SSR for this component.
No description
4 Replies
kapa.ai
kapa.ai3w ago
Hi @GlitchDev. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- You're experiencing a hydration mismatch issue when using raw text files in your Nuxt application. This is a common problem in SSR applications where the server and client render slightly different content. Why This Happens The issue is related to how raw text is processed during server-side rendering versus client-side hydration. When you import a raw file, the encoding or character representation might differ slightly between server and client environments, causing Vue to detect a mismatch during hydration. This is similar to issues seen with other data manipulation operations in Nuxt SSR, where modifying data can cause hydration mismatches as documented in Stack Overflow. Solutions Here are a few approaches to solve this issue: 1. Use <client-only> Component Wrap your component in a <client-only> tag to skip server-side rendering for this specific component:
<client-only>
<CodeBlock :content="base" :showCopy="true" />
</client-only>
<client-only>
<CodeBlock :content="base" :showCopy="true" />
</client-only>
This is the simplest solution but means your content won't be pre-rendered on the server. 2. Create a Consistent Encoding/Decoding Process Since you mentioned that using encodeURI prevents the mismatch, you could create a consistent process:
// In your setup
const encodedBase = ref(encodeURI(base));

// Then in your template
<CodeBlock :content="encodedBase" :showCopy="true" />
// In your setup
const encodedBase = ref(encodeURI(base));

// Then in your template
<CodeBlock :content="encodedBase" :showCopy="true" />
Make sure your CodeBlock component decodes the content consistently. 3. Define Raw Content in Schema (for Content Files) If you're using Nuxt Content, you can define a rawbody field in your schema to properly handle raw content as mentioned in the Nuxt Content documentation:
// content.config.ts
import { defineCollection, defineContentConfig, z } from '@nuxt/content'

export default defineContentConfig({
collections: {
docs: defineCollection({
source: '**',
type: 'page',
schema: z.object({
rawbody: z.string()
})
})
}
})
// content.config.ts
import { defineCollection, defineContentConfig, z } from '@nuxt/content'

export default defineContentConfig({
collections: {
docs: defineCollection({
source: '**',
type: 'page',
schema: z.object({
rawbody: z.string()
})
})
}
})
4. Create a Copy of the Data Similar to the solution for array manipulation issues, create a copy of your data to avoid modifying the original:
const rawBase = ref(base);
const rawBase = ref(base);
Best Practice The most reliable approach would be to use encodeURI/decodeURI consistently or to create a computed property that processes the raw content in a way that produces identical results on both server and client. Remember that hydration mismatches occur when the server-rendered HTML doesn't match what the client would render with the same data. Ensuring consistent data processing between server and client is key to avoiding these issues. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@691226066580144148> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
manniL
manniL3w ago
random idea: might be related to line breaks?
GlitchDev
GlitchDevOP2w ago
Sorry I saw the reply just now. Yes that's pretty likely. I tested it with a file without line breaks and it did not create a hydration text mismatch. But I'm unsure why that is so.

Did you find this page helpful?