fix: nextjs unreliable params in route

This commit is contained in:
iCrawl
2023-12-21 05:19:32 +01:00
parent 1c3211a5fc
commit 520d6f64dd
2 changed files with 6 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { fetchVersions } from '~/app/docAPI';
export async function GET(_: NextRequest, params: { package: string }) {
return NextResponse.json(await fetchVersions(params.package));
export async function GET(req: NextRequest) {
const packageName = req.nextUrl.pathname.split('/').slice(2, 3)[0] ?? 'discord.js';
return NextResponse.json(await fetchVersions(packageName));
}

View File

@@ -10,15 +10,14 @@ import useSWR from 'swr';
const isDev = process.env.NEXT_PUBLIC_LOCAL_DEV === 'true' ?? process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview';
// eslint-disable-next-line promise/prefer-await-to-then
const fetcher = async (url: string) => fetch(url).then(async (res) => res.json());
export default function VersionSelect({ versions }: { readonly versions: string[] }) {
const pathname = usePathname();
const packageName = pathname?.split('/').slice(3, 4)[0];
const branchName = pathname?.split('/').slice(4, 5)[0];
const { data } = useSWR<string[]>(`/api/${packageName}/versions`, { fetcher, fallbackData: versions });
const { data } = useSWR<string[]>(packageName ? `/api/${packageName}/versions` : null, {
fallbackData: versions,
});
const versionMenu = useMenuState({
gutter: 8,