mirror of
https://github.com/discordjs/discord.js.git
synced 2024-08-21 03:04:47 +12:00
feat(website): feature flag package and version selection
This commit is contained in:
parent
b097b25116
commit
51de9668e5
1 changed files with 17 additions and 4 deletions
|
@ -1,7 +1,23 @@
|
|||
import { get } from '@vercel/edge-config';
|
||||
import { NextResponse, type NextRequest } from 'next/server';
|
||||
import { PACKAGES } from './util/constants';
|
||||
|
||||
async function fetchLatestVersion(packageName: string) {
|
||||
const res = await fetch(`https://docs.discordjs.dev/api/info?package=${packageName}`);
|
||||
const data: string[] = await res.json();
|
||||
|
||||
return data.at(-2);
|
||||
}
|
||||
|
||||
export default async function middleware(request: NextRequest) {
|
||||
if (request.nextUrl.pathname === '/docs') {
|
||||
const skip = await get<boolean>('SKIP_PACKAGE_VERSION_SELECTION');
|
||||
if (skip) {
|
||||
const latestVersion = await fetchLatestVersion('builders');
|
||||
return NextResponse.redirect(new URL(`/docs/packages/builders/${latestVersion}`, request.url));
|
||||
}
|
||||
}
|
||||
|
||||
if (request.nextUrl.pathname.includes('discord.js')) {
|
||||
return NextResponse.redirect('https://discord.js.org/#/docs/discord.js');
|
||||
}
|
||||
|
@ -9,10 +25,7 @@ export default async function middleware(request: NextRequest) {
|
|||
if (PACKAGES.some((pkg) => request.nextUrl.pathname.includes(pkg))) {
|
||||
// eslint-disable-next-line prefer-named-capture-group
|
||||
const packageName = /\/docs\/packages\/([^/]+)\/.*/.exec(request.nextUrl.pathname)?.[1] ?? 'builders';
|
||||
const res = await fetch(`https://docs.discordjs.dev/api/info?package=${packageName}`);
|
||||
const data: string[] = await res.json();
|
||||
|
||||
const latestVersion = data.at(-2);
|
||||
const latestVersion = await fetchLatestVersion(packageName);
|
||||
return NextResponse.redirect(
|
||||
new URL(request.nextUrl.pathname.replace('stable', latestVersion ?? 'main'), request.url),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue