Skip to content

Commit a57fa7e

Browse files
cia48621793timneutkens
authored andcommitted
An example with react-helmet (vercel#1264)
* upload example * fix * fix * fix * fix .babelrc * fix standard style * fix indent * rename helmetHead to helmet * added gitignore * package.json * removed yarn.lock * Added more examples of using react-helmet * removed gitignore
1 parent 123a635 commit a57fa7e

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

examples/with-react-helmet/.DS_Store

6 KB
Binary file not shown.

examples/with-react-helmet/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
# react-helmet example
3+
4+
## How to use
5+
6+
Download the example [or clone the repo](https://github.com/zeit/next.js):
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-react-helmet
10+
cd with-react-helmet
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
_Or alternatively:_
20+
```bash
21+
yarn
22+
yarn run dev
23+
```
24+
25+
26+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
27+
28+
```bash
29+
now
30+
```
31+
## Notes
32+
This an minimalistic example of how to combine next.js and [react-helmet](https://github.com/nfl/react-helmet).
33+
The title of the page shall be changed to "Hello next.js!"
34+
The rest is all up to you.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "with-react-helmet",
3+
"license": "ISC",
4+
"version": "1.0.0",
5+
"scripts": {
6+
"dev": "next",
7+
"build": "next build",
8+
"start": "next start"
9+
},
10+
"dependencies": {
11+
"next": "^2.0.0-beta",
12+
"react": "^15.4.2",
13+
"react-dom": "^15.4.2",
14+
"react-helmet": "^4.0.0"
15+
}
16+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Document, { Head, Main, NextScript } from 'next/document'
2+
import Helmet from 'react-helmet'
3+
4+
export default class extends Document {
5+
static async getInitialProps ({ renderPage }) {
6+
// see https://github.com/nfl/react-helmet#server-usage for more information
7+
// 'head' was occupied by 'renderPage().head', we cannot use it
8+
return { ...renderPage(), helmet: Helmet.rewind() }
9+
}
10+
11+
// should render on <html>
12+
get helmetHtmlAttrComponents () {
13+
return this.props.helmet.htmlAttributes.toComponent()
14+
}
15+
16+
// should render on <head>
17+
get helmetHeadComponents () {
18+
return Object.keys(this.props.helmet)
19+
.filter(el => el !== 'htmlAttributes') // remove htmlAttributes which is not for <head> but for <html>
20+
.map(el => this.props.helmet[el].toComponent())
21+
}
22+
23+
get helmetJsx () {
24+
return (<Helmet
25+
htmlAttributes={{lang: 'en'}}
26+
title='Hello next.js!'
27+
meta={[
28+
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }
29+
]}
30+
/>)
31+
}
32+
33+
render () {
34+
return (<html {...this.helmetHtmlAttrComponents}>
35+
<Head>
36+
{ this.helmetJsx }
37+
{ this.helmetHeadComponents }
38+
</Head>
39+
<body>
40+
<Main />
41+
<NextScript />
42+
</body>
43+
</html>)
44+
}
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default () => (<div>
2+
Hello World!
3+
</div>)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy