fix(website): caching and revalidation

This commit is contained in:
iCrawl
2023-11-11 21:05:25 +01:00
parent cf49f405b0
commit 62e6573296
4 changed files with 36 additions and 18 deletions

View File

@@ -17,43 +17,55 @@ export const fetchVersions = cache(async (packageName: string): Promise<string[]
return ['main'];
}
const { rows } = await sql.execute('select version from documentation where name = ? order by version desc', [
packageName,
]);
try {
const { rows } = await sql.execute('select version from documentation where name = ? order by version desc', [
packageName,
]);
// @ts-expect-error: https://github.com/planetscale/database-js/issues/71
return rows.map((row) => 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;
});