Commit 8d8d5ebd authored by Robin Frischmann's avatar Robin Frischmann Committed by Dan Zajdband

Example using Fela (#863)

* added example using fela

* update package-json

* removed nested routing test

* fixed linting issues

* fixed typo
parent 2390c87c
# Example app with Fela
## How to use
Download the example [or clone the repo](https://github.com/zeit/next.js):
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-fela
cd with-fela
```
Install it and run:
```bash
npm install
npm run dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example features how to use a different styling solution than [styled-jsx](https://github.com/zeit/styled-jsx) that also supports universal styles. That means we can serve the required styles for the first render within the HTML and then load the rest in the client. In this case we are using [fela](https://github.com/rofrischmann/fela).
For this purpose we are extending the `<Document />` and injecting the server side rendered styles into the `<head>`.
import { createRenderer } from 'fela'
// add your renderer configuration here
const renderer = createRenderer()
export function getRenderer () {
return renderer
}
export function getMountNode () {
if (typeof window !== 'undefined') {
return document.getElementById('fela-stylesheet')
}
return undefined
}
import { Provider } from 'react-fela'
import { getRenderer, getMountNode } from './fela'
export default ({ children }) => (
<Provider renderer={getRenderer()} mountNode={getMountNode()}>
{children}
</Provider>
)
{
"name": "with-fela",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"fela": "^4.1.2",
"next": "^2.0.0-beta",
"react-fela": "^4.1.2"
}
}
\ No newline at end of file
import Document, { Head, Main, NextScript } from 'next/document'
import { getRenderer } from '../fela'
export default class MyDocument extends Document {
static getInitialProps ({ renderPage }) {
const page = renderPage()
const renderer = getRenderer()
const css = renderer.renderToString()
renderer.clear()
return {
...page,
css
}
}
render () {
return (
<html>
<Head>
<title>My page</title>
<style id='fela-stylesheet'>{this.props.css}</style>
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
)
}
}
import { createComponent } from 'react-fela'
import Page from '../layout'
const title = ({ size }) => ({
fontSize: size + 'px',
color: 'red'
})
const Title = createComponent(title, 'h1')
export default () => (
<Page>
<Title size={50}>My Title</Title>
</Page>
)
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