mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 12:57:46 +00:00
[api] Update /api/examples/list to be static for vc init (#7596)
Previously, this API would download the entire git repository for each request. Instead, we can defer downloading to the `/download` API only and change the list API to be statically generated at build time. This will improve the time to render the results from `vc init`. ## Before - `https://vercel-lu6z5kf4s.vercel.sh/api/examples/list` responds in 6200 ms - `https://vercel-lu6z5kf4s.vercel.sh/api/examples/list-all` responds in 800ms ## After - `https://vercel-ctsxcwzgc.vercel.sh/api/examples/list` responds in 60 ms - `https://vercel-ctsxcwzgc.vercel.sh/api/examples/list-all` responds in 60 ms
This commit is contained in:
46
api/_lib/script/build.ts
Normal file
46
api/_lib/script/build.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import fs from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { getExampleList } from '../examples/example-list';
|
||||
import { mapOldToNew } from '../examples/map-old-to-new';
|
||||
|
||||
const repoRoot = join(__dirname, '..', '..', '..');
|
||||
const pubDir = join(repoRoot, 'public');
|
||||
|
||||
async function main() {
|
||||
console.log(`Building static frontend ${repoRoot}...`);
|
||||
|
||||
await fs.rm(pubDir, { recursive: true, force: true });
|
||||
await fs.mkdir(pubDir);
|
||||
|
||||
const examples = await getExampleList();
|
||||
const pathListAll = join(pubDir, 'list-all.json');
|
||||
await fs.writeFile(pathListAll, JSON.stringify(examples));
|
||||
|
||||
const exampleDirs = await fs.readdir(join(repoRoot, 'examples'), {
|
||||
withFileTypes: true,
|
||||
});
|
||||
|
||||
const existingExamples = exampleDirs
|
||||
.filter(dir => dir.isDirectory())
|
||||
.map(dir => ({
|
||||
name: dir.name,
|
||||
visible: true,
|
||||
suggestions: [],
|
||||
}));
|
||||
|
||||
const oldExamples = Object.keys(mapOldToNew).map(key => ({
|
||||
name: key,
|
||||
visible: false,
|
||||
suggestions: mapOldToNew[key],
|
||||
}));
|
||||
|
||||
const pathList = join(pubDir, 'list.json');
|
||||
await fs.writeFile(
|
||||
pathList,
|
||||
JSON.stringify([...existingExamples, ...oldExamples])
|
||||
);
|
||||
|
||||
console.log('Completed building static frontend.');
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
Reference in New Issue
Block a user