Unverified Commit f8f308d6 authored by Martin Bavio's avatar Martin Bavio Committed by GitHub

Fix examples using __NEXT_DATA__ (#13567)

This PR fixes https://github.com/vercel/next.js/issues/4883.

It introduces a new global variable (`window.__REHYDRATE_IDS`) only when necessary.

In the case of the `with-react-with-styles` example, rehydration isn't happening, so I removed the creation of the variable. 

In the case of the `with-react-intl` example, the information doesn't seem to be set anymore on `__NEXT_DATA__`, so I just removed the default reference to it.
parent f6975740
import Document, { Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
import { StyleSheetServer } from 'aphrodite' import { StyleSheetServer } from 'aphrodite'
export default class MyDocument extends Document { class MyDocument extends Document {
static async getInitialProps({ renderPage }) { static async getInitialProps({ renderPage }) {
const { html, css } = StyleSheetServer.renderStatic(() => renderPage()) const { html, css } = StyleSheetServer.renderStatic(() => renderPage())
const ids = css.renderedClassNames const ids = css.renderedClassNames
return { ...html, css, ids } 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 /* 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 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 the client goes to render styles. If you don't you'll get a second
<style> tag */ <style> tag */
const { css, ids } = this.props
return ( return (
<html> <Html>
<Head> <Head>
<style <style
data-aphrodite data-aphrodite
dangerouslySetInnerHTML={{ __html: this.props.css.content }} dangerouslySetInnerHTML={{ __html: css.content }}
/> />
</Head> </Head>
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
{ids && (
<script
dangerouslySetInnerHTML={{
__html: `
window.__REHYDRATE_IDS = ${JSON.stringify(ids)}
`,
}}
/>
)}
</body> </body>
</html> </Html>
) )
} }
} }
export default MyDocument
import { StyleSheet, css } from 'aphrodite' import { StyleSheet, css } from 'aphrodite'
// Rehydrate to ensure that the client doesn't duplicate styles
// It has to execute before any code that defines styles
// '__REHYDRATE_IDS' is set in '_document.js'
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
/* StyleSheet.rehydrate takes an array of rendered classnames, StyleSheet.rehydrate(window.__REHYDRATE_IDS)
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 function Home() { export default function Home() {
......
import Document, { Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
import { renderStatic } from 'glamor/server' import { renderStatic } from 'glamor/server'
export default class MyDocument extends Document { class MyDocument extends Document {
static async getInitialProps({ renderPage }) { static async getInitialProps({ renderPage }) {
const page = renderPage() const page = renderPage()
const styles = renderStatic(() => page.html || page.errorHtml) const { css, ids } = renderStatic(() => page.html || page.errorHtml)
return { ...page, ...styles } return { ...page, css, ids }
}
constructor(props) {
super(props)
const { __NEXT_DATA__, ids } = props
if (ids) {
__NEXT_DATA__.ids = this.props.ids
}
} }
render() { render() {
const { ids, css } = this.props
return ( return (
<html> <Html>
<Head> <Head>
<style dangerouslySetInnerHTML={{ __html: this.props.css }} /> <style dangerouslySetInnerHTML={{ __html: css }} />
</Head> </Head>
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
{ids && (
<script
dangerouslySetInnerHTML={{
__html: `
window.__REHYDRATE_IDS = ${JSON.stringify(ids)}
`,
}}
/>
)}
</body> </body>
</html> </Html>
) )
} }
} }
export default MyDocument
import { rehydrate } from 'glamor' import { rehydrate, css } from 'glamor'
// Adds server generated styles to glamor cache. // Rehydrate to ensure that the client doesn't duplicate styles
// Has to run before any `style()` calls // It has to execute before any code that defines styles
// '__NEXT_DATA__.ids' is set in '_document.js' // '__REHYDRATE_IDS' is set in '_document.js'
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
rehydrate(window.__NEXT_DATA__.ids) rehydrate(window.__REHYDRATE_IDS)
} }
const rule = css({
color: 'red',
fontSize: 50,
})
export default function Home() { export default function Home() {
return <h1 css={{ color: 'red', fontSize: 50 }}>My page</h1> return <h1 {...rule}>My page</h1>
} }
...@@ -16,7 +16,7 @@ export default class MyApp extends App { ...@@ -16,7 +16,7 @@ export default class MyApp extends App {
// Get the `locale` and `messages` from the request object on the server. // Get the `locale` and `messages` from the request object on the server.
// In the browser, use the same values that the server serialized. // In the browser, use the same values that the server serialized.
const { req } = ctx const { req } = ctx
const { locale, messages } = req || window.__NEXT_DATA__.props const { locale, messages } = req
return { pageProps, locale, messages } return { pageProps, locale, messages }
} }
......
import Document, { Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
import { StyleSheetServer } from 'aphrodite' import { StyleSheetServer } from 'aphrodite'
export default class MyDocument extends Document { class MyDocument extends Document {
static async getInitialProps({ renderPage }) { static async getInitialProps({ renderPage }) {
const { html, css } = StyleSheetServer.renderStatic(() => renderPage()) const { html, css } = StyleSheetServer.renderStatic(() => renderPage())
const ids = css.renderedClassNames const ids = css.renderedClassNames
return { ...html, css, ids } 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 /* 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 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 the client goes to render styles. If you don't you'll get a second
<style> tag */ <style> tag */
const { css, ids } = this.props
return ( return (
<html> <Html>
<Head> <Head>
<style <style
data-aphrodite data-aphrodite
dangerouslySetInnerHTML={{ __html: this.props.css.content }} dangerouslySetInnerHTML={{ __html: css.content }}
/> />
</Head> </Head>
<body> <body>
<Main /> <Main />
<NextScript /> <NextScript />
{ids && (
<script
dangerouslySetInnerHTML={{
__html: `
window.__REHYDRATE_IDS = ${JSON.stringify(ids)}
`,
}}
/>
)}
</body> </body>
</html> </Html>
) )
} }
} }
export default MyDocument
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