Skip to content

Commit e1babdf

Browse files
timneutkensarunoda
authored andcommitted
Render styled-jsx in _document example (vercel#1287)
1 parent 1971517 commit e1babdf

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

examples/with-cxs/pages/_document.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import cxs from 'cxs'
44
export default class MyDocument extends Document {
55
static async getInitialProps ({ renderPage }) {
66
const page = renderPage()
7-
let style = cxs.getCss()
7+
const style = cxs.getCss()
88
return { ...page, style }
99
}
1010

examples/with-styled-components/pages/_document.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import styleSheet from 'styled-components/lib/models/StyleSheet'
44
export default class MyDocument extends Document {
55
static async getInitialProps ({ renderPage }) {
66
const page = renderPage()
7-
const style = styleSheet.rules().map(rule => rule.cssText).join('\n')
8-
return { ...page, style }
7+
const styles = styleSheet.rules().map(rule => rule.cssText).join('\n')
8+
return { ...page, styles }
99
}
1010

1111
render () {
1212
return (
1313
<html>
1414
<Head>
1515
<title>My page</title>
16-
<style dangerouslySetInnerHTML={{ __html: this.props.style }} />
1716
</Head>
1817
<body>
1918
<Main />

readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,13 @@ Pages in `Next.js` skip the definition of the surrounding document's markup. For
473473
```jsx
474474
// ./pages/_document.js
475475
import Document, { Head, Main, NextScript } from 'next/document'
476+
import flush from 'styled-jsx/server'
476477

477478
export default class MyDocument extends Document {
478-
static async getInitialProps (ctx) {
479-
const props = await Document.getInitialProps(ctx)
480-
return { ...props, customValue: 'hi there!' }
479+
static getInitialProps ({ renderPage }) {
480+
const {html, head} = renderPage()
481+
const styles = flush()
482+
return { html, head, styles }
481483
}
482484

483485
render () {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy