Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c8d024

Browse files
Groklingtimneutkens
authored andcommitted
Add rehydration to example/aphrodite (vercel#1858)
* Add rehydration to example/aphrodite * linting * replaced yarn.lock
1 parent 17ce040 commit 7c8d024

File tree

3 files changed

+127
-92
lines changed

3 files changed

+127
-92
lines changed

examples/with-aphrodite/pages/_document.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@ import { StyleSheetServer } from 'aphrodite'
44
export default class MyDocument extends Document {
55
static async getInitialProps ({ renderPage }) {
66
const { html, css } = StyleSheetServer.renderStatic(() => renderPage())
7-
return { ...html, css }
7+
const ids = css.renderedClassNames
8+
return { ...html, css, ids }
9+
}
10+
11+
constructor (props) {
12+
super(props)
13+
/* Take the renderedClassNames from aphrodite (as generated
14+
in getInitialProps) and assign them to __NEXT_DATA__ so that they
15+
are accessible to the client for rehydration. */
16+
const { __NEXT_DATA__, ids } = props
17+
if (ids) {
18+
__NEXT_DATA__.ids = this.props.ids
19+
}
820
}
921

1022
render () {
23+
/* Make sure to use data-aphrodite attribute in the style tag here
24+
so that aphrodite knows which style tag it's in control of when
25+
the client goes to render styles. If you don't you'll get a second
26+
<style> tag */
1127
return (
1228
<html>
1329
<Head>
1430
<title>My page</title>
15-
<style dangerouslySetInnerHTML={{ __html: this.props.css.content }} />
31+
<style data-aphrodite dangerouslySetInnerHTML={{ __html: this.props.css.content }} />
1632
</Head>
1733
<body>
1834
<Main />

examples/with-aphrodite/pages/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import React from 'react'
22
import { StyleSheet, css } from 'aphrodite'
33

4+
if (typeof window !== 'undefined') {
5+
/* StyleSheet.rehydrate takes an array of rendered classnames,
6+
and ensures that the client side render doesn't generate
7+
duplicate style definitions in the <style data-aphrodite> tag */
8+
StyleSheet.rehydrate(window.__NEXT_DATA__.ids)
9+
}
10+
411
export default () => (
512
<div className={css(styles.root)}>
613
<h1 className={css(styles.title)}>My page</h1>

0 commit comments

Comments
 (0)