mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
fix(website): don't crash if no version was found
This commit is contained in:
@@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises';
|
|||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import rehypeShikiFromHighlighter from '@shikijs/rehype/core';
|
import rehypeShikiFromHighlighter from '@shikijs/rehype/core';
|
||||||
import type { Metadata } from 'next';
|
import type { Metadata } from 'next';
|
||||||
|
import { notFound } from 'next/navigation';
|
||||||
import { MDXRemote } from 'next-mdx-remote-client/rsc';
|
import { MDXRemote } from 'next-mdx-remote-client/rsc';
|
||||||
import remarkGfm from 'remark-gfm';
|
import remarkGfm from 'remark-gfm';
|
||||||
import { DocItem } from '@/components/DocItem';
|
import { DocItem } from '@/components/DocItem';
|
||||||
@@ -89,6 +90,7 @@ export default async function Page({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const entryPointString = parsedEntrypoints.join('.');
|
const entryPointString = parsedEntrypoints.join('.');
|
||||||
|
|
||||||
const node = await fetchNode({
|
const node = await fetchNode({
|
||||||
entryPoint: entryPointString,
|
entryPoint: entryPointString,
|
||||||
item: decodeURIComponent(foundItem),
|
item: decodeURIComponent(foundItem),
|
||||||
@@ -96,6 +98,10 @@ export default async function Page({
|
|||||||
version,
|
version,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!node) {
|
||||||
|
notFound();
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="mx-auto flex w-full max-w-screen-xl flex-col gap-8 px-6 py-4">
|
<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} />
|
<DocItem node={node} packageName={packageName} version={version} />
|
||||||
|
|||||||
@@ -1,35 +1,6 @@
|
|||||||
import Cloudflare from 'cloudflare';
|
|
||||||
import { NextResponse, type NextRequest } from 'next/server';
|
import { NextResponse, type NextRequest } from 'next/server';
|
||||||
import { DEFAULT_ENTRY_POINT, PACKAGES, PACKAGES_WITH_ENTRY_POINTS } from './util/constants';
|
import { PACKAGES } from '@/util/constants';
|
||||||
import { ENV } from './util/env';
|
import { fetchLatestVersion } from '@/util/fetchLatestVersion';
|
||||||
|
|
||||||
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 '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function middleware(request: NextRequest) {
|
export default async function middleware(request: NextRequest) {
|
||||||
if (request.nextUrl.pathname === '/docs') {
|
if (request.nextUrl.pathname === '/docs') {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export async function fetchEntryPoints(packageName: string, version: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ENV.IS_LOCAL_DEV) {
|
if (ENV.IS_LOCAL_DEV) {
|
||||||
|
try {
|
||||||
const fileContent = await readFile(
|
const fileContent = await readFile(
|
||||||
join(
|
join(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
@@ -20,6 +21,9 @@ export async function fetchEntryPoints(packageName: string, version: string) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return JSON.parse(fileContent);
|
return JSON.parse(fileContent);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMain = version === 'main';
|
const isMain = version === 'main';
|
||||||
|
|||||||
31
apps/website/src/util/fetchLatestVersion.ts
Normal file
31
apps/website/src/util/fetchLatestVersion.ts
Normal 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 '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,6 +19,7 @@ export async function fetchNode({
|
|||||||
const normalizeItem = item.replaceAll(':', '.').toLowerCase();
|
const normalizeItem = item.replaceAll(':', '.').toLowerCase();
|
||||||
|
|
||||||
if (ENV.IS_LOCAL_DEV) {
|
if (ENV.IS_LOCAL_DEV) {
|
||||||
|
try {
|
||||||
const fileContent = await readFile(
|
const fileContent = await readFile(
|
||||||
join(
|
join(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
@@ -28,6 +29,9 @@ export async function fetchNode({
|
|||||||
);
|
);
|
||||||
|
|
||||||
return JSON.parse(fileContent);
|
return JSON.parse(fileContent);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMain = version === 'main';
|
const isMain = version === 'main';
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export async function fetchSitemap({
|
|||||||
const normalizedEntryPoint = entryPoint ? `${entryPoint}.` : '';
|
const normalizedEntryPoint = entryPoint ? `${entryPoint}.` : '';
|
||||||
|
|
||||||
if (ENV.IS_LOCAL_DEV) {
|
if (ENV.IS_LOCAL_DEV) {
|
||||||
|
try {
|
||||||
const fileContent = await readFile(
|
const fileContent = await readFile(
|
||||||
join(
|
join(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
@@ -25,6 +26,9 @@ export async function fetchSitemap({
|
|||||||
);
|
);
|
||||||
|
|
||||||
return JSON.parse(fileContent);
|
return JSON.parse(fileContent);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMain = version === 'main';
|
const isMain = version === 'main';
|
||||||
|
|||||||
Reference in New Issue
Block a user