docs: Point path versioned requests to new sites (#7531)

Following #7528, we also need to route any requests using the old
versioned paths to the correct location.

Signed-off-by: Charlie Egan <charlie@styra.com>
This commit is contained in:
Charlie Egan
2025-04-23 13:54:07 +00:00
committed by GitHub
parent d65888c14f
commit e490277477
2 changed files with 43 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
// Redirect path versioned requests to archived versions of the OPA
// documentation.
// Context: https://github.com/open-policy-agent/opa/issues/7037
import { Context } from "@netlify/edge-functions";
export default async (
request: Request,
context: Context
): Promise<Response | undefined> => {
const url: URL = new URL(request.url);
const pathname: string = url.pathname;
// this is the name of the archived site project and forms the base of all
// archived sites.
const siteName: string = "opa-docs";
const versionRegex: RegExp = /^\/docs\/v(\d+\.\d+\.\d+)(\/.*)?$/;
const match: RegExpMatchArray | null = pathname.match(versionRegex);
if (match) {
const version: string = match[1]; // e.g., "0.65.0"
const restOfPath: string = match[2] || "/"; // e.g., "/introduction" or defaults to "/"
// Format version for the Netlify subdomain: replace dots with dashes
const formattedVersion: string = version.replace(/\./g, "-"); // e.g., "0-65-0"
// Construct the target archive URL
const targetDomain: string = `v${formattedVersion}--${siteName}.netlify.app`;
const targetUrl: string = `https://${targetDomain}${restOfPath}`;
console.log(`Edge Function: Redirecting ${pathname} to ${targetUrl}`);
return Response.redirect(targetUrl, 301);
}
return undefined;
};
+6
View File
@@ -8,6 +8,12 @@ edge_functions = "docs/website/edge"
path = "/badge-endpoint/*"
function = "badge"
# Redirect all path based versioned requests to their new archived sites.
# https://github.com/open-policy-agent/opa/issues/7037
[[edge_functions]]
path = "/docs/*"
function = "version-redirect"
[build.environment]
# keep this version in sync with the version in .github/workflows/pull-request.yml
# Note, that upgrading Hugo is tricky as all versions of the OPA docs need to work