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 10 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
3 changes: 3 additions & 0 deletions examples/with-react-helmet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/yarn.lock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use global gitignore for these.

node_modules/
Copy link
Member

@timneutkens timneutkens Feb 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant all of them 😅 Sorry was on my phone then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the whole .gitignore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the parent .gitignore already takes care of ignoring node_modules. As for ignoring the .DS_Store, you could put it in your own ~/.gitignore_global as it's specific to your system (after having removed the files from this PR).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yarn.lock is also caught by the .gitignore in the examples directory btw.

**/.DS_Store
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"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add author license and version to be consistent with https://github.com/zeit/next.js/blob/master/examples/basic-css/package.json#L14-L15

43 changes: 43 additions & 0 deletions examples/with-react-helmet/pages/_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Document, { Head, Main, NextScript } from 'next/document'
import Helmet from 'react-helmet'

export default class extends Document {
static async getInitialProps ({ renderPage }) {
const page = renderPage()

// see https://github.com/nfl/react-helmet#server-usage for more information
// 'head' was occupied by 'page.head', we cannot use it
return { ...page, 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
title='Hello next.js!'
/>)
}

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>)