Skip to content

Commit 10b22b9

Browse files
committed
Create react-dynamic-rendering package
1 parent 584747e commit 10b22b9

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-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 (miduga@gmail.com)
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Dynamic Rendering 🔀
2+
3+
If your visitor is a bot, like GoogleBot or Yandex, use Server Side Rendering and Client Side Rendering.
4+
5+
If your visitor is a real user, use Client Side Rendering.
6+
7+
## Results of using it
8+
9+
### Benefits... 👍
10+
11+
✅ Improve TTI
12+
😴 Lazy Load for the user
13+
14+
### Downsides... 👎
15+
🚩 Keep Hydration data
16+
👩‍🔬 Need universal User Agent data
17+
🤖 Bot still gets full cost
18+
19+
### Keep in mind... 🧠
20+
🏋️‍ Perfect for stuff below the fold
21+
🆓 Free resources from your server
22+
⏳ Help GoogleBot to index your content faster
23+
24+
## How to use 👨‍🏫
25+
Just wrap the components you want to be static on the client.
26+
27+
```javascript
28+
import DynamicRendering from '@midudev/react-dynamic-rendering'
29+
30+
export default function DynamicRenderingPage({articles}) {
31+
return (
32+
<Grid>
33+
{articles.map((article, idx) => (
34+
<ProgressiveHydration key={idx} force={idx < 3}>
35+
<Card {...article} />
36+
</ProgressiveHydration>
37+
))}
38+
</Grid>
39+
)
40+
}
41+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {useNearScreen} from '../useNearScreen'
2+
3+
const {useRef} = React
4+
5+
export default function DynamicRendering({children, isBot, force}) {
6+
const ref = useRef(null)
7+
const isNearScreen = useNearScreen({ref})
8+
9+
if (isBot || isNearScreen || force) {
10+
return children
11+
} else {
12+
return <div ref={ref} style={{height: `100px`, border: '1px solid red'}} />
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@midudev/react-dynamic-rendering",
3+
"version": "1.0.0",
4+
"description": "Dynamic Rendering allows you to render React components different depending on user agent. If your visitor is a bot, like GoogleBot or Yandex, use Server Side Rendering + hydration. If your visitor is a real user, use only Client Side Rendering.",
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) miduga@gmail.com",
11+
"license": "ISC",
12+
"peerDependencies": {
13+
"react": ">=16.8.0"
14+
}
15+
}

0 commit comments

Comments
 (0)