Skip to content

Commit e52e6c6

Browse files
whitedogg13sophiebits
authored andcommitted
Dynamically append path alias (aaa/bbb.html = aaa/bbb = aaa/bbb/) int… (reactjs#783)
* Dynamically append path alias (aaa/bbb.html = aaa/bbb = aaa/bbb/) into redirects during gatsby onCreateNode callback API to avoid 404 * Consider more cases: (1) markdown without redirects (2) duplicated permalink for /docs/pure-render-mixin.html, rename the unused one * Test permalink with ending `.html` rather then containing them
1 parent a1655f4 commit e52e6c6

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

content/docs/reference-pure-render-mixin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: pure-render-mixin
33
title: PureRenderMixin
44
layout: docs
55
category: Reference
6-
permalink: docs/pure-render-mixin.html
6+
permalink: docs/pure-render-mixin-old.html
77
---
88

99
> Note

gatsby/onCreateNode.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
// Parse date information out of blog post filename.
1010
const BLOG_POST_FILENAME_REGEX = /([0-9]+)\-([0-9]+)\-([0-9]+)\-(.+)\.md$/;
1111

12+
function buildRedirectString(permalink, redirect_from) {
13+
if (!permalink || !permalink.endsWith('.html')) {
14+
return redirect_from ? JSON.stringify(redirect_from) : '';
15+
}
16+
17+
let basePath = permalink.slice(0, -'.html'.length);
18+
let redirects = [basePath, basePath + '/'];
19+
if (Array.isArray(redirect_from)) {
20+
redirects = redirects.concat(redirect_from);
21+
}
22+
23+
return JSON.stringify(redirects);
24+
}
25+
1226
// Add custom fields to MarkdownRemark nodes.
1327
module.exports = exports.onCreateNode = ({node, boundActionCreators, getNode}) => {
1428
const {createNodeField} = boundActionCreators;
@@ -67,8 +81,9 @@ module.exports = exports.onCreateNode = ({node, boundActionCreators, getNode}) =
6781
createNodeField({
6882
node,
6983
name: 'redirect',
70-
value: redirect_from ? JSON.stringify(redirect_from) : '',
84+
value: buildRedirectString(permalink, redirect_from),
7185
});
86+
7287
return;
7388
}
74-
};
89+
};

0 commit comments

Comments
 (0)