Skip to content

Commit 4e4359b

Browse files
committed
perf(web): Open Source Licenses downloads its manifest once per session
1 parent a6ec88f commit 4e4359b

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

‎apps/web/src/components/settings/OpenSourceLicenses.tsx‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,35 @@ type LicenseManifestState =
2121
| { readonly status: "error"; readonly message: string }
2222
| { readonly status: "ready"; readonly manifest: ThirdPartyLicenseManifest };
2323

24-
async function loadLicenseManifest(signal: AbortSignal): Promise<ThirdPartyLicenseManifest> {
25-
const response = await fetch(
26-
`${import.meta.env.BASE_URL.replace(/\/$/, "")}/third-party-licenses.json`,
27-
{ signal },
28-
);
29-
if (!response.ok) {
30-
throw new Error(`The license manifest request failed with status ${String(response.status)}.`);
24+
let licenseManifestPromise: Promise<ThirdPartyLicenseManifest> | undefined;
25+
26+
async function fetchLicenseManifest(): Promise<ThirdPartyLicenseManifest> {
27+
const signal = AbortSignal.timeout(30_000);
28+
try {
29+
const response = await fetch(
30+
`${import.meta.env.BASE_URL.replace(/\/$/, "")}/third-party-licenses.json`,
31+
{ signal },
32+
);
33+
if (!response.ok) {
34+
throw new Error(
35+
`The license manifest request failed with status ${String(response.status)}.`,
36+
);
37+
}
38+
return decodeThirdPartyLicenseManifest((await response.json()) as unknown);
39+
} catch (error) {
40+
if (signal.aborted) {
41+
throw new Error("The license manifest took too long to load.", { cause: error });
42+
}
43+
throw error;
3144
}
32-
return decodeThirdPartyLicenseManifest((await response.json()) as unknown);
45+
}
46+
47+
function loadLicenseManifest(): Promise<ThirdPartyLicenseManifest> {
48+
licenseManifestPromise ??= fetchLicenseManifest().catch((error: unknown) => {
49+
licenseManifestPromise = undefined;
50+
throw error;
51+
});
52+
return licenseManifestPromise;
3353
}
3454

3555
function LicenseNoticeRow({
@@ -198,19 +218,23 @@ export function OpenSourceLicensesPanel() {
198218
const [requestVersion, setRequestVersion] = useState(0);
199219

200220
useEffect(() => {
201-
const controller = new AbortController();
221+
let cancelled = false;
202222
setState({ status: "loading" });
203-
void loadLicenseManifest(controller.signal).then(
204-
(manifest) => setState({ status: "ready", manifest }),
223+
void loadLicenseManifest().then(
224+
(manifest) => {
225+
if (!cancelled) setState({ status: "ready", manifest });
226+
},
205227
(error: unknown) => {
206-
if (controller.signal.aborted) return;
228+
if (cancelled) return;
207229
setState({
208230
status: "error",
209231
message: error instanceof Error ? error.message : "The license manifest could not load.",
210232
});
211233
},
212234
);
213-
return () => controller.abort();
235+
return () => {
236+
cancelled = true;
237+
};
214238
}, [requestVersion]);
215239

216240
const entries = state.status === "ready" ? state.manifest.entries : [];

0 commit comments

Comments
 (0)