Skip to content

Commit 0bab34d

Browse files
montogeekskipjack
authored andcommitted
chore(site): Add page title to server render
- Created a simple hash object to save paths and page titles - Use previous object for SSGPlugin since crawl wasn't working, it fails to collect all links ignoring 'configuration' section entirely.
1 parent 708c776 commit 0bab34d

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/server.jsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import Favicon from './favicon.ico';
1111

1212
// Define bundles (previously used `Object.values(locals.assets)`) but
1313
// can't retrieve from there anymore due to separate compilation.
14-
const bundles = [
15-
'/vendor.bundle.js',
16-
'/index.bundle.js'
17-
];
14+
const bundles = ['/vendor.bundle.js', '/index.bundle.js'];
1815

1916
// Export method for `StaticSiteGeneratorPlugin`
2017
// CONSIDER: How high can we mount `Site` into the DOM hierarchy? If
@@ -23,34 +20,35 @@ const bundles = [
2320
// description, etc).
2421
export default locals => {
2522
let { assets } = locals.webpackStats.compilation;
23+
let title = locals.paths[locals.path];
2624

2725
return ReactDOMServer.renderToString(
28-
<StaticRouter location={ locals.path } context={{}}>
26+
<StaticRouter location={locals.path} context={{}}>
2927
<html>
3028
<head>
31-
<meta charSet="UTF-8" />
29+
<meta charset='utf-8' />
3230
<meta name="theme-color" content="#2B3A42" />
3331
<meta name="viewport" content="width=device-width, initial-scale=1" />
34-
<title>{/* TODO */} | webpack</title>
35-
<meta name="description" content="webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset." />
36-
<link rel="icon" type="image/x-icon" href={ Favicon } />
37-
{ Object.keys(assets).filter(asset => /\.css$/.test(asset)).map(path => (
38-
<link key={ path } rel="stylesheet" href={ `/${path}` } />
39-
))}
32+
<title>{title} | webpack</title>
33+
<meta
34+
name="description"
35+
content="webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset."
36+
/>
37+
<link rel="icon" type="image/x-icon" href={Favicon} />
38+
{Object.keys(assets)
39+
.filter(asset => /\.css$/.test(asset))
40+
.map(path => <link key={path} rel="stylesheet" href={`/${path}`} />)}
4041
</head>
4142
<body>
4243
<div id="root">
4344
<Route
4445
path="/"
45-
render={ props => (
46-
<Site
47-
{ ...props }
48-
import={ path => require(`./content/${path}`) } />
49-
)} />
46+
render={props => (
47+
<Site {...props} import={path => require(`./content/${path}`)} />
48+
)}
49+
/>
5050
</div>
51-
{ bundles.map(path => (
52-
<script key={ path } src={ path } />
53-
))}
51+
{bundles.map(path => <script key={path} src={path} />)}
5452
</body>
5553
</html>
5654
</StaticRouter>

webpack.prod.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@ const RedirectWebpackPlugin = require('redirect-webpack-plugin');
88

99
// Load Common Configuration
1010
const common = require('./webpack.common.js');
11+
const Content = require('./src/_content.json');
12+
13+
const paths = Content.children.reduce((paths, page) => {
14+
if (page.type === 'directory') {
15+
paths = Object.assign(
16+
paths,
17+
page.children.reduce((childPaths, child) => {
18+
childPaths[child.url] = child.title;
19+
return childPaths;
20+
}, {})
21+
);
22+
} else {
23+
paths[page.url] = page.title;
24+
}
25+
26+
return paths;
27+
}, {});
1128

1229
// ...
1330
const prod = {
@@ -17,7 +34,7 @@ const prod = {
1734
'process.env.NODE_ENV': JSON.stringify('production')
1835
})
1936
]
20-
}
37+
};
2138

2239
// Export both SSG and SPA configurations
2340
module.exports = env => [
@@ -28,7 +45,8 @@ module.exports = env => [
2845
},
2946
plugins: [
3047
new SSGPlugin({
31-
crawl: true,
48+
paths: Object.keys(paths),
49+
locals: { paths },
3250
globals: {
3351
window: {}
3452
}
@@ -77,8 +95,8 @@ module.exports = env => [
7795
plugins: [
7896
new webpack.optimize.CommonsChunkPlugin({
7997
name: 'vendor',
80-
chunks: [ 'index' ]
98+
chunks: ['index']
8199
})
82100
]
83101
})
84-
]
102+
];

0 commit comments

Comments
 (0)