Skip to content

Commit

Permalink
Add redirects to docusaurus
Browse files Browse the repository at this point in the history
  • Loading branch information
RishhiB committed Oct 28, 2024
1 parent 5bbcabe commit 7ad2b7e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
59 changes: 59 additions & 0 deletions docs/build-redirects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

/*
* This script builds FF.com's redirects
*/

import path from "node:path";
import { fileURLToPath } from "node:url";

import chalk from "chalk";
import fs from "fs-extra";

const dirname = path.dirname(fileURLToPath(import.meta.url));

const {
params: { currentVersion, ltsVersion },
} = await fs.readJSON(path.resolve(dirname, "data", "versions.json"));

try {
const content = `/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
// Map of incoming URL paths to redirect URLs
const routes = new Map([
["/docs/apis", "/docs/api/${currentVersion}"],
["/docs/api/current", "/docs/api/${currentVersion}"],
["/docs/api/lts", "/docs/api/${ltsVersion}"],
]);
/**
* Handles incoming HTTP requests and redirects them to the appropriate URL based on the current and LTS versions.
* It reads the versions from /docs/data/versions.json and matches the incoming URL to a set of predefined routes.
* If a matching route is found, it constructs and returns the redirect URL. Otherwise, it returns a 404 response.
*/
module.exports = async (context, { headers }) => {
const { pathname, search } = new URL(headers["x-ms-original-url"]);
const route = [...routes].find(([path, _]) => pathname.startsWith(path));
context.res = {
status: route ? 302 : 404,
headers: { location: route ? \`\${pathname.replace(...route)}\${search}\` : "/404" },
};
};
`;
const versionPath = path.resolve(dirname, "api", "fallback", "index.js");
await fs.writeFile(versionPath, content);
} catch (error) {
console.error(chalk.red("Redirects could not be generated due to one or more errors:"));
console.error(error);
process.exit(1);
}

console.log(chalk.green("Redirects generated!"));
process.exit(0);
3 changes: 2 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
"author": "Microsoft and contributors",
"scripts": {
"api-markdown-documenter": "node ./infra/generate-api-documentation.mjs",
"build": "npm run build:api-documentation && npm run build:docusaurus",
"build": "npm run build:api-documentation && npm run build:docusaurus && npm run build:redirects",
"build:api-documentation": "npm run download-doc-models && npm run api-markdown-documenter",
"build:api-documentation:dev": "cross-env NODE_ENV=development npm run build:api-documentation",
"build:dev": "cross-env NODE_ENV=development npm run build",
"build:docusaurus": "npm run gen-docusaurus && docusaurus build",
"build:redirects": "node ./build-redirects.js",
"clean": "concurrently \"npm:clean:*\"",
"clean:api-documentation": "rimraf --glob \"docs/api/**/*.md\" \"versioned_docs/*/api/**/*.md\"",
"clean:doc-models": "rimraf --glob .doc-models",
Expand Down

0 comments on commit 7ad2b7e

Please sign in to comment.