Skip to content

Commit

Permalink
fix(services): fix CORS error in firefox when calling jsdelivr API
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Aug 18, 2024
1 parent 52d9254 commit d00c7e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"Modes",
"Import-maps",
"Formatter",
"Build"
"Build",
"services"
]
}
5 changes: 4 additions & 1 deletion src/livecodes/services/pkgInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable import/no-internal-modules */
import type { APIError, CDNService, PkgInfo } from '../models';
import { isFirefox } from '../utils/utils';
import { removeCDNPrefix, removeSpecifier } from './utils';

// see: https://github.com/jsdelivr/www.jsdelivr.com/blob/master/src/public/js/utils/search.js
Expand All @@ -18,7 +20,8 @@ const attributesToRetrieve = ['name', 'description', 'homepage', 'repository.url
const apiEndpoint = 'https://data.jsdelivr.com/v1';

const jsDelivrHeaders = {
'User-Agent': 'https://livecodes.io',
// https://github.com/live-codes/livecodes/issues/628
...(isFirefox() ? {} : { 'User-Agent': 'https://livecodes.io' }),
};

interface APIPkgFiles {
Expand Down
5 changes: 5 additions & 0 deletions src/livecodes/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const isMobile = /* @__PURE__ */ () => {
return mobile;
};

export const isFirefox = /* @__PURE__ */ () => {
const userAgent = navigator.userAgent.toLowerCase();
return userAgent.includes('firefox') || userAgent.includes('fxios');
};

export const isRelativeUrl = /* @__PURE__ */ (url?: string) =>
!url?.startsWith('http') && !url?.startsWith('data:');

Expand Down

0 comments on commit d00c7e5

Please sign in to comment.