Logo Dark
Get In Touch

Nuxt 3 & Brotli Compression: A Deep Dive Into Hydration Mismatch Solutions

31 August 2023

Tech

Table of contents

Nuxt Heading Image

🧠 Understanding Hydration Mismatch

  • Hydration mismatch occurs when there's a discrepancy between the initial HTML sent by the server and the client-rendered content.
  • During server-side rendering, Nuxt sends a pre-rendered HTML along with JavaScript code to the client.
  • This HTML is then "hydrated" by the client-side JavaScript, turning it into interactive components.

However, if there are differences between the initial HTML and the client-rendered content, a mismatch arises. This can happen due to various reasons, including:

  • Incorrect HTML code. Using div tag inside p tag is invalid. ( This is the most common and easy to solve )
  • Cloudflare Brotli Compression Settings - 😨 ( We will talk about this reason in this article Since this is not much common and it is difficult sometimes to strike )

✨ Example Scenarios

  • Let's consider a scenario where a Nuxt application uses Cloudflare's aggressive Brotli compression.
  • The server-side rendered HTML contains certain DOM nodes and JavaScript code.
  • However, due to the high compression settings, the JavaScript code received on the client side might be slightly altered.
  • As a result, when the client-side rendering kicks in, it doesn't match the initial HTML, leading to a hydration mismatch.
  <!-- Server-side rendered HTML -->
  <div id="app">
  <!--  Just observe below line Client Side -->
    <div v-if="isReady"></div>  
    <p>Hello from server!</p>
  </div>
  <script>
    // JavaScript code
  </script>
 <!-- Client Side -->
  <div id="app">
  <!--  Here lies the problem -->
    <!--[-->  
    <p>Hello from server!</p>
  </div>
  <script>
    // JavaScript code
  </script>
Element Log Nuxt

🤔 Problem

  • One of the problems associated with Cloudflare's Brotli compression settings is the removal of comments from the source code during compression.
  • This can lead to discrepancies between the comments present in the server-side code and what the client-side JavaScript receives.

🔧 Solution

  • Turn off Compression
Turn off Compression

Helpful Resources

🚀 Conclusion

  • Hydration mismatch issues can be frustrating for both developers and users.
  • While Cloudflare's Brotli compression settings can provide substantial performance gains, they can inadvertently trigger discrepancies between server and client rendering.
  • By understanding the impact of Brotli compression, adjusting settings wisely, and following best practices
  • Happy coding! 💻🌟

WRITTEN BY

Bhavin Panchal

Bhavin is a Nuxt.js maestro, building fast, scalable, and optimized web applications. He turns technical challenges into smooth user experiences, making sure that every app he touches performs at its best.

More from this author

Making IT Possible

Making IT Possible

Making IT Possible

Making IT Possible

Making IT Possible

Making IT Possible

India (HQ)

201, iSquare Corporate Park, Science City Road, Ahmedabad-380060, Gujarat, India

Canada

24 Merlot Court, Timberlea, NS B3T 0C2, Canada

For Sales

[email protected]

Looking For Jobs

Apply Now

LinkedIn
Instagram
X
Facebook
Youtube
Discord
Dribbble
Behance
Github