-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
Changes from 10 commits
94ad64b
34b418b
fae8058
4309a55
b72e285
f345503
4f51755
8a7b358
49376e8
d6c263c
75428ff
a66dfd4
82dd379
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**/yarn.lock | ||
node_modules/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant all of them 😅 Sorry was on my phone then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the whole There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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. |
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" | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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>) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default () => (<div> | ||
Hello World! | ||
</div>) |
There was a problem hiding this comment.
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.