refactor: correctly generate routes

This commit is contained in:
iCrawl
2022-11-30 03:55:23 +01:00
parent 90d192078f
commit 2849af0118
5 changed files with 89 additions and 90 deletions

View File

@@ -30,7 +30,7 @@ import shikiLangJavascript from 'shiki/languages/javascript.tmLanguage.json';
import shikiLangTypescript from 'shiki/languages/typescript.tmLanguage.json'; import shikiLangTypescript from 'shiki/languages/typescript.tmLanguage.json';
import shikiThemeDarkPlus from 'shiki/themes/dark-plus.json'; import shikiThemeDarkPlus from 'shiki/themes/dark-plus.json';
import shikiThemeLightPlus from 'shiki/themes/light-plus.json'; import shikiThemeLightPlus from 'shiki/themes/light-plus.json';
import vercelLogo from '../../../assets/powered-by-vercel.svg'; import vercelLogo from '../../../../../assets/powered-by-vercel.svg';
import { MDXRemote } from '~/components/MDXRemote'; import { MDXRemote } from '~/components/MDXRemote';
import { Nav } from '~/components/Nav'; import { Nav } from '~/components/Nav';
import { Class } from '~/components/model/Class'; import { Class } from '~/components/model/Class';
@@ -44,18 +44,14 @@ import { DESCRIPTION, PACKAGES } from '~/util/constants';
import { findMember, findMemberByKey } from '~/util/model.server'; import { findMember, findMemberByKey } from '~/util/model.server';
import { tryResolveDescription } from '~/util/summary'; import { tryResolveDescription } from '~/util/summary';
export async function generateStaticParams() { export async function generateStaticParams({ params }: { params: { package: string } }) {
return ( const packageName = params.package;
await Promise.all(
PACKAGES.map(async (packageName) => {
try { try {
let data: any[] = []; let data: any[] = [];
let versions: string[] = []; let versions: string[] = [];
if (process.env.NEXT_PUBLIC_LOCAL_DEV) { if (process.env.NEXT_PUBLIC_LOCAL_DEV) {
const res = await readFile( const res = await readFile(join(cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'), 'utf8');
join(cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'),
'utf8',
);
data = JSON.parse(res); data = JSON.parse(res);
} else { } else {
const response = await fetch(`https://docs.discordjs.dev/api/info?package=${packageName}`); const response = await fetch(`https://docs.discordjs.dev/api/info?package=${packageName}`);
@@ -111,17 +107,29 @@ export async function generateStaticParams() {
}), }),
]; ];
} catch { } catch {
return { slug: ['packages', '404'] }; return { slug: [] };
} }
}),
)
).flat();
} }
async function getData(slug: string[]) { async function getData(packageName: string, slug: string[]) {
const [path, packageName = 'builders', branchName = 'main', member] = slug; const [branchName = 'main', member] = slug;
if (path !== 'packages' || !PACKAGES.includes(packageName)) { if (!PACKAGES.includes(packageName)) {
notFound();
}
let data;
try {
if (process.env.NEXT_PUBLIC_LOCAL_DEV) {
const res = await readFile(join(cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'), 'utf8');
data = JSON.parse(res);
} else {
const res = await fetch(`https://docs.discordjs.dev/docs/${packageName}/${branchName}.api.json`, {
next: { revalidate: 3_600 },
});
data = await res.json();
}
} catch {
notFound(); notFound();
} }
@@ -161,21 +169,6 @@ async function getData(slug: string[]) {
}, },
}); });
let data;
try {
if (process.env.NEXT_PUBLIC_LOCAL_DEV) {
const res = await readFile(join(cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'), 'utf8');
data = JSON.parse(res);
} else {
const res = await fetch(`https://docs.discordjs.dev/docs/${packageName}/${branchName}.api.json`, {
next: { revalidate: 3_600 },
});
data = await res.json();
}
} catch {
notFound();
}
const model = createApiModel(data); const model = createApiModel(data);
const pkg = findPackage(model, packageName); const pkg = findPackage(model, packageName);
@@ -261,8 +254,8 @@ function member(props?: ApiItemJSON | undefined) {
} }
} }
export default async function Page({ params }: { params: { slug: string[] } }) { export default async function Page({ params }: { params: { package: string; slug: string[] } }) {
const data = await getData(params.slug); const data = await getData(params.package, params.slug);
// const name = useMemo( // const name = useMemo(
// () => `discord.js${params.data?.member?.name ? ` | ${params.data.member.name}` : ''}`, // () => `discord.js${params.data?.member?.name ? ` | ${params.data.member.name}` : ''}`,

View File

@@ -0,0 +1,10 @@
import type { PropsWithChildren } from 'react';
import { PACKAGES } from '~/util/constants';
export async function generateStaticParams() {
return PACKAGES.map((packageName) => ({ package: packageName }));
}
export default function PackageLayout({ children }: PropsWithChildren) {
return children;
}

View File

@@ -5,10 +5,6 @@ import Link from 'next/link';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';
import { PACKAGES } from '~/util/constants'; import { PACKAGES } from '~/util/constants';
export async function generateStaticParams() {
return PACKAGES.map((packageName) => ({ package: packageName }));
}
async function getData(pkg: string) { async function getData(pkg: string) {
if (!PACKAGES.includes(pkg)) { if (!PACKAGES.includes(pkg)) {
notFound(); notFound();