[QUESTION] How to avoid hydration for static content
See original GitHub issueWe 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:
- Created 4 years ago
- Reactions:1
- Comments:12 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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.