Skip to content

Commit

Permalink
Merge pull request #1 from zeit/add/css
Browse files Browse the repository at this point in the history
Added css using aphrodite
  • Loading branch information
nkzawa committed Oct 6, 2016
2 parents 20e90ed + 7a39dac commit 22cfcf5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/css.js
@@ -0,0 +1 @@
module.exports = require('aphrodite')
7 changes: 5 additions & 2 deletions lib/document.js
Expand Up @@ -25,10 +25,13 @@ export default class Document extends Component {
}

export function Head (props, context) {
const { head } = context._documentProps
const { head, css } = context._documentProps
const h = (head || [])
.map((h, i) => React.cloneElement(h, { key: '_next' + i }))
return <head>{h}</head>
return <head>
{h}
<style data-aphrodite>{css.content}</style>
</head>
}

Head.contextTypes = { _documentProps: PropTypes.any }
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -14,6 +14,7 @@
"test": "ava"
},
"dependencies": {
"aphrodite": "0.5.0",
"babel-core": "6.17.0",
"babel-generator": "6.17.0",
"babel-loader": "6.2.5",
Expand Down Expand Up @@ -41,6 +42,7 @@
},
"devDependencies": {
"ava": "0.16.0",
"del": "2.2.2",
"gulp": "3.9.1",
"gulp-babel": "6.1.2",
"gulp-cached": "1.1.0",
Expand Down
16 changes: 11 additions & 5 deletions server/render.js
Expand Up @@ -4,6 +4,7 @@ import { renderToString, renderToStaticMarkup } from 'react-dom/server'
import fs from 'mz/fs'
import Document from '../lib/document'
import App from '../lib/app'
import { StyleSheetServer } from '../lib/css'

export async function render (path, req, res, { root = process.cwd() } = {}) {
const mod = require(resolve(root, '.next', 'pages', path)) || {}
Expand All @@ -17,15 +18,20 @@ export async function render (path, req, res, { root = process.cwd() } = {}) {
const bundlePath = resolve(root, '.next', '.next', 'pages', (path || 'index') + '.js')
const component = await fs.readFile(bundlePath, 'utf8')

const app = createElement(App, {
Component,
props,
router: {}
const { html, css } = StyleSheetServer.renderStatic(() => {
const app = createElement(App, {
Component,
props,
router: {}
})

return renderToString(app)
})

const doc = createElement(Document, {
head: [],
html: renderToString(app),
html: html,
css: css,
data: { component },
hotReload: false
})
Expand Down

0 comments on commit 22cfcf5

Please sign in to comment.