Skip to content

Commit cd1b0df

Browse files
author
Strek
authored
Changed the version generation script to update vercel.json instead of _redirects (reactjs#4136)
* Changed the version generation script to update vercel.json instead of _redirects * Fix prettier failures * Expected to return a value at the end of async function 'writeRedirectsFile' * Add same script for beta as well * Fixes review comments * Fixes formatting in old site
1 parent e0aed3f commit cd1b0df

File tree

6 files changed

+626
-138
lines changed

6 files changed

+626
-138
lines changed

beta/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"analyze": "ANALYZE=true next build",
88
"dev": "next",
9-
"build": "next build && node ./scripts/generateRSS.js",
9+
"build": "next build && node ./scripts/generateRSS.js && node ./scripts/generateRedirects.js",
1010
"lint": "next lint",
1111
"lint:fix": "next lint --fix",
1212
"format:source": "prettier --config .prettierrc --write \"{plugins,src}/**/*.{js,ts,jsx,tsx}\"",

beta/scripts/generateRedirects.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*/
4+
5+
const resolve = require('path').resolve;
6+
const {writeFile} = require('fs-extra');
7+
const readFileSync = require('fs').readFileSync;
8+
const safeLoad = require('js-yaml').safeLoad;
9+
const path = require('path');
10+
const versionsFile = resolve(__dirname, '../../content/versions.yml');
11+
const file = readFileSync(versionsFile, 'utf8');
12+
const versions = safeLoad(file);
13+
const redirectsFilePath = path.join('vercel.json');
14+
15+
function writeRedirectsFile(redirects, redirectsFilePath) {
16+
if (!redirects.length) {
17+
return null;
18+
}
19+
20+
/**
21+
* We will first read the old config to validate if the redirect already exists in the json
22+
*/
23+
const vercelConfigPath = resolve(__dirname, '../../vercel.json');
24+
const vercelConfigFile = readFileSync(vercelConfigPath);
25+
const oldConfigContent = JSON.parse(vercelConfigFile);
26+
/**
27+
* Map data as vercel expects it to be
28+
*/
29+
30+
let vercelRedirects = {};
31+
32+
redirects.forEach((redirect) => {
33+
const {fromPath, isPermanent, toPath} = redirect;
34+
35+
vercelRedirects[fromPath] = {
36+
destination: toPath,
37+
permanent: !!isPermanent,
38+
};
39+
});
40+
41+
/**
42+
* Make sure we dont have the same redirect already
43+
*/
44+
oldConfigContent.redirects.forEach((data) => {
45+
if(vercelRedirects[data.source]){
46+
delete vercelRedirects[data.source];
47+
}
48+
});
49+
50+
/**
51+
* Serialize the object to array of objects
52+
*/
53+
let newRedirects = [];
54+
Object.keys(vercelRedirects).forEach((value) =>
55+
newRedirects.push({
56+
source: value,
57+
destination: vercelRedirects[value].destination,
58+
permanent: !!vercelRedirects[value].isPermanent,
59+
})
60+
);
61+
62+
/**
63+
* We already have a vercel.json so we spread the new contents along with old ones
64+
*/
65+
const newContents = {
66+
...oldConfigContent,
67+
redirects: [...oldConfigContent.redirects, ...newRedirects],
68+
};
69+
writeFile(redirectsFilePath, JSON.stringify(newContents, null, 2));
70+
}
71+
72+
// versions.yml structure is [{path: string, url: string, ...}, ...]
73+
writeRedirectsFile(
74+
versions
75+
.filter((version) => version.path && version.url)
76+
.map((version) => ({
77+
fromPath: version.path,
78+
toPath: version.url,
79+
})),
80+
redirectsFilePath
81+
);

beta/vercel.json

Lines changed: 249 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,253 @@
11
{
2-
"github": { "silent": true },
2+
"github": {
3+
"silent": true
4+
},
35
"trailingSlash": false,
46
"redirects": [
5-
{ "source": "/tips/controlled-input-null-value.html", "destination": "/docs/forms.html#controlled-input-null-value", "permanent": false },
6-
{ "source": "/link/switch-to-createroot", "destination": "https://github.com/reactwg/react-18/discussions/5", "permanent": false },
7-
{ "source": "/server-components", "destination": "/blog/2020/12/21/data-fetching-with-react-server-components.html", "permanent": false },
8-
{ "source": "/concurrent", "destination": "/docs/concurrent-mode-intro.html", "permanent": false },
9-
{ "source": "/hooks", "destination": "/docs/hooks-intro.html", "permanent": false },
10-
{ "source": "/tutorial", "destination": "/tutorial/tutorial.html", "permanent": false },
11-
{ "source": "/your-story", "destination": "https://www.surveymonkey.co.uk/r/MVQV2R9", "permanent": false },
12-
{ "source": "/stories", "destination": "https://medium.com/react-community-stories", "permanent": false },
13-
{ "source": "/html-jsx.html", "destination": "https://magic.reactjs.net/htmltojsx.htm", "destination": "", "permanent": false },
14-
{ "source": "/link/attribute-behavior", "destination": "/blog/2017/09/08/dom-attributes-in-react-16.html#changes-in-detail", "permanent": false },
15-
{ "source": "/link/controlled-components", "destination": "/docs/forms.html#controlled-components", "permanent": false },
16-
{ "source": "/link/crossorigin-error", "destination": "/docs/cross-origin-errors.html", "permanent": false },
17-
{ "source": "/link/dangerously-set-inner-html", "destination": "/docs/dom-elements.html#dangerouslysetinnerhtml", "permanent": false },
18-
{ "source": "/link/derived-state", "destination": "/blog/2018/06/07/you-probably-dont-need-derived-state.html", "permanent": false },
19-
{ "source": "/link/error-boundaries", "destination": "/docs/error-boundaries.html", "permanent": false },
20-
{ "source": "/link/event-pooling", "destination": "/docs/legacy-event-pooling.html", "permanent": false },
21-
{ "source": "/link/hooks-data-fetching", "destination": "/docs/hooks-faq.html#how-can-i-do-data-fetching-with-hooks", "permanent": false },
22-
{ "source": "/link/invalid-aria-props", "destination": "/warnings/invalid-aria-prop.html", "permanent": false },
23-
{ "source": "/link/invalid-hook-call", "destination": "/warnings/invalid-hook-call-warning.html", "permanent": false },
24-
{ "source": "/link/legacy-context", "destination": "/docs/legacy-context.html", "permanent": false },
25-
{ "source": "/link/legacy-factories", "destination": "/warnings/legacy-factories.html", "permanent": false },
26-
{ "source": "/link/mock-scheduler", "destination": "/docs/testing-environments.html#mocking-a-rendering-surface", "destination": "", "permanent": false },
27-
{ "source": "/link/perf-use-production-build", "destination": "/docs/optimizing-performance.html#use-the-production-build", "permanent": false },
28-
{ "source": "/link/react-devtools", "destination": "/blog/2015/09/02/new-react-developer-tools.html#installation", "permanent": false },
29-
{ "source": "/link/react-polyfills", "destination": "/docs/javascript-environment-requirements.html", "permanent": false },
30-
{ "source": "/link/refs-must-have-owner", "destination": "/warnings/refs-must-have-owner.html", "permanent": false },
31-
{ "source": "/link/rules-of-hooks", "destination": "/docs/hooks-rules.html", "permanent": false },
32-
{ "source": "/link/special-props", "destination": "/warnings/special-props.html", "permanent": false },
33-
{ "source": "/link/strict-mode-find-node", "destination": "/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage", "permanent": false },
34-
{ "source": "/link/strict-mode-string-ref", "destination": "/docs/refs-and-the-dom.html#legacy-api-string-refs", "permanent": false },
35-
{ "source": "/link/unsafe-component-lifecycles", "destination": "/blog/2018/03/27/update-on-async-rendering.html", "permanent": false },
36-
{ "source": "/link/warning-keys", "destination": "/docs/lists-and-keys.html#keys", "permanent": false },
37-
{ "source": "/link/wrap-tests-with-act", "destination": "/docs/test-utils.html#act", "permanent": false },
38-
{ "source": "/link/interaction-tracing", "destination": "https://gist.github.com/bvaughn/8de925562903afd2e7a12554adcdda16", "permanent": false },
39-
{ "source": "/link/profiling", "destination": "https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977", "permanent": false },
40-
{ "source": "/link/test-utils-mock-component", "destination": "https://gist.github.com/bvaughn/fbf41b3f895bf2d297935faa5525eee9", "permanent": false },
41-
{ "source": "/link/uselayouteffect-ssr", "destination": "https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85", "permanent": false },
42-
{ "source": "/link/react-devtools-faq", "destination": "https://github.com/facebook/react/tree/main/packages/react-devtools#faq", "permanent": false },
43-
{ "source": "/link/setstate-in-render", "destination": "https://github.com/facebook/react/issues/18178#issuecomment-595846312", "permanent": false },
44-
{ "source": "/version/15.6", "destination": "https://react-legacy.netlify.app", "permanent": false }
7+
{
8+
"source": "/tips/controlled-input-null-value.html",
9+
"destination": "/docs/forms.html#controlled-input-null-value",
10+
"permanent": false
11+
},
12+
{
13+
"source": "/link/switch-to-createroot",
14+
"destination": "https://github.com/reactwg/react-18/discussions/5",
15+
"permanent": false
16+
},
17+
{
18+
"source": "/server-components",
19+
"destination": "/blog/2020/12/21/data-fetching-with-react-server-components.html",
20+
"permanent": false
21+
},
22+
{
23+
"source": "/concurrent",
24+
"destination": "/docs/concurrent-mode-intro.html",
25+
"permanent": false
26+
},
27+
{
28+
"source": "/hooks",
29+
"destination": "/docs/hooks-intro.html",
30+
"permanent": false
31+
},
32+
{
33+
"source": "/tutorial",
34+
"destination": "/tutorial/tutorial.html",
35+
"permanent": false
36+
},
37+
{
38+
"source": "/your-story",
39+
"destination": "https://www.surveymonkey.co.uk/r/MVQV2R9",
40+
"permanent": false
41+
},
42+
{
43+
"source": "/stories",
44+
"destination": "https://medium.com/react-community-stories",
45+
"permanent": false
46+
},
47+
{
48+
"source": "/html-jsx.html",
49+
"destination": "",
50+
"permanent": false
51+
},
52+
{
53+
"source": "/link/attribute-behavior",
54+
"destination": "/blog/2017/09/08/dom-attributes-in-react-16.html#changes-in-detail",
55+
"permanent": false
56+
},
57+
{
58+
"source": "/link/controlled-components",
59+
"destination": "/docs/forms.html#controlled-components",
60+
"permanent": false
61+
},
62+
{
63+
"source": "/link/crossorigin-error",
64+
"destination": "/docs/cross-origin-errors.html",
65+
"permanent": false
66+
},
67+
{
68+
"source": "/link/dangerously-set-inner-html",
69+
"destination": "/docs/dom-elements.html#dangerouslysetinnerhtml",
70+
"permanent": false
71+
},
72+
{
73+
"source": "/link/derived-state",
74+
"destination": "/blog/2018/06/07/you-probably-dont-need-derived-state.html",
75+
"permanent": false
76+
},
77+
{
78+
"source": "/link/error-boundaries",
79+
"destination": "/docs/error-boundaries.html",
80+
"permanent": false
81+
},
82+
{
83+
"source": "/link/event-pooling",
84+
"destination": "/docs/legacy-event-pooling.html",
85+
"permanent": false
86+
},
87+
{
88+
"source": "/link/hooks-data-fetching",
89+
"destination": "/docs/hooks-faq.html#how-can-i-do-data-fetching-with-hooks",
90+
"permanent": false
91+
},
92+
{
93+
"source": "/link/invalid-aria-props",
94+
"destination": "/warnings/invalid-aria-prop.html",
95+
"permanent": false
96+
},
97+
{
98+
"source": "/link/invalid-hook-call",
99+
"destination": "/warnings/invalid-hook-call-warning.html",
100+
"permanent": false
101+
},
102+
{
103+
"source": "/link/legacy-context",
104+
"destination": "/docs/legacy-context.html",
105+
"permanent": false
106+
},
107+
{
108+
"source": "/link/legacy-factories",
109+
"destination": "/warnings/legacy-factories.html",
110+
"permanent": false
111+
},
112+
{
113+
"source": "/link/mock-scheduler",
114+
"destination": "",
115+
"permanent": false
116+
},
117+
{
118+
"source": "/link/perf-use-production-build",
119+
"destination": "/docs/optimizing-performance.html#use-the-production-build",
120+
"permanent": false
121+
},
122+
{
123+
"source": "/link/react-devtools",
124+
"destination": "/blog/2015/09/02/new-react-developer-tools.html#installation",
125+
"permanent": false
126+
},
127+
{
128+
"source": "/link/react-polyfills",
129+
"destination": "/docs/javascript-environment-requirements.html",
130+
"permanent": false
131+
},
132+
{
133+
"source": "/link/refs-must-have-owner",
134+
"destination": "/warnings/refs-must-have-owner.html",
135+
"permanent": false
136+
},
137+
{
138+
"source": "/link/rules-of-hooks",
139+
"destination": "/docs/hooks-rules.html",
140+
"permanent": false
141+
},
142+
{
143+
"source": "/link/special-props",
144+
"destination": "/warnings/special-props.html",
145+
"permanent": false
146+
},
147+
{
148+
"source": "/link/strict-mode-find-node",
149+
"destination": "/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage",
150+
"permanent": false
151+
},
152+
{
153+
"source": "/link/strict-mode-string-ref",
154+
"destination": "/docs/refs-and-the-dom.html#legacy-api-string-refs",
155+
"permanent": false
156+
},
157+
{
158+
"source": "/link/unsafe-component-lifecycles",
159+
"destination": "/blog/2018/03/27/update-on-async-rendering.html",
160+
"permanent": false
161+
},
162+
{
163+
"source": "/link/warning-keys",
164+
"destination": "/docs/lists-and-keys.html#keys",
165+
"permanent": false
166+
},
167+
{
168+
"source": "/link/wrap-tests-with-act",
169+
"destination": "/docs/test-utils.html#act",
170+
"permanent": false
171+
},
172+
{
173+
"source": "/link/interaction-tracing",
174+
"destination": "https://gist.github.com/bvaughn/8de925562903afd2e7a12554adcdda16",
175+
"permanent": false
176+
},
177+
{
178+
"source": "/link/profiling",
179+
"destination": "https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977",
180+
"permanent": false
181+
},
182+
{
183+
"source": "/link/test-utils-mock-component",
184+
"destination": "https://gist.github.com/bvaughn/fbf41b3f895bf2d297935faa5525eee9",
185+
"permanent": false
186+
},
187+
{
188+
"source": "/link/uselayouteffect-ssr",
189+
"destination": "https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85",
190+
"permanent": false
191+
},
192+
{
193+
"source": "/link/react-devtools-faq",
194+
"destination": "https://github.com/facebook/react/tree/main/packages/react-devtools#faq",
195+
"permanent": false
196+
},
197+
{
198+
"source": "/link/setstate-in-render",
199+
"destination": "https://github.com/facebook/react/issues/18178#issuecomment-595846312",
200+
"permanent": false
201+
},
202+
{
203+
"source": "/version/15.6",
204+
"destination": "https://react-legacy.netlify.app",
205+
"permanent": false
206+
},
207+
{
208+
"source": "/version/16.8",
209+
"destination": "https://5d4b5feba32acd0008d0df98--reactjs.netlify.com/",
210+
"permanent": false
211+
},
212+
{
213+
"source": "/version/16.7",
214+
"destination": "https://5c54aa429e16c80007af3cd2--reactjs.netlify.com/",
215+
"permanent": false
216+
},
217+
{
218+
"source": "/version/16.6",
219+
"destination": "https://5c11762d4be4d10008916ab1--reactjs.netlify.com/",
220+
"permanent": false
221+
},
222+
{
223+
"source": "/version/16.5",
224+
"destination": "https://5bcf5863c6aed64970d6de5b--reactjs.netlify.com/",
225+
"permanent": false
226+
},
227+
{
228+
"source": "/version/16.4",
229+
"destination": "https://5b90c17ac9659241e7f4c938--reactjs.netlify.com",
230+
"permanent": false
231+
},
232+
{
233+
"source": "/version/16.3",
234+
"destination": "https://5b05c94e0733d530fd1fafe0--reactjs.netlify.com",
235+
"permanent": false
236+
},
237+
{
238+
"source": "/version/16.2",
239+
"destination": "https://5abc31d8be40f1556f06c4be--reactjs.netlify.com",
240+
"permanent": false
241+
},
242+
{
243+
"source": "/version/16.1",
244+
"destination": "https://5a1dbcf14c4b93299e65b9a9--reactjs.netlify.com",
245+
"permanent": false
246+
},
247+
{
248+
"source": "/version/16.0",
249+
"destination": "https://5a046bf5a6188f4b8fa4938a--reactjs.netlify.com",
250+
"permanent": false
251+
}
45252
]
46-
}
253+
}

0 commit comments

Comments
 (0)