fix(website): don't crash if no version was found

This commit is contained in:
iCrawl
2025-05-15 22:05:38 +02:00
parent c92a8c27a2
commit 576443c29a
6 changed files with 75 additions and 55 deletions

View File

@@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import rehypeShikiFromHighlighter from '@shikijs/rehype/core';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { MDXRemote } from 'next-mdx-remote-client/rsc';
import remarkGfm from 'remark-gfm';
import { DocItem } from '@/components/DocItem';
@@ -89,6 +90,7 @@ export default async function Page({
}
const entryPointString = parsedEntrypoints.join('.');
const node = await fetchNode({
entryPoint: entryPointString,
item: decodeURIComponent(foundItem),
@@ -96,6 +98,10 @@ export default async function Page({
version,
});
if (!node) {
notFound();
}
return (
<main className="mx-auto flex w-full max-w-screen-xl flex-col gap-8 px-6 py-4">
<DocItem node={node} packageName={packageName} version={version} />

View File

@@ -1,35 +1,6 @@
import Cloudflare from 'cloudflare';
import { NextResponse, type NextRequest } from 'next/server';
import { DEFAULT_ENTRY_POINT, PACKAGES, PACKAGES_WITH_ENTRY_POINTS } from './util/constants';
import { ENV } from './util/env';
const client = new Cloudflare({
apiToken: process.env.CF_D1_DOCS_API_KEY,
});
async function fetchLatestVersion(packageName: string): Promise<string> {
const hasEntryPoints = PACKAGES_WITH_ENTRY_POINTS.includes(packageName);
if (ENV.IS_LOCAL_DEV) {
if (hasEntryPoints) {
return ['main', ...DEFAULT_ENTRY_POINT].join('/');
}
return 'main';
}
try {
const { result } = await client.d1.database.query(process.env.CF_D1_DOCS_ID!, {
account_id: process.env.CF_ACCOUNT_ID!,
sql: `select version from documentation where name = ? and version != 'main' order by version desc limit 1;`,
params: [packageName],
});
return `${(result[0]?.results as { version: string }[] | undefined)?.[0]?.version ?? 'main'}${hasEntryPoints ? ['', ...DEFAULT_ENTRY_POINT].join('/') : ''}`;
} catch {
return '';
}
}
import { PACKAGES } from '@/util/constants';
import { fetchLatestVersion } from '@/util/fetchLatestVersion';
export default async function middleware(request: NextRequest) {
if (request.nextUrl.pathname === '/docs') {

View File

@@ -11,15 +11,19 @@ export async function fetchEntryPoints(packageName: string, version: string) {
}
if (ENV.IS_LOCAL_DEV) {
const fileContent = await readFile(
join(
process.cwd(),
`${hasEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.entrypoints.api.json`,
),
'utf8',
);
try {
const fileContent = await readFile(
join(
process.cwd(),
`${hasEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.entrypoints.api.json`,
),
'utf8',
);
return JSON.parse(fileContent);
return JSON.parse(fileContent);
} catch {
return null;
}
}
const isMain = version === 'main';

View File

@@ -0,0 +1,31 @@
import { Cloudflare } from 'cloudflare';
import { PACKAGES_WITH_ENTRY_POINTS, DEFAULT_ENTRY_POINT } from '@/util/constants';
import { ENV } from '@/util/env';
const client = new Cloudflare({
apiToken: process.env.CF_D1_DOCS_API_KEY,
});
export async function fetchLatestVersion(packageName: string): Promise<string> {
const hasEntryPoints = PACKAGES_WITH_ENTRY_POINTS.includes(packageName);
if (ENV.IS_LOCAL_DEV) {
if (hasEntryPoints) {
return ['main', ...DEFAULT_ENTRY_POINT].join('/');
}
return 'main';
}
try {
const { result } = await client.d1.database.query(process.env.CF_D1_DOCS_ID!, {
account_id: process.env.CF_ACCOUNT_ID!,
sql: `select version from documentation where name = ? and version != 'main' order by version desc limit 1;`,
params: [packageName],
});
return `${(result[0]?.results as { version: string }[] | undefined)?.[0]?.version ?? 'main'}${hasEntryPoints ? ['', ...DEFAULT_ENTRY_POINT].join('/') : ''}`;
} catch {
return '';
}
}

View File

@@ -19,15 +19,19 @@ export async function fetchNode({
const normalizeItem = item.replaceAll(':', '.').toLowerCase();
if (ENV.IS_LOCAL_DEV) {
const fileContent = await readFile(
join(
process.cwd(),
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}${normalizeItem}.api.json`,
),
'utf8',
);
try {
const fileContent = await readFile(
join(
process.cwd(),
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}${normalizeItem}.api.json`,
),
'utf8',
);
return JSON.parse(fileContent);
return JSON.parse(fileContent);
} catch {
return null;
}
}
const isMain = version === 'main';

View File

@@ -16,15 +16,19 @@ export async function fetchSitemap({
const normalizedEntryPoint = entryPoint ? `${entryPoint}.` : '';
if (ENV.IS_LOCAL_DEV) {
const fileContent = await readFile(
join(
process.cwd(),
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}sitemap.api.json`,
),
'utf8',
);
try {
const fileContent = await readFile(
join(
process.cwd(),
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}sitemap.api.json`,
),
'utf8',
);
return JSON.parse(fileContent);
return JSON.parse(fileContent);
} catch {
return null;
}
}
const isMain = version === 'main';