-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
fetchSitemap.ts
37 lines (33 loc) · 875 Bytes
/
fetchSitemap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { notFound } from 'next/navigation';
import { ENV } from './env';
export async function fetchSitemap({
packageName,
version,
}: {
readonly packageName: string;
readonly version: string;
}) {
if (ENV.IS_LOCAL_DEV) {
try {
const fileContent = await readFile(
join(process.cwd(), `../../packages/${packageName}/docs/${packageName}/split/${version}.sitemap.api.json`),
'utf8',
);
return JSON.parse(fileContent);
} catch {
notFound();
}
}
try {
const isMainVersion = version === 'main';
const fileContent = await fetch(
`${process.env.BLOB_STORAGE_URL}/rewrite/${packageName}/${version}.sitemap.api.json`,
{ next: isMainVersion ? { revalidate: 0 } : { revalidate: 604_800 } },
);
return await fileContent.json();
} catch {
notFound();
}
}