diff --git a/docs/build-redirects.js b/docs/build-redirects.js new file mode 100644 index 000000000000..c7982b0419ca --- /dev/null +++ b/docs/build-redirects.js @@ -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); diff --git a/docs/package.json b/docs/package.json index 0aca579a6e8c..5ab826171c9d 100644 --- a/docs/package.json +++ b/docs/package.json @@ -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",