Skip to content

Commit

Permalink
app title and meta tags with react-helmet
Browse files Browse the repository at this point in the history
  • Loading branch information
xpepermint committed Sep 12, 2015
1 parent 50c8ab8 commit b6a24a0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/components/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';
import {Link} from 'react-router';
import Helmet from "react-helmet";

let App = React.createClass({
render() {
return (
<div>
<Helmet title="Welcome"/>
<ul>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact</Link></li>
Expand Down
5 changes: 5 additions & 0 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import express from 'express';
import React from 'react';
import {renderToString} from 'react-dom/server';
import createLocation from 'history/lib/createLocation';
import Helmet from 'react-helmet';
import {RoutingContext, match} from 'react-router';
import routes from './routes';

Expand All @@ -24,10 +25,14 @@ app.use((req, res, next) => {
if (renderProps == null) return next(error);

let markup = renderToString(<RoutingContext {...renderProps}/>);
let helmet = Helmet.rewind();
let html = [
`<!DOCTYPE html>`,
`<html>`,
`<head>`,
`<title>${helmet.title}</title>`,
helmet.meta,
helmet.link,
`<meta charset="utf-8"/>`,
`<link rel="stylesheet" href="${assetsPath}/app.css"></link>`,
`</head>`,
Expand Down
46 changes: 46 additions & 0 deletions docs/6-react-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
React does not handle the page title and meta tags. We will use the power of the [react-helmet](https://github.com/nfl/react-helmet) package.

Start by installing the dependencies.

```
npm install --save react-helmet
```

Put the `<Helmet/>` tag inside components.

```js
// app/components/App
...
import Helmet from "react-helmet";

let App = React.createClass({
render() {
return (
<div>
<Helmet title="App" />
...
</div>
)
}
});
...
```

Open `app/server.js` and upgrade the server-side logic.

```js
import Helmet from 'react-helmet';
...
let markup = renderToString(...);
let helmet = Helmet.rewind();
let html = [
...
`<head>`,
`<title>${helmet.title}</title>`,
helmet.meta,
helmet.link,
...
`</head>`,
...
].join('');
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"history": "^1.9.0",
"react": "^0.14.0-rc1",
"react-dom": "^0.14.0-rc1",
"react-helmet": "^1.1.3",
"react-router": "^1.0.0-rc1"
},
"devDependencies": {
Expand Down

0 comments on commit b6a24a0

Please sign in to comment.