Skip to content

Commit

Permalink
fix: prerender bailout
Browse files Browse the repository at this point in the history
  • Loading branch information
iCrawl committed May 24, 2024
1 parent 7f467ed commit a35d760
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { DocItem } from '~/components/DocItem';
import { fetchNode } from '~/util/fetchNode';

Expand All @@ -25,6 +26,10 @@ export default async function Page({
}) {
const node = await fetchNode({ item: params.item, packageName: params.packageName, version: params.version });

if (!node) {
notFound();
}

return (
<main className="flex w-full flex-col gap-8 pb-12 md:pb-0">
<DocItem node={node} packageName={params.packageName} version={params.version} />
Expand Down
6 changes: 6 additions & 0 deletions apps/website/src/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { VscGithubInverted } from '@react-icons/all-files/vsc/VscGithubInverted'
import { ChevronDown, ChevronUp } from 'lucide-react';
import dynamic from 'next/dynamic';
import Link from 'next/link';
import { notFound } from 'next/navigation';
import { fetchSitemap } from '~/util/fetchSitemap';
import { fetchVersions } from '~/util/fetchVersions';
import { resolveNodeKind } from './DocKind';
Expand All @@ -28,6 +29,11 @@ export async function Navigation({
readonly version: string;
}) {
const node = await fetchSitemap({ packageName, version });

if (!node) {
notFound();
}

const versions = await fetchVersions(packageName);

const groupedNodes = node.reduce((acc: any, node: any) => {
Expand Down
39 changes: 14 additions & 25 deletions apps/website/src/util/fetchNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { notFound } from 'next/navigation';
import { ENV } from './env';

export async function fetchNode({
Expand All @@ -15,32 +14,22 @@ export async function fetchNode({
const normalizeItem = item.split(encodeURIComponent(':')).join('.').toLowerCase();

if (ENV.IS_LOCAL_DEV) {
try {
const fileContent = await readFile(
join(
process.cwd(),
`../../packages/${packageName}/docs/${packageName}/split/${version}.${normalizeItem}.api.json`,
),
'utf8',
);
const fileContent = await readFile(
join(
process.cwd(),
`../../packages/${packageName}/docs/${packageName}/split/${version}.${normalizeItem}.api.json`,
),
'utf8',
);

return JSON.parse(fileContent);
} catch (error_) {
console.error(error_);
notFound();
}
return JSON.parse(fileContent);
}

try {
const isMainVersion = version === 'main';
const fileContent = await fetch(
`${process.env.BLOB_STORAGE_URL}/rewrite/${packageName}/${version}.${normalizeItem}.api.json`,
{ next: isMainVersion ? { revalidate: 0 } : { revalidate: 604_800 } },
);
const isMainVersion = version === 'main';
const fileContent = await fetch(
`${process.env.BLOB_STORAGE_URL}/rewrite/${packageName}/${version}.${normalizeItem}.api.json`,
{ next: isMainVersion ? { revalidate: 0 } : { revalidate: 604_800 } },
);

return await fileContent.json();
} catch (error_) {
console.error(error_);
notFound();
}
return fileContent.json();
}
33 changes: 11 additions & 22 deletions apps/website/src/util/fetchSitemap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { notFound } from 'next/navigation';
import { ENV } from './env';

export async function fetchSitemap({
Expand All @@ -11,29 +10,19 @@ export async function fetchSitemap({
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',
);
const fileContent = await readFile(
join(process.cwd(), `../../packages/${packageName}/docs/${packageName}/split/${version}.sitemap.api.json`),
'utf8',
);

return JSON.parse(fileContent);
} catch (error_) {
console.error(error_);
notFound();
}
return JSON.parse(fileContent);
}

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 } },
);
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 (error_) {
console.error(error_);
notFound();
}
return fileContent.json();
}

0 comments on commit a35d760

Please sign in to comment.