Skip to content

Commit 831a9c1

Browse files
authored
feat: redirect current version urls (#3680)
Allows our CLI tool to user the latest version in the path, and gracefully handles this server side: https://reactnative.dev/docs/0.71/X -> https://reactnative.dev/docs/X
1 parent a4f93e7 commit 831a9c1

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

scripts/update-redirect/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
* @format
9+
*/
10+
11+
const fs = require('fs');
12+
const path = require('path');
13+
const assert = require('assert');
14+
15+
if (process.argv.length < 4) {
16+
console.log(
17+
`Usage: update-redirect <path to _redirects> <path to versions.json>`
18+
);
19+
process.exit(1);
20+
}
21+
22+
const [redirects, versions] = process.argv.slice(2);
23+
assert.match(
24+
redirects,
25+
/_redirects$/,
26+
'Expects _redirects path as 2nd argument'
27+
);
28+
assert.match(
29+
versions,
30+
/versions.json$/,
31+
'Expects versions.jsoon path as 3nd argument'
32+
);
33+
34+
const latestPublicVersion = JSON.parse(fs.readFileSync(versions, 'utf8'))[0];
35+
const VERSION_KEY = '$LATEST_VERSION$';
36+
37+
const before = fs.readFileSync(redirects, 'utf8');
38+
39+
if (!before.includes(VERSION_KEY)) {
40+
console.warn(
41+
`yarn run update-redirect is expecting to find ${VERSION_KEY} in '${redirects}
42+
43+
This is a problem because any direct linking to ${latestPublicVersion} must be redirected:
44+
- from: https://reactnative.dev/docs/${latestPublicVersion}/...
45+
- to: https://reactnative.dev/docs/...
46+
47+
Someone must have committed the _redirects after build.`
48+
);
49+
process.exit(1);
50+
}
51+
52+
fs.writeFileSync(redirects, before.replace(VERSION_KEY, latestPublicVersion));
53+
console.log(
54+
`Successfully added direct for /docs/${latestPublicVersion}/* -> /docs/*`
55+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@react-native-website/update-redirects",
3+
"version": "0.0.0",
4+
"private": true,
5+
"bin": {
6+
"update-redirect": "./index.js"
7+
}
8+
}

website/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"scripts": {
1313
"start": "docusaurus start",
14-
"build": "docusaurus build",
14+
"build": "docusaurus build && yarn run update-redirect ./build/_redirects ./versions.json",
1515
"swizzle": "docusaurus swizzle",
1616
"deploy": "docusaurus deploy",
1717
"serve": "docusaurus serve",
@@ -58,6 +58,7 @@
5858
"devDependencies": {
5959
"@docusaurus/types": "^2.3.1",
6060
"@react-native-website/lint-examples": "0.0.0",
61+
"@react-native-website/update-redirects": "0.0.0",
6162
"alex": "^10.0.0",
6263
"fs-extra": "^10.1.0",
6364
"glob": "^8.0.3",

website/static/_redirects

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@
4747
/docs/0.66/architecture-glossary /architecture/glossary
4848

4949
# Rework of the community
50-
/help /community/overview
50+
/help /community/overview
51+
52+
# Latest version might be linked to directly and should redirect
53+
/docs/$LATEST_VERSION$/* /docs/:splat

0 commit comments

Comments
 (0)