Skip to content

Commit 1879a96

Browse files
perf: parallelize manifest+github README, edge-cache pages
1 parent ae9e014 commit 1879a96

4 files changed

Lines changed: 53 additions & 18 deletions

File tree

‎src/lib/data-api.ts‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ export function findReadmeEntry(manifest: Manifest): Manifest[number] | null {
123123
* Best-effort fetch of the README from the dataset's GitHub repo, used when
124124
* the data.nemar.org manifest doesn't list one (git-annex manifests typically
125125
* carry annexed content only, not git-tracked files like README.md at root).
126-
* Tries HEAD, then main, then master, with a few common filename variants.
127-
* Returns null on any failure. SSR-only — no CORS concerns.
126+
* Races HEAD/main/master across README.md/README/README.txt/readme.md in
127+
* parallel; first 200 wins. Returns null on any failure. SSR-only — no CORS.
128128
*/
129129
export async function fetchGithubReadme(
130130
githubUrl: string,
@@ -138,23 +138,27 @@ export async function fetchGithubReadme(
138138
const filenames = ["README.md", "README", "README.txt", "readme.md"];
139139

140140
const controller = new AbortController();
141-
const timeout = init.timeoutMs ?? 3_000;
141+
const timeout = init.timeoutMs ?? 1_500;
142142
const timer = setTimeout(() => controller.abort(), timeout);
143143
const onParentAbort = () => controller.abort();
144144
if (init.signal) init.signal.addEventListener("abort", onParentAbort);
145145

146-
try {
147-
for (const branch of branches) {
148-
for (const filename of filenames) {
149-
const url = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${filename}`;
150-
try {
151-
const res = await fetch(url, { signal: controller.signal });
152-
if (res.ok) return await res.text();
153-
} catch {
154-
/* try next combination */
155-
}
156-
}
146+
const urls: string[] = [];
147+
for (const branch of branches) {
148+
for (const filename of filenames) {
149+
urls.push(`https://raw.githubusercontent.com/${owner}/${repo}/${branch}/${filename}`);
157150
}
151+
}
152+
153+
try {
154+
return await Promise.any(
155+
urls.map(async (url) => {
156+
const res = await fetch(url, { signal: controller.signal });
157+
if (!res.ok) throw new Error(`${url}: ${res.status}`);
158+
return res.text();
159+
}),
160+
);
161+
} catch {
158162
return null;
159163
} finally {
160164
clearTimeout(timer);

‎src/pages/dataset/[id].astro‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,17 @@ const selectedVersion =
8686
? versionParam
8787
: landing.latest ?? landing.versions[0]?.version ?? null;
8888
89-
const manifest = selectedVersion ? await getManifest(id, selectedVersion) : null;
89+
// Race manifest fetch and GitHub README fetch in parallel. Manifest may 404
90+
// (e.g. nm000104 v1.0.0 right now), in which case we fall through to GH; if
91+
// it succeeds and lists a README, we prefer that and discard the GH work.
92+
// Cost: one extra GH raw round-trip per page when manifest README is present,
93+
// but cuts ~500ms off the critical path when manifest is missing.
94+
const manifestPromise = selectedVersion ? getManifest(id, selectedVersion) : Promise.resolve(null);
95+
const githubReadmePromise = metadata.external_links.github_url
96+
? fetchGithubReadme(metadata.external_links.github_url, { timeoutMs: 1_500 })
97+
: Promise.resolve(null);
98+
99+
const manifest = await manifestPromise;
90100
91101
// README resolution order:
92102
// 1. data.nemar.org manifest README* (preferred; version-locked to <ver>)
@@ -102,8 +112,8 @@ if (manifest) {
102112
if (readmeSource) readmeSourceKind = "manifest";
103113
}
104114
}
105-
if (!readmeSource && metadata.external_links.github_url) {
106-
readmeSource = await fetchGithubReadme(metadata.external_links.github_url, { timeoutMs: 3_000 });
115+
if (!readmeSource) {
116+
readmeSource = await githubReadmePromise;
107117
if (readmeSource) readmeSourceKind = "github";
108118
}
109119
if (!readmeSource && metadata.description && metadata.description !== metadata.name) {
@@ -128,7 +138,13 @@ const basePath = selectedVersion
128138
? `https://data.nemar.org/${encodeURIComponent(id)}/${encodeURIComponent(selectedVersion)}`
129139
: "";
130140
131-
Astro.response.headers.set("Cache-Control", "public, max-age=60");
141+
// Edge-cache aggressively: dataset detail rarely changes within a window. Cloudflare
142+
// honors s-maxage at the edge; max-age stays short so browsers re-validate on nav.
143+
// stale-while-revalidate keeps the edge serving last-good response while it refreshes.
144+
Astro.response.headers.set(
145+
"Cache-Control",
146+
"public, max-age=60, s-maxage=300, stale-while-revalidate=86400",
147+
);
132148
---
133149

134150
<Base title={metadata.name} description={metadata.description ?? `${id} dataset on NEMAR`}>

‎src/pages/discover.astro‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ const pageCount = Math.max(1, Math.ceil(totalReturned / PAGE_SIZE));
4848
// Re-serialize the canonical filter state to drop unknown params.
4949
const canonicalParams = filterStateToURL(state);
5050
canonicalParams.delete("page");
51+
52+
// Discover pages cache at the edge keyed on the full URL (query params),
53+
// so each filter+page combo gets its own cache entry. New datasets appear
54+
// when the cache rolls (2 min) or revalidates in the background.
55+
Astro.response.headers.set(
56+
"Cache-Control",
57+
"public, max-age=30, s-maxage=120, stale-while-revalidate=3600",
58+
);
5159
---
5260

5361
<Base title="Discover datasets">

‎src/pages/index.astro‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ try {
2424
totalParticipants = 29_344;
2525
totalSize = 65 * 1024 ** 4;
2626
}
27+
28+
// Stats refresh slowly. Edge-cache for 10 min; serve stale up to a day while
29+
// revalidating so new visitors always hit cache.
30+
Astro.response.headers.set(
31+
"Cache-Control",
32+
"public, max-age=60, s-maxage=600, stale-while-revalidate=86400",
33+
);
2734
---
2835

2936
<Base title="NEMAR">

0 commit comments

Comments
 (0)