Skip to content

Commit

Permalink
Switch to custom cors proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanhollman committed Feb 24, 2024
1 parent 6856c4f commit 3be7b75
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-static-content-to-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ jobs:
run: npm install
- name: Build
run: npm run build
env:
VITE_CORS_PROXY_ENDPOINT: ${{ secrets.CORS_PROXY_ENDPOINT }}
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload artifact
Expand Down
12 changes: 8 additions & 4 deletions src/utilities/fetch/fetch-with-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export function proxyify(url: string) {
return `https://corsproxy.io/?${encodeURIComponent(url)}`;
export function proxyify(url: string): string {
// Only use the CORS proxy if the endpoint is defined.
if (import.meta.env.VITE_CORS_PROXY_ENDPOINT) {
return `${import.meta.env.VITE_CORS_PROXY_ENDPOINT}${encodeURIComponent(url)}`;
}
return `${url}`;
}

export function fetchWithProxy(url: string) {
return fetch(`https://corsproxy.io/?${encodeURIComponent(url)}`);
export async function fetchWithProxy(url: string): Promise<Response> {
return fetch(proxyify(url));
}
11 changes: 11 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
/**
* Used to configure the CORS proxy endpoint, this is set in the github action during the build/deployment.
*/
readonly VITE_CORS_PROXY_ENDPOINT: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}

0 comments on commit 3be7b75

Please sign in to comment.