From a35d760421a447d6fc265413be9618e1832021d6 Mon Sep 17 00:00:00 2001 From: iCrawl Date: Fri, 24 May 2024 02:10:07 +0200 Subject: [PATCH] fix: prerender bailout --- .../[packageName]/[version]/[item]/page.tsx | 5 +++ apps/website/src/components/Navigation.tsx | 6 +++ apps/website/src/util/fetchNode.ts | 39 +++++++------------ apps/website/src/util/fetchSitemap.ts | 33 ++++++---------- 4 files changed, 36 insertions(+), 47 deletions(-) diff --git a/apps/website/src/app/docs/packages/[packageName]/[version]/[item]/page.tsx b/apps/website/src/app/docs/packages/[packageName]/[version]/[item]/page.tsx index 4b31faaf7d1a..dc188019eb80 100644 --- a/apps/website/src/app/docs/packages/[packageName]/[version]/[item]/page.tsx +++ b/apps/website/src/app/docs/packages/[packageName]/[version]/[item]/page.tsx @@ -1,4 +1,5 @@ import type { Metadata } from 'next'; +import { notFound } from 'next/navigation'; import { DocItem } from '~/components/DocItem'; import { fetchNode } from '~/util/fetchNode'; @@ -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 (
diff --git a/apps/website/src/components/Navigation.tsx b/apps/website/src/components/Navigation.tsx index 687b3898c0e9..88293836cefe 100644 --- a/apps/website/src/components/Navigation.tsx +++ b/apps/website/src/components/Navigation.tsx @@ -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'; @@ -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) => { diff --git a/apps/website/src/util/fetchNode.ts b/apps/website/src/util/fetchNode.ts index bb73d58d6684..d2d4da5c8a34 100644 --- a/apps/website/src/util/fetchNode.ts +++ b/apps/website/src/util/fetchNode.ts @@ -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({ @@ -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(); } diff --git a/apps/website/src/util/fetchSitemap.ts b/apps/website/src/util/fetchSitemap.ts index 9f8023d33302..d1dfe1a0c22e 100644 --- a/apps/website/src/util/fetchSitemap.ts +++ b/apps/website/src/util/fetchSitemap.ts @@ -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({ @@ -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(); }