question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to avoid hydration for static content

See original GitHub issue

We recently switched from React to Preact and one of the things that we miss in performance is that with React we had a hack to avoid Hydration for static content.

I had this code (Suggested on https://github.com/facebook/react/issues/10923#issuecomment-338715787):

<StaticContent><MyStaticComponent /></StaticContent>
import { createElement, useRef, useState, useEffect } from 'react'

function useStaticContent() {
  const ref = useRef(null)
  const [render, setRender] = useState(typeof window === 'undefined')

  useEffect(() => {
    // check if the innerHTML is empty as client side navigation
    // need to render the component without server-side backup
    const isEmpty = ref.current.innerHTML === ''
    if (isEmpty) {
      setRender(true)
    }
  }, [])

  return [render, ref]
}

export default function StaticContent({ children, element = 'div', ...props }) {
  const [render, ref] = useStaticContent()

  // if we're in the server or a spa navigation, just render it
  if (render) {
    return createElement(element, {
      ...props,
      children,
    })
  }

  // avoid re-render on the client
  return createElement(element, {
    ...props,
    ref,
    suppressHydrationWarning: true,
    dangerouslySetInnerHTML: { __html: '' },
  })
}

However, in Preact this hack is not avoiding the hydration… Is there another way to do it? 👏

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
JoviDeCroockcommented, Mar 8, 2020

Hmm in a real SSR scenario this shouldn’t happen but yes I can see why this happens in this test, I’ll see if I can do something about it

2reactions
aralrocacommented, Feb 25, 2020

Thank you @JoviDeCroock and @developit ! I just reported yesterday and today is already fixed! This is incredible and I feel very happy with the change to Preact ecosystem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Efficient Hydration in JavaScript Frameworks is so ...
The challenge is that to re-render the static parts on the server you need specialized data format to be able to diff the...
Read more >
The Hydration Problem - Incremental Elm
Let's take an honest look at whether Elm is a good fit for building static sites.
Read more >
Hydration is Pure Overhead - Builder.io
In summary, hydration is recovering event handlers by downloading and re-executing all components in the SSR/SSG-rendered HTML. The site is sent ...
Read more >
The Perils of Rehydration - Josh W Comeau
The first pass, at compile-time, produces all of the static non-personal content, and leaves holes where the dynamic content will go. Then, ...
Read more >
Partial Hydration: Making the Static Interactive - YouTube
What is Hydration ? · Islands, Partial Hydration, & JavaScript Frameworks · TanStack Router: Type safe routing and URL state management for your ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found