Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An example with react-helmet #1264

Merged
merged 13 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/with-react-helmet/.DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions examples/with-react-helmet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# react-helmet example

## 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-react-helmet
cd with-react-helmet
```

Install it and run:

```bash
npm install
npm run dev
```
_Or alternatively:_
```bash
yarn
yarn run dev
```


Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))

```bash
now
```
## Notes
This an minimalistic example of how to combine next.js and [react-helmet](https://github.com/nfl/react-helmet).
The title of the page shall be changed to "Hello next.js!"
The rest is all up to you.
16 changes: 16 additions & 0 deletions examples/with-react-helmet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "with-react-helmet",
"license": "ISC",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "^2.0.0-beta",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-helmet": "^4.0.0"
}
}
45 changes: 45 additions & 0 deletions examples/with-react-helmet/pages/_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Document, { Head, Main, NextScript } from 'next/document'
import Helmet from 'react-helmet'

export default class extends Document {
static async getInitialProps ({ renderPage }) {
// see https://github.com/nfl/react-helmet#server-usage for more information
// 'head' was occupied by 'renderPage().head', we cannot use it
return { ...renderPage(), helmet: Helmet.rewind() }
}

// should render on <html>
get helmetHtmlAttrComponents () {
return this.props.helmet.htmlAttributes.toComponent()
}

// should render on <head>
get helmetHeadComponents () {
return Object.keys(this.props.helmet)
.filter(el => el !== 'htmlAttributes') // remove htmlAttributes which is not for <head> but for <html>
.map(el => this.props.helmet[el].toComponent())
}

get helmetJsx () {
return (<Helmet
htmlAttributes={{lang: 'en'}}
title='Hello next.js!'
meta={[
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }
]}
/>)
}

render () {
return (<html {...this.helmetHtmlAttrComponents}>
<Head>
{ this.helmetJsx }
{ this.helmetHeadComponents }
</Head>
<body>
<Main />
<NextScript />
</body>
</html>)
}
}
3 changes: 3 additions & 0 deletions examples/with-react-helmet/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => (<div>
Hello World!
</div>)