From 62e65732963f341f94b7c08575308ef1ce8c0a06 Mon Sep 17 00:00:00 2001 From: iCrawl Date: Sat, 11 Nov 2023 21:05:25 +0100 Subject: [PATCH] fix(website): caching and revalidation --- apps/website/src/app/docAPI.ts | 48 ++++++++++++------- .../[package]/[version]/[item]/page.tsx | 2 + .../packages/[package]/[version]/layout.tsx | 2 + .../src/app/docs/packages/[package]/page.tsx | 2 + 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/apps/website/src/app/docAPI.ts b/apps/website/src/app/docAPI.ts index 68a0504e81e6..50818b0dddaa 100644 --- a/apps/website/src/app/docAPI.ts +++ b/apps/website/src/app/docAPI.ts @@ -17,43 +17,55 @@ export const fetchVersions = cache(async (packageName: string): Promise row.version).slice(0, N_RECENT_VERSIONS); + // @ts-expect-error: https://github.com/planetscale/database-js/issues/71 + return rows.map((row) => row.version).slice(0, N_RECENT_VERSIONS); + } catch { + return []; + } }); export const fetchModelJSON = cache(async (packageName: string, version: string) => { if (process.env.NEXT_PUBLIC_LOCAL_DEV) { - let res; - try { - res = await readFile(join(process.cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'), 'utf8'); + const res = await readFile( + join(process.cwd(), '..', '..', 'packages', packageName, 'docs', 'docs.api.json'), + 'utf8', + ); return JSON.parse(res); } catch { - console.log(res); return null; } } if (process.env.NEXT_PUBLIC_VERCEL_ENV === 'preview') { + try { + const { rows } = await sql.execute('select data from documentation where name = ? and version = ?', [ + packageName, + 'main', + ]); + + // @ts-expect-error: https://github.com/planetscale/database-js/issues/71 + return rows[0]?.data ?? null; + } catch { + return null; + } + } + + try { const { rows } = await sql.execute('select data from documentation where name = ? and version = ?', [ packageName, - 'main', + version, ]); // @ts-expect-error: https://github.com/planetscale/database-js/issues/71 return rows[0]?.data ?? null; + } catch { + return null; } - - const { rows } = await sql.execute('select data from documentation where name = ? and version = ?', [ - packageName, - version, - ]); - - // @ts-expect-error: https://github.com/planetscale/database-js/issues/71 - return rows[0]?.data ?? null; }); diff --git a/apps/website/src/app/docs/packages/[package]/[version]/[item]/page.tsx b/apps/website/src/app/docs/packages/[package]/[version]/[item]/page.tsx index fb9f1d07ccc2..ee134334aab3 100644 --- a/apps/website/src/app/docs/packages/[package]/[version]/[item]/page.tsx +++ b/apps/website/src/app/docs/packages/[package]/[version]/[item]/page.tsx @@ -29,6 +29,8 @@ import { OVERLOAD_SEPARATOR } from '~/util/constants'; import { fetchMember } from '~/util/fetchMember'; import { findMember } from '~/util/model'; +export const revalidate = 3_600; + export interface ItemRouteParams { item: string; package: string; diff --git a/apps/website/src/app/docs/packages/[package]/[version]/layout.tsx b/apps/website/src/app/docs/packages/[package]/[version]/layout.tsx index f5452907784f..0d5a78912f06 100644 --- a/apps/website/src/app/docs/packages/[package]/[version]/layout.tsx +++ b/apps/website/src/app/docs/packages/[package]/[version]/layout.tsx @@ -12,6 +12,8 @@ import { addPackageToModel } from '~/util/addPackageToModel'; import { PACKAGES } from '~/util/constants'; import { Providers } from './providers'; +export const revalidate = 3_600; + const Header = dynamic(async () => import('~/components/Header')); const Footer = dynamic(async () => import('~/components/Footer')); diff --git a/apps/website/src/app/docs/packages/[package]/page.tsx b/apps/website/src/app/docs/packages/[package]/page.tsx index fda12935605d..2752c3dacfd3 100644 --- a/apps/website/src/app/docs/packages/[package]/page.tsx +++ b/apps/website/src/app/docs/packages/[package]/page.tsx @@ -7,6 +7,8 @@ import { fetchVersions } from '~/app/docAPI'; import { buttonVariants } from '~/styles/Button'; import { PACKAGES } from '~/util/constants'; +export const revalidate = 3_600; + export default async function Page({ params }: { params: { package: string } }) { if (!PACKAGES.includes(params.package)) { notFound();