Skip to content

Commit ec44237

Browse files
authored
Merge pull request #801 from bvaughn/versions
Added redirects for old React versions
2 parents 5593059 + 631f32c commit ec44237

File tree

8 files changed

+176
-3
lines changed

8 files changed

+176
-3
lines changed

content/versions.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- title: '16.3.2'
2+
changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1632-april-16-2018
3+
- title: '16.2.0'
4+
path: /version/16.2
5+
url: https://5abc31d8be40f1556f06c4be--reactjs.netlify.com
6+
changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1620-november-28-2017
7+
- title: '16.1.1'
8+
path: /version/16.1
9+
url: https://5a1dbcf14c4b93299e65b9a9--reactjs.netlify.com
10+
changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1611-november-13-2017
11+
- title: '16.0.0'
12+
path: /version/16.0
13+
url: https://5a046bf5a6188f4b8fa4938a--reactjs.netlify.com
14+
changelog: https://github.com/facebook/react/blob/master/CHANGELOG.md#1600-september-26-2017

gatsby-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
'gatsby-source-react-error-codes',
2121
'gatsby-transformer-authors-yaml',
2222
'gatsby-transformer-home-example-code',
23+
'gatsby-transformer-versions-yaml',
2324
'gatsby-plugin-netlify',
2425
'gatsby-plugin-glamor',
2526
'gatsby-plugin-react-next',
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const {appendFile, exists, readFile, writeFile} = require('fs-extra');
2+
3+
const HEADER_COMMENT = `## Created with gatsby-transformer-versions-yaml`;
4+
5+
// Patterned after the 'gatsby-plugin-netlify' plug-in:
6+
// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-netlify/src/create-redirects.js
7+
module.exports = async function writeRedirectsFile(
8+
redirects,
9+
redirectsFilePath,
10+
) {
11+
if (!redirects.length) {
12+
return null;
13+
}
14+
15+
// Map redirect data to the format Netlify expects
16+
// https://www.netlify.com/docs/redirects/
17+
redirects = redirects.map(redirect => {
18+
const {
19+
fromPath,
20+
isPermanent,
21+
redirectInBrowser, // eslint-disable-line no-unused-vars
22+
toPath,
23+
...rest
24+
} = redirect;
25+
26+
// The order of these parameters is significant.
27+
const pieces = [
28+
fromPath,
29+
toPath,
30+
isPermanent ? 301 : 302, // Status
31+
];
32+
33+
for (let key in rest) {
34+
const value = rest[key];
35+
36+
if (typeof value === `string` && value.indexOf(` `) >= 0) {
37+
console.warn(
38+
`Invalid redirect value "${value}" specified for key "${key}". ` +
39+
`Values should not contain spaces.`,
40+
);
41+
} else {
42+
pieces.push(`${key}=${value}`);
43+
}
44+
}
45+
46+
return pieces.join(` `);
47+
});
48+
49+
let appendToFile = false;
50+
51+
// Websites may also have statically defined redirects
52+
// In that case we should append to them (not overwrite)
53+
// Make sure we aren't just looking at previous build results though
54+
const fileExists = await exists(redirectsFilePath);
55+
if (fileExists) {
56+
const fileContents = await readFile(redirectsFilePath);
57+
if (fileContents.indexOf(HEADER_COMMENT) < 0) {
58+
appendToFile = true;
59+
}
60+
}
61+
62+
const data = `${HEADER_COMMENT}\n\n${redirects.join(`\n`)}`;
63+
64+
return appendToFile
65+
? appendFile(redirectsFilePath, `\n\n${data}`)
66+
: writeFile(redirectsFilePath, data);
67+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const readFileSync = require('fs').readFileSync;
2+
const resolve = require('path').resolve;
3+
const safeLoad = require('js-yaml').safeLoad;
4+
const createRedirects = require('./create-redirects');
5+
const path = require('path');
6+
7+
// Reads versions.yml data into GraphQL.
8+
// This is used to generate redirect rules for older documentation versions.
9+
exports.onPostBuild = async ({store}) => {
10+
const versionsFile = resolve(__dirname, '../../content/versions.yml');
11+
const file = readFileSync(versionsFile, 'utf8');
12+
const versions = safeLoad(file);
13+
14+
const {program} = store.getState();
15+
const redirectsFilePath = path.join(
16+
program.directory,
17+
'public',
18+
'_redirects',
19+
);
20+
21+
// versions.yml structure is [{path: string, url: string, ...}, ...]
22+
createRedirects(
23+
versions.filter(version => version.path && version.url).map(version => ({
24+
fromPath: version.path,
25+
toPath: version.url,
26+
})),
27+
redirectsFilePath,
28+
);
29+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "gatsby-transformer-versions-yaml",
3+
"version": "0.0.1"
4+
}

src/components/LayoutHeader/Header.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ const Header = ({location}: {location: Location}) => (
162162
borderRadius: 15,
163163
},
164164
}}
165-
href="https://github.com/facebook/react/releases"
166-
target="_blank"
167-
rel="noopener">
165+
href="/versions">
168166
v{version}
169167
</a>
170168
<a

src/pages/versions.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2013-present, Facebook, Inc.
3+
*
4+
* @emails react-core
5+
* @flow
6+
*/
7+
8+
import Container from 'components/Container';
9+
import Header from 'components/Header';
10+
import TitleAndMetaTags from 'components/TitleAndMetaTags';
11+
import React from 'react';
12+
import {sharedStyles} from 'theme';
13+
14+
// $FlowFixMe This is a valid path
15+
import versions from '../../content/versions.yml';
16+
17+
const Versions = () => (
18+
<Container>
19+
<div css={sharedStyles.articleLayout.container}>
20+
<div css={sharedStyles.articleLayout.content}>
21+
<Header>React Versions</Header>
22+
<TitleAndMetaTags title="React - Versions" />
23+
<div css={sharedStyles.markdown}>
24+
<p>
25+
A complete release history for React is available{' '}
26+
<a
27+
href="https://github.com/facebook/react/releases"
28+
target="_blank"
29+
rel="noopener">
30+
in GitHub
31+
</a>. Documentation for recent releases can also be found below:
32+
</p>
33+
{versions.map(version => (
34+
<div key={version.title}>
35+
<h3>{version.title}</h3>
36+
<ul>
37+
<li>
38+
<a href={version.changelog} target="_blank" rel="noopener">
39+
Changelog
40+
</a>
41+
</li>
42+
{version.path && (
43+
<li>
44+
<a href={version.path} rel="nofollow">
45+
Documentation
46+
</a>
47+
</li>
48+
)}
49+
</ul>
50+
</div>
51+
))}
52+
</div>
53+
</div>
54+
</div>
55+
</Container>
56+
);
57+
58+
export default Versions;

static/robots.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Disallow: /version/

0 commit comments

Comments
 (0)