From e4902774778da576da2a8f4b2fd50df6cc3da8b5 Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Wed, 23 Apr 2025 13:54:07 +0000 Subject: [PATCH] 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 --- docs/website/edge/version-redirect.ts | 37 +++++++++++++++++++++++++++ netlify.toml | 6 +++++ 2 files changed, 43 insertions(+) create mode 100644 docs/website/edge/version-redirect.ts diff --git a/docs/website/edge/version-redirect.ts b/docs/website/edge/version-redirect.ts new file mode 100644 index 0000000000..239d3e09b3 --- /dev/null +++ b/docs/website/edge/version-redirect.ts @@ -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 => { + 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; +}; \ No newline at end of file diff --git a/netlify.toml b/netlify.toml index d5358fae32..ab1957418a 100644 --- a/netlify.toml +++ b/netlify.toml @@ -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