Skip to content

Commit fbf4fdb

Browse files
committed
Create react-static-content package
1 parent 8bb243e commit fbf4fdb

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-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+
## Static Content 📸
2+
3+
Server Side Rendering for a component and skip the hydration step on the client. Useful for components that don't need to use interactivity (like SEO links).
4+
5+
## Results of using it
6+
7+
### Benefits... 👍
8+
9+
⚡ Avoid re-hydrate for static components
10+
🤳 Thus could greatly improve TTI
11+
12+
### Downsides... 👎
13+
⚠️ Lose interactivity
14+
🏋️‍ Hydration data still there
15+
🥪 Element wrapper (ex. <div>)
16+
17+
### Keep in mind... 🧠
18+
📸 For expensive rendering lists or static content (SEO Footers)
19+
🤖 GoogleBot is definitely going to detect it
20+
21+
## How to use 👨‍🏫
22+
Just wrap the components you want to be static on the client.
23+
24+
```javascript
25+
import StaticContent from '@midudev/react-static-content'
26+
27+
export default function SeoFooter({listOfLinks}) {
28+
return (
29+
<footer>
30+
<StaticContent>
31+
{listOfLinks.map((link, idx) => (
32+
<a key={idx} href={link.href} title={link.literal} />
33+
<span>{link.literal}</span>
34+
</a>
35+
))}
36+
</StaticContent>
37+
</footer>
38+
)
39+
}
40+
```
41+
42+
43+
## Resources 🔗
44+
[Tweet about suppressHydrationWarning](https://twitter.com/reactjs/status/928650326701494273?lang=es)
45+
[suppressHydrationWarning Pull Request](https://github.com/facebook/react/pull/11126)
46+
[Hack for avoiding hydration](https://github.com/facebook/react/issues/10923#issuecomment-338715787)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const {useEffect, useRef, useState} = React
2+
3+
export default function StaticContent({children}) {
4+
const ref = useRef(null)
5+
const [render, setRender] = useState(typeof window === 'undefined')
6+
7+
useEffect(function() {
8+
// check if the innerHTML is empty as client side navigation
9+
// need to render the component without server-side backup
10+
const isEmpty = ref.current.innerHTML === ''
11+
if (isEmpty) {
12+
setRender(true)
13+
}
14+
}, [])
15+
16+
// if we're in the server or a spa navigation, just render it
17+
if (render) {
18+
return <div>{children}</div>
19+
}
20+
21+
// avoid re-render on the client
22+
return (
23+
<div
24+
ref={ref}
25+
suppressHydrationWarning
26+
dangerouslySetInnerHTML={{__html: ''}}
27+
/>
28+
)
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@midudev/react-static-content",
3+
"version": "1.0.0",
4+
"description": "Server Side Rendering for a component and skip the hydration step on the client. Useful for components that don't need to use interactivity (like SEO links).",
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)