Skip to content

Commit 6821e77

Browse files
authored
Merge pull request #11 from bitrise-io/integrations-handle-proxy-outage
handle when proxy is down
2 parents 7a46b0e + 7d0b305 commit 6821e77

5 files changed

Lines changed: 59 additions & 32 deletions

File tree

src/js/404.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ import { renderStatus } from './shared/status';
2424
window.location.href = `/stacks/subpage?subpage=${subpagePath}`;
2525
}
2626
}
27+
28+
const stepPath = detectTopicFromUrl(url, 'integrations/steps', 'step');
29+
if (stepPath) {
30+
const integrationsProxyAvailable = await fetch('/integrations-proxy')
31+
.then((integrationsProxyResponse) => integrationsProxyResponse.ok)
32+
.catch(() => false);
33+
if (!integrationsProxyAvailable) {
34+
window.location.href = `/integrations/step?step=${stepPath}`;
35+
}
36+
}
2737
})();
2838

2939
window.addEventListener('load', async () => {

src/js/integrations.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const searchField = document.getElementById('search');
1717
searchField.parentNode.action = document.location.pathname; // TODO: move to webflow
1818
searchField.value = queryFilter;
1919

20-
IntegrationsService.loadIntegrations().then((integrations) => {
20+
(async () => {
21+
const integrations = await IntegrationsService.loadIntegrations();
2122
sidebar.render(integrations, platformFilter, categoryFilter, queryFilter);
2223
content.render(integrations, platformFilter, categoryFilter, queryFilter);
2324

@@ -40,7 +41,17 @@ IntegrationsService.loadIntegrations().then((integrations) => {
4041
}, 500);
4142
}
4243

44+
const proxyAvailable = await fetch('/integrations-proxy')
45+
.then((proxyResponse) => proxyResponse.ok)
46+
.catch(() => false);
47+
48+
if (!proxyAvailable) {
49+
document.querySelectorAll('a[href^="/integrations"]').forEach((link) => {
50+
link.href = `/integrations/step?step=${link.getAttribute('href').replace(/^.*\/integrations\/steps\//, '')}`;
51+
});
52+
}
53+
4354
fancyConsoleLog('Bitrise.io Integrations');
44-
});
55+
})();
4556

4657
if (import.meta.webpackHot) import.meta.webpackHot.accept();

src/js/integrations/worker.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
const ORIGIN_HOST = 'bitrise.io';
22

3+
const setCorsHeaders = (response) => {
4+
response.headers.set('Access-Control-Allow-Origin', '*');
5+
response.headers.append('Vary', 'Origin');
6+
return response;
7+
};
8+
39
export default {
410
async fetch(request) {
511
const urlObject = new URL(request.url);
612
const canonical = new URL(request.url);
713

14+
if (urlObject.pathname.match(/^\/integrations-proxy$/)) {
15+
return setCorsHeaders(new Response('OK'));
16+
}
17+
818
urlObject.hostname = ORIGIN_HOST;
919
canonical.hostname = 'bitrise.io';
1020

src/js/stacks.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ const getStacksLink = (path, useFallback = false) => {
9494
const pagePath = detectTopicFromUrl(url, 'stacks', 'subpage').replace(/\/$/, '');
9595
const pageType = pagePath.split('/')[0];
9696

97-
const proxyAvailable = await fetch('/stacks-proxy')
98-
.then((response) => response.ok)
99-
.catch(() => false);
100-
10197
if (pageType === '') {
10298
const stackService = new StacksService();
10399
const stacksLinks = await stackService.fetchStacksIndexJson();
@@ -479,11 +475,17 @@ const getStacksLink = (path, useFallback = false) => {
479475
window.location.href = '/stacks';
480476
}
481477

482-
document.querySelectorAll('a[href^="/stacks"]').forEach((link) => {
483-
if (!proxyAvailable && !link.href.match(/\.xml$/) && !link.href.match(/\/stacks\/subpage/)) {
484-
link.href = getStacksLink(link.getAttribute('href').replace(/^.*\/stacks\//, ''), true);
485-
}
486-
});
478+
const proxyAvailable = await fetch('/stacks-proxy')
479+
.then((response) => response.ok)
480+
.catch(() => false);
481+
482+
if (!proxyAvailable) {
483+
document.querySelectorAll('a[href^="/stacks"]').forEach((link) => {
484+
if (!link.href.match(/\.xml$/) && !link.href.match(/\/stacks\/subpage/)) {
485+
link.href = getStacksLink(link.getAttribute('href').replace(/^.*\/stacks\//, ''), true);
486+
}
487+
});
488+
}
487489

488490
fancyConsoleLog('Bitrise.io Stacks');
489491
})();

src/js/steps.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
import DetailsSection from './integrations/DetailsSection';
22
import HeaderSection from './integrations/HeaderSection';
33
import IntegrationsService from './integrations/IntegrationsService';
4-
import { fancyConsoleLog, setMetaContent } from './shared/common';
4+
import { detectTopicFromUrl, fancyConsoleLog, setMetaContent } from './shared/common';
55

66
import '../css/steps.css';
77

8-
/**
9-
* @param {URL} url
10-
* @returns {?string}
11-
*/
12-
function detectStepFromUrl(url) {
13-
let path = url.pathname;
14-
if (path.match(/steps\/?$/)) {
15-
path = `${path.replace(/\/$/, '')}/${url.searchParams.get('step')}`;
16-
}
17-
18-
const match = path.match(/steps\/(.+)$/);
19-
if (match) {
20-
return match[1];
21-
}
22-
return null;
23-
}
24-
258
const url = new URL(document.location.href);
26-
const stepFilter = detectStepFromUrl(url);
9+
const stepFilter = detectTopicFromUrl(url, 'integrations/steps', 'step');
2710

2811
const header = new HeaderSection();
2912
const details = new DetailsSection();
3013

31-
IntegrationsService.loadIntegrations().then((integrations) => {
14+
(async () => {
15+
const integrations = await IntegrationsService.loadIntegrations();
3216
if (!stepFilter || !(stepFilter in integrations.steps)) {
3317
window.location.href = '/integrations';
3418
} else {
@@ -53,8 +37,18 @@ IntegrationsService.loadIntegrations().then((integrations) => {
5337
header.render(integrations, step);
5438
details.render(integrations, step);
5539

40+
const proxyAvailable = await fetch('/integrations-proxy')
41+
.then((proxyResponse) => proxyResponse.ok)
42+
.catch(() => false);
43+
44+
if (!proxyAvailable) {
45+
document.querySelectorAll('a[href^="/integrations"]').forEach((link) => {
46+
link.href = `/integrations/step?step=${link.getAttribute('href').replace(/^.*\/integrations\/steps\//, '')}`;
47+
});
48+
}
49+
5650
fancyConsoleLog(`Bitrise.io Integrations: ${step.title}`);
5751
}
58-
});
52+
})();
5953

6054
if (import.meta.webpackHot) import.meta.webpackHot.accept();

0 commit comments

Comments
 (0)