From 6909c644cc431a565626b7237a210b353780c57b Mon Sep 17 00:00:00 2001 From: Nixinova Date: Wed, 28 Jun 2023 17:43:11 +1200 Subject: [PATCH] Fallback to packaged data files if fetch fails Resolves #21 --- changelog.md | 3 ++- src/helpers/load-data.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 7935af9..fe0086d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,8 @@ # Changelog ## Next -- Fixed cached data files having formatting issues. +- Changed fetching of data files to fallback to using the packaged files if the fetch request fails. +- Fixed packaged data files having formatting issues. ## 2.5.5 *2023-06-25* diff --git a/src/helpers/load-data.ts b/src/helpers/load-data.ts index 139c905..ecfab4c 100644 --- a/src/helpers/load-data.ts +++ b/src/helpers/load-data.ts @@ -11,7 +11,8 @@ async function loadWebFile(file: string): Promise { if (cachedContent) return cachedContent; // Otherwise cache the request const dataUrl = (file: string): string => `https://raw.githubusercontent.com/github/linguist/HEAD/lib/linguist/${file}`; - const fileContent = await fetch(dataUrl(file)).then(data => data.text()); + // Load file content, falling back to the local file if the request fails + const fileContent = await fetch(dataUrl(file)).then(data => data.text()).catch(async () => await loadLocalFile(file)); cache.set(file, fileContent); return fileContent; }