Skip to content

Commit 9f69c4c

Browse files
committed
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 7b805b3 commit 9f69c4c

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

DISCUSSION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ this branch:
1414
- [ ] Finish re-incorporating mobile sidebar
1515
- [ ] Re-integrate google-analytics
1616
- [ ] Re-incorporate `redirects.json`
17-
- [ ] Populate page title in `server.jsx` (fernando)
17+
- [x] Populate page title in `server.jsx` (fernando)
1818
- [ ] Finish `Navigation` component (greg)
1919
- [x] Add custom route for landing page (greg)
2020

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
@@ -7,6 +7,23 @@ const SSGPlugin = require('static-site-generator-webpack-plugin');
77

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

1128
// ...
1229
const prod = {
@@ -16,7 +33,7 @@ const prod = {
1633
'process.env.NODE_ENV': JSON.stringify('production')
1734
})
1835
]
19-
}
36+
};
2037

2138
// Export both SSG and SPA configurations
2239
module.exports = env => [
@@ -27,7 +44,8 @@ module.exports = env => [
2744
},
2845
plugins: [
2946
new SSGPlugin({
30-
crawl: true,
47+
paths: Object.keys(paths),
48+
locals: { paths },
3149
globals: {
3250
window: {}
3351
}
@@ -43,8 +61,8 @@ module.exports = env => [
4361
plugins: [
4462
new webpack.optimize.CommonsChunkPlugin({
4563
name: 'vendor',
46-
chunks: [ 'index' ]
64+
chunks: ['index']
4765
})
4866
]
4967
})
50-
]
68+
];

0 commit comments

Comments
 (0)