perf(anonymize): show the README preview without waiting on the branch list

The preview rendered only after repo details, the full branch list and the
README loaded one after another. The early README fetch was usually
discarded and fetched again, the README endpoint listed every branch from
GitHub first, and preview calls were slowed down after 50 API requests.

Render as soon as the README and details are in, keep a default-branch
README once that branch is selected, skip identical preview requests,
fetch the README by branch name on a cache miss, and drop the slowdown on
the CPU-only preview endpoint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 14:31:37 +00:00
1 parent 31a4eb015f
commit d161d89cee
5 files changed
+136 -100

No files matched your search

+30 -33
View File
@@ -161,45 +161,42 @@ export class GitHubRepository {
throw new AnonymousError("repo_not_found", { httpStatus: 404 });
}
this._data.branches = await this.branches(opt);
model.branches = this._data.branches;
// Serve the cached README without touching GitHub. Otherwise fetch it by
// branch name: listing every branch first (one GitHub page per 100
// branches) only to look up the head commit made the anonymize form's
// preview wait on the whole branch list.
const selected = (model.branches || []).filter((f) => f.name == opt.branch)[0];
if (selected?.readme && opt?.force !== true) return selected.readme;
const selected = model.branches.filter((f) => f.name == opt.branch)[0];
if (selected && (!selected.readme || opt?.force === true)) {
// get the list of repo from github
const oct = octokit(opt.accessToken);
try {
const ghRes = await oct.repos.getReadme({
owner: this.owner,
repo: this.repo,
ref: selected?.commit,
});
const readme = Buffer.from(
ghRes.data.content,
ghRes.data.encoding as BufferEncoding
).toString("utf-8");
selected.readme = readme;
await RepositoryModel.updateOne(
{ externalId: this.id, "branches.name": opt.branch },
{ $set: { "branches.$.readme": readme } }
).exec();
} catch (error) {
throw new AnonymousError("readme_not_available", {
httpStatus: 404,
cause: error as Error,
object: this,
});
}
}
if (!selected) {
const oct = octokit(opt.accessToken);
let readme: string;
try {
const ghRes = await oct.repos.getReadme({
owner: this.owner,
repo: this.repo,
ref: opt.force === true || !selected?.commit ? opt.branch : selected.commit,
});
readme = Buffer.from(
ghRes.data.content,
ghRes.data.encoding as BufferEncoding
).toString("utf-8");
} catch (error) {
throw new AnonymousError("readme_not_available", {
httpStatus: 404,
cause: error as Error,
object: this,
});
}
return selected.readme;
// Cache it on the branch entry when the branch list is already stored;
// the branches endpoint keeps cached READMEs when it refreshes the list.
if (selected) {
selected.readme = readme;
await RepositoryModel.updateOne(
{ externalId: this.id, "branches.name": opt.branch },
{ $set: { "branches.$.readme": readme } }
).exec();
}
return readme;
}
public get owner(): string {
+4 -1
View File
@@ -270,7 +270,10 @@ export default async function start() {
apiRouter.use("/pr", speedLimiter, router.pullRequestPrivate);
apiRouter.use("/gist", speedLimiter, router.gistPublic);
apiRouter.use("/gist", speedLimiter, router.gistPrivate);
apiRouter.use("/anonymize-preview", speedLimiter, router.anonymizePreview);
// The form previews on every edit, and slowing it down after 50 calls made
// typing feel broken. It only runs the anonymizer on posted text, never
// GitHub, so the shared API rate limit is enough.
apiRouter.use("/anonymize-preview", router.anonymizePreview);
// Cache message.txt presence so /api/message doesn't hit the filesystem
// synchronously on every request. Re-checked on a 60s interval — the file