Skip to content

Commit 75c3572

Browse files
montogeekskipjack
authored andcommitted
chore(site) Move page title generation to server.jsx
1 parent 7b754f9 commit 75c3572

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

src/server.jsx

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,55 @@ 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 = ['/vendor.bundle.js', '/index.bundle.js'];
14+
const bundles = [
15+
'/vendor.bundle.js',
16+
'/index.bundle.js'
17+
];
18+
19+
const Content = require('./_content.json');
20+
21+
const paths = Content.children.reduce((paths, page) => {
22+
if (page.type === 'directory') {
23+
page.children.forEach(child => (paths[child.url] = child.title));
24+
} else {
25+
paths[page.url] = page.title;
26+
}
27+
28+
return paths;
29+
}, {});
1530

1631
// Export method for `SSGPlugin`
1732
export default locals => {
1833
let { assets } = locals.webpackStats.compilation;
19-
let title = locals.paths[locals.path];
34+
let title = paths[locals.path];
2035

2136
return ReactDOMServer.renderToString(
2237
<StaticRouter location={locals.path} context={{}}>
2338
<html>
2439
<head>
25-
<meta charset='utf-8' />
40+
<meta charset="utf-8" />
2641
<meta name="theme-color" content="#2B3A42" />
2742
<meta name="viewport" content="width=device-width, initial-scale=1" />
2843
<title>{title} | webpack</title>
29-
<meta
30-
name="description"
31-
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."
32-
/>
33-
<link rel="icon" type="image/x-icon" href={Favicon} />
34-
{Object.keys(assets)
35-
.filter(asset => /\.css$/.test(asset))
36-
.map(path => <link key={path} rel="stylesheet" href={`/${path}`} />)}
44+
<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." />
45+
<link rel="icon" type="image/x-icon" href={ Favicon } />
46+
{ Object.keys(assets).filter(asset => /\.css$/.test(asset)).map(path => (
47+
<link key={ path } rel="stylesheet" href={ `/${path}` } />
48+
))}
3749
</head>
3850
<body>
3951
<div id="root">
4052
<Route
4153
path="/"
42-
render={props => (
43-
<Site {...props} import={path => require(`./content/${path}`)} />
44-
)}
45-
/>
54+
render={ props => (
55+
<Site
56+
{ ...props }
57+
import={ path => require(`./content/${path}`) } />
58+
)} />
4659
</div>
47-
{bundles.map(path => <script key={path} src={path} />)}
60+
{ bundles.map(path => (
61+
<script key={ path } src={ path } />
62+
))}
4863
</body>
4964
</html>
5065
</StaticRouter>

webpack.prod.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ 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-
page.children.forEach(child => (paths[child.url] = child.title));
16-
} else {
17-
paths[page.url] = page.title;
18-
}
19-
20-
return paths;
21-
}, {});
2211

2312
// ...
2413
const prod = {
@@ -39,8 +28,7 @@ module.exports = env => [
3928
},
4029
plugins: [
4130
new SSGPlugin({
42-
paths: Object.keys(paths),
43-
locals: { paths },
31+
crawl: true,
4432
globals: {
4533
window: {}
4634
}

0 commit comments

Comments
 (0)