Skip to content

Commit 576443c

Browse files
committed
fix(website): don't crash if no version was found
1 parent c92a8c2 commit 576443c

File tree

6 files changed

+75
-55
lines changed

6 files changed

+75
-55
lines changed

apps/website/src/app/docs/packages/[packageName]/[version]/[[...item]]/page.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { readFile } from 'node:fs/promises';
22
import { join } from 'node:path';
33
import rehypeShikiFromHighlighter from '@shikijs/rehype/core';
44
import type { Metadata } from 'next';
5+
import { notFound } from 'next/navigation';
56
import { MDXRemote } from 'next-mdx-remote-client/rsc';
67
import remarkGfm from 'remark-gfm';
78
import { DocItem } from '@/components/DocItem';
@@ -89,13 +90,18 @@ export default async function Page({
8990
}
9091

9192
const entryPointString = parsedEntrypoints.join('.');
93+
9294
const node = await fetchNode({
9395
entryPoint: entryPointString,
9496
item: decodeURIComponent(foundItem),
9597
packageName,
9698
version,
9799
});
98100

101+
if (!node) {
102+
notFound();
103+
}
104+
99105
return (
100106
<main className="mx-auto flex w-full max-w-screen-xl flex-col gap-8 px-6 py-4">
101107
<DocItem node={node} packageName={packageName} version={version} />

apps/website/src/middleware.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,6 @@
1-
import Cloudflare from 'cloudflare';
21
import { NextResponse, type NextRequest } from 'next/server';
3-
import { DEFAULT_ENTRY_POINT, PACKAGES, PACKAGES_WITH_ENTRY_POINTS } from './util/constants';
4-
import { ENV } from './util/env';
5-
6-
const client = new Cloudflare({
7-
apiToken: process.env.CF_D1_DOCS_API_KEY,
8-
});
9-
10-
async function fetchLatestVersion(packageName: string): Promise<string> {
11-
const hasEntryPoints = PACKAGES_WITH_ENTRY_POINTS.includes(packageName);
12-
13-
if (ENV.IS_LOCAL_DEV) {
14-
if (hasEntryPoints) {
15-
return ['main', ...DEFAULT_ENTRY_POINT].join('/');
16-
}
17-
18-
return 'main';
19-
}
20-
21-
try {
22-
const { result } = await client.d1.database.query(process.env.CF_D1_DOCS_ID!, {
23-
account_id: process.env.CF_ACCOUNT_ID!,
24-
sql: `select version from documentation where name = ? and version != 'main' order by version desc limit 1;`,
25-
params: [packageName],
26-
});
27-
28-
return `${(result[0]?.results as { version: string }[] | undefined)?.[0]?.version ?? 'main'}${hasEntryPoints ? ['', ...DEFAULT_ENTRY_POINT].join('/') : ''}`;
29-
} catch {
30-
return '';
31-
}
32-
}
2+
import { PACKAGES } from '@/util/constants';
3+
import { fetchLatestVersion } from '@/util/fetchLatestVersion';
334

345
export default async function middleware(request: NextRequest) {
356
if (request.nextUrl.pathname === '/docs') {

apps/website/src/util/fetchEntryPoints.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ export async function fetchEntryPoints(packageName: string, version: string) {
1111
}
1212

1313
if (ENV.IS_LOCAL_DEV) {
14-
const fileContent = await readFile(
15-
join(
16-
process.cwd(),
17-
`${hasEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.entrypoints.api.json`,
18-
),
19-
'utf8',
20-
);
14+
try {
15+
const fileContent = await readFile(
16+
join(
17+
process.cwd(),
18+
`${hasEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.entrypoints.api.json`,
19+
),
20+
'utf8',
21+
);
2122

22-
return JSON.parse(fileContent);
23+
return JSON.parse(fileContent);
24+
} catch {
25+
return null;
26+
}
2327
}
2428

2529
const isMain = version === 'main';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Cloudflare } from 'cloudflare';
2+
import { PACKAGES_WITH_ENTRY_POINTS, DEFAULT_ENTRY_POINT } from '@/util/constants';
3+
import { ENV } from '@/util/env';
4+
5+
const client = new Cloudflare({
6+
apiToken: process.env.CF_D1_DOCS_API_KEY,
7+
});
8+
9+
export async function fetchLatestVersion(packageName: string): Promise<string> {
10+
const hasEntryPoints = PACKAGES_WITH_ENTRY_POINTS.includes(packageName);
11+
12+
if (ENV.IS_LOCAL_DEV) {
13+
if (hasEntryPoints) {
14+
return ['main', ...DEFAULT_ENTRY_POINT].join('/');
15+
}
16+
17+
return 'main';
18+
}
19+
20+
try {
21+
const { result } = await client.d1.database.query(process.env.CF_D1_DOCS_ID!, {
22+
account_id: process.env.CF_ACCOUNT_ID!,
23+
sql: `select version from documentation where name = ? and version != 'main' order by version desc limit 1;`,
24+
params: [packageName],
25+
});
26+
27+
return `${(result[0]?.results as { version: string }[] | undefined)?.[0]?.version ?? 'main'}${hasEntryPoints ? ['', ...DEFAULT_ENTRY_POINT].join('/') : ''}`;
28+
} catch {
29+
return '';
30+
}
31+
}

apps/website/src/util/fetchNode.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ export async function fetchNode({
1919
const normalizeItem = item.replaceAll(':', '.').toLowerCase();
2020

2121
if (ENV.IS_LOCAL_DEV) {
22-
const fileContent = await readFile(
23-
join(
24-
process.cwd(),
25-
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}${normalizeItem}.api.json`,
26-
),
27-
'utf8',
28-
);
22+
try {
23+
const fileContent = await readFile(
24+
join(
25+
process.cwd(),
26+
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}${normalizeItem}.api.json`,
27+
),
28+
'utf8',
29+
);
2930

30-
return JSON.parse(fileContent);
31+
return JSON.parse(fileContent);
32+
} catch {
33+
return null;
34+
}
3135
}
3236

3337
const isMain = version === 'main';

apps/website/src/util/fetchSitemap.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ export async function fetchSitemap({
1616
const normalizedEntryPoint = entryPoint ? `${entryPoint}.` : '';
1717

1818
if (ENV.IS_LOCAL_DEV) {
19-
const fileContent = await readFile(
20-
join(
21-
process.cwd(),
22-
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}sitemap.api.json`,
23-
),
24-
'utf8',
25-
);
19+
try {
20+
const fileContent = await readFile(
21+
join(
22+
process.cwd(),
23+
`${hasEntryPoint || normalizedEntryPoint ? `../../../discord-api-types` : `../../packages/${packageName}`}/docs/${packageName}/split/${version}.${normalizedEntryPoint}sitemap.api.json`,
24+
),
25+
'utf8',
26+
);
2627

27-
return JSON.parse(fileContent);
28+
return JSON.parse(fileContent);
29+
} catch {
30+
return null;
31+
}
2832
}
2933

3034
const isMain = version === 'main';

0 commit comments

Comments
 (0)