File tree Expand file tree Collapse file tree 3 files changed +127
-92
lines changed
examples/with-aphrodite/pages Expand file tree Collapse file tree 3 files changed +127
-92
lines changed Original file line number Diff line number Diff line change @@ -4,15 +4,31 @@ import { StyleSheetServer } from 'aphrodite'
4
4
export default class MyDocument extends Document {
5
5
static async getInitialProps ( { renderPage } ) {
6
6
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
+ }
8
20
}
9
21
10
22
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 */
11
27
return (
12
28
< html >
13
29
< Head >
14
30
< title > My page</ title >
15
- < style dangerouslySetInnerHTML = { { __html : this . props . css . content } } />
31
+ < style data-aphrodite dangerouslySetInnerHTML = { { __html : this . props . css . content } } />
16
32
</ Head >
17
33
< body >
18
34
< Main />
Original file line number Diff line number Diff line change 1
1
import React from 'react'
2
2
import { StyleSheet , css } from 'aphrodite'
3
3
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
+
4
11
export default ( ) => (
5
12
< div className = { css ( styles . root ) } >
6
13
< h1 className = { css ( styles . title ) } > My page</ h1 >
You can’t perform that action at this time.
0 commit comments