Skip to content

Commit 584747e

Browse files
committed
Create react-progressive-hydration package
1 parent fbf4fdb commit 584747e

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2019, Miguel Ángel Durán ([email protected])
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Progressive Hydration 🏁
2+
3+
Server Side Rendering for a component and skip the hydration step on the client but, as soon as it appears on the viewport, hydrate it. Useful for list of items or components that are not visible on the viewport but yet you need to render them for SEO.
4+
5+
## Results of using it
6+
7+
### Benefits... 👍
8+
9+
👀 Only re-hydrate what's visible
10+
🤳 Thus could greatly improve TTI
11+
🔛 Activate interactivity on demand
12+
13+
### Downsides... 👎
14+
🏋️‍ Hydration data still there
15+
🥪 Element wrapper needed (ex. <div>)
16+
17+
### Keep in mind... 🧠
18+
📸 Kind of lazy loading experience
19+
🤖 GoogleBot will get the rendered static html (not hydrated)
20+
21+
## How to use 👨‍🏫
22+
Just wrap the components you want to be hydrated progressively.
23+
24+
Use `force` prop in order to hydrate the component no matter if it's below the fold.
25+
26+
```javascript
27+
import ProgressiveHydration from '@midudev/react-progressive-hydration'
28+
29+
export default function ProgressiveHydrationPage({articles}) {
30+
return (
31+
<Grid>
32+
{articles.map((article, idx) => (
33+
<ProgressiveHydration key={idx} force={idx < 3}>
34+
<Card {...article} />
35+
</ProgressiveHydration>
36+
))}
37+
</Grid>
38+
)
39+
}
40+
```
41+
42+
43+
## Resources 🔗
44+
[The case of partial hydration (with Next and Preact)](https://medium.com/spring-media-techblog/how-we-achieved-the-best-web-performance-with-partial-hydration-20fab9c808d5)
45+
[Rendering on the Web: Performance Implications of Application Architecture](https://www.youtube.com/watch?v=k-A2VfuUROg)
46+
[Hack for avoiding hydration](https://github.com/facebook/react/issues/10923#issuecomment-338715787)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {useNearScreen} from '../useNearScreen'
2+
3+
const {useRef} = React
4+
5+
export default function ProgressiveHydration({children, force}) {
6+
const ref = useRef(null)
7+
const render = typeof window === 'undefined' || force
8+
const isNearScreen = useNearScreen({ref})
9+
10+
// we're in the server or the element is above the fold
11+
if (render || isNearScreen) {
12+
return <div>{children}</div>
13+
}
14+
15+
// avoid hydration until is in the viewport
16+
return (
17+
<div
18+
ref={ref}
19+
suppressHydrationWarning
20+
dangerouslySetInnerHTML={{__html: ''}}
21+
/>
22+
)
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@midudev/react-progressive-hydration",
3+
"version": "1.0.0",
4+
"description": "Server Side Rendering for a component and skip the hydration step on the client but, as soon as it appears on the viewport, then hydrate it. Useful for list of items.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": ["react", "react rendering", "dynamic rendering", "static rendering", "progressive hydration"],
10+
"author": "Miguel Ángel Durán (@midudev) [email protected]",
11+
"license": "ISC",
12+
"peerDependencies": {
13+
"react": ">=16.8.0"
14+
}
15+
}
16+

0 commit comments

Comments
 (0)