Skip to content

Commit f043428

Browse files
authored
chore: Cleanup docs (#1284)
1 parent 671c383 commit f043428

13,588 files changed

Lines changed: 83 additions & 495439 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

documentation/app/c-at-e-file-server.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
11
import parseRange from 'range-parser'
2+
import versionRedirects from './version-redirects.json'
3+
4+
/**
5+
* Check if the request path is for an old minor version and should be redirected
6+
* to the latest minor version within the same major version.
7+
* @param {Request} request - The incoming request
8+
* @returns {Response | null} - Returns a redirect Response if applicable, else null
9+
*/
10+
function redirectMinorVersions(request) {
11+
const url = new URL(request.url);
12+
const pathMatch = url.pathname.match(/^\/docs\/([^\/]+)(\/.*)?$/);
13+
14+
if (!pathMatch) {
15+
return null;
16+
}
17+
18+
const [, version, restOfPath = ''] = pathMatch;
19+
20+
const majorVersion = version.split('.')[0];
21+
const targetVersion = versionRedirects.latestByMajor[majorVersion];
22+
23+
if (targetVersion === null || (targetVersion && targetVersion !== version)) {
24+
// Construct the new URL with the latest version
25+
const versionPrefix = targetVersion === null ? '' : `/${targetVersion}`;
26+
const newPath = `/docs${versionPrefix}${restOfPath}`;
27+
const newUrl = new URL(newPath, url.origin);
28+
newUrl.search = url.search; // Preserve query parameters
29+
30+
// Return a 301 permanent redirect
31+
return new Response(null, {
32+
status: 301,
33+
headers: {
34+
'Location': newUrl.toString(),
35+
'Cache-Control': 'public, max-age=3600'
36+
}
37+
});
38+
}
39+
40+
return null;
41+
}
242

343
/**
444
* Attempt to locate the requested resource from a Fastly KV Store,
@@ -15,6 +55,12 @@ export async function get(store_name, request) {
1555
return null
1656
}
1757

58+
// Check if this request should be redirected to a newer minor version
59+
const redirect = redirectMinorVersions(request);
60+
if (redirect) {
61+
return redirect;
62+
}
63+
1864
// if path ends in / or does not have an extension
1965
// then append /index.html to the end so we can serve a page
2066
let path = new URL(request.url).pathname
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"latestByMajor": {
3+
"1": "1.13.0",
4+
"2": "2.5.0",
5+
"3": null
6+
}
7+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
import { readFileSync, writeFileSync } from 'fs';
4+
import { join, dirname } from 'path';
5+
import { fileURLToPath } from 'url';
6+
7+
const __dirname = dirname(fileURLToPath(import.meta.url));
8+
9+
const versionsPath = join(__dirname, 'versions.json');
10+
const versions = JSON.parse(readFileSync(versionsPath, 'utf-8'));
11+
12+
// Find latest version for each major version (versions.json is already sorted newest first)
13+
const latestByMajor = {};
14+
for (const version of versions) {
15+
const major = version.split('.')[0];
16+
if (!latestByMajor[major]) {
17+
latestByMajor[major] = version;
18+
}
19+
}
20+
21+
// Latest major redirects to /docs/ (no version in path)
22+
const latestMajor = Math.max(...Object.keys(latestByMajor).map(Number));
23+
latestByMajor[latestMajor] = null;
24+
25+
const outputPath = join(__dirname, 'app', 'version-redirects.json');
26+
writeFileSync(outputPath, JSON.stringify({ latestByMajor }, null, 2));

documentation/package-lock.json

Lines changed: 1 addition & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

documentation/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
"scripts": {
33
"docusaurus": "docusaurus",
44
"start": "docusaurus start",
5-
"build": "node --max-old-space-size=8000 node_modules/.bin/docusaurus build",
5+
"build": "node --max-old-space-size=8000 node_modules/.bin/docusaurus build && npm run generate-version-redirects",
66
"swizzle": "docusaurus swizzle",
77
"deploy": "npm run build && cd app && npm install && npm run deploy",
88
"clear": "docusaurus clear",
99
"serve": "docusaurus serve",
1010
"write-translations": "docusaurus write-translations",
1111
"write-heading-ids": "docusaurus write-heading-ids",
1212
"remove-fastly-prefix": "node rename-docs.mjs remove-prefix",
13-
"add-fastly-prefix": "node rename-docs.mjs add-prefix"
13+
"add-fastly-prefix": "node rename-docs.mjs add-prefix",
14+
"generate-version-redirects": "node generate-version-redirects.mjs"
1415
},
1516
"devDependencies": {
1617
"@cmfcmf/docusaurus-search-local": "^2.0.1",

0 commit comments

Comments
 (0)