Commit 7c8d0246 authored by Grokling's avatar Grokling Committed by Tim Neutkens

Add rehydration to example/aphrodite (#1858)

* Add rehydration to example/aphrodite

* linting

* replaced yarn.lock
parent 17ce0401
...@@ -4,15 +4,31 @@ import { StyleSheetServer } from 'aphrodite' ...@@ -4,15 +4,31 @@ import { StyleSheetServer } from 'aphrodite'
export default class MyDocument extends Document { export default class MyDocument extends Document {
static async getInitialProps ({ renderPage }) { static async getInitialProps ({ renderPage }) {
const { html, css } = StyleSheetServer.renderStatic(() => renderPage()) const { html, css } = StyleSheetServer.renderStatic(() => renderPage())
return { ...html, css } const ids = css.renderedClassNames
return { ...html, css, ids }
}
constructor (props) {
super(props)
/* Take the renderedClassNames from aphrodite (as generated
in getInitialProps) and assign them to __NEXT_DATA__ so that they
are accessible to the client for rehydration. */
const { __NEXT_DATA__, ids } = props
if (ids) {
__NEXT_DATA__.ids = this.props.ids
}
} }
render () { render () {
/* Make sure to use data-aphrodite attribute in the style tag here
so that aphrodite knows which style tag it's in control of when
the client goes to render styles. If you don't you'll get a second
<style> tag */
return ( return (
<html> <html>
<Head> <Head>
<title>My page</title> <title>My page</title>
<style dangerouslySetInnerHTML={{ __html: this.props.css.content }} /> <style data-aphrodite dangerouslySetInnerHTML={{ __html: this.props.css.content }} />
</Head> </Head>
<body> <body>
<Main /> <Main />
......
import React from 'react' import React from 'react'
import { StyleSheet, css } from 'aphrodite' import { StyleSheet, css } from 'aphrodite'
if (typeof window !== 'undefined') {
/* StyleSheet.rehydrate takes an array of rendered classnames,
and ensures that the client side render doesn't generate
duplicate style definitions in the <style data-aphrodite> tag */
StyleSheet.rehydrate(window.__NEXT_DATA__.ids)
}
export default () => ( export default () => (
<div className={css(styles.root)}> <div className={css(styles.root)}>
<h1 className={css(styles.title)}>My page</h1> <h1 className={css(styles.title)}>My page</h1>
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment