Skip to content

Commit 7a46b0e

Browse files
authored
Merge pull request #10 from bitrise-io/stacks-handle-proxy-outage
stacks pages can handle proxy outage
2 parents 7f46a9f + 3ceba67 commit 7a46b0e

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

src/js/404.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import '../css/404.css';
22
import '../css/status.css';
3-
import ChangelogService from './changelog/ChangelogService';
43
import { detectTopicFromUrl } from './shared/common';
54
import { renderStatus } from './shared/status';
65

76
(async () => {
87
const url = new URL(document.location.href);
98
const topicSlugId = detectTopicFromUrl(url, 'changelog', 'topic');
109
if (topicSlugId) {
11-
const apiBase = document.location.hostname.match(/(localhost|127\.0\.0\.1)/) ? '' : 'https://bitrise.io';
12-
const changelogService = new ChangelogService(apiBase, 'https://app.bitrise.io');
13-
const currentApiBase = await changelogService.getApiBase();
14-
if (currentApiBase === changelogService.fallbackApiBase) {
10+
const changelogProxyAvailable = await fetch('/changelog-proxy')
11+
.then((changelogProxyResponse) => changelogProxyResponse.ok)
12+
.catch(() => false);
13+
if (!changelogProxyAvailable) {
1514
window.location.href = `/changelog/topic?topic=${topicSlugId}`;
1615
}
1716
}
17+
18+
const subpagePath = detectTopicFromUrl(url, 'stacks', 'subpage');
19+
if (subpagePath) {
20+
const stacksProxyAvailable = await fetch('/stacks-proxy')
21+
.then((stacksProxyResponse) => stacksProxyResponse.ok)
22+
.catch(() => false);
23+
if (!stacksProxyAvailable) {
24+
window.location.href = `/stacks/subpage?subpage=${subpagePath}`;
25+
}
26+
}
1827
})();
1928

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

src/js/stacks.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,22 @@ const getStackRemovalDate = async (pagePath) => {
8282
return stackReport?.stack_meta?.removal_date ? formatDate(new Date(stackReport.stack_meta.removal_date)) : null;
8383
};
8484

85+
const getStacksLink = (path, useFallback = false) => {
86+
if (useFallback) {
87+
return `/stacks/subpage?subpage=${path.replace(/^\//, '')}`;
88+
}
89+
return `/stacks${path}`;
90+
};
91+
8592
(async () => {
8693
const url = new URL(document.location.href);
87-
const pagePath = detectTopicFromUrl(url, 'stacks').replace(/\/$/, '');
94+
const pagePath = detectTopicFromUrl(url, 'stacks', 'subpage').replace(/\/$/, '');
8895
const pageType = pagePath.split('/')[0];
8996

97+
const proxyAvailable = await fetch('/stacks-proxy')
98+
.then((response) => response.ok)
99+
.catch(() => false);
100+
90101
if (pageType === '') {
91102
const stackService = new StacksService();
92103
const stacksLinks = await stackService.fetchStacksIndexJson();
@@ -468,6 +479,12 @@ const getStackRemovalDate = async (pagePath) => {
468479
window.location.href = '/stacks';
469480
}
470481

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+
});
487+
471488
fancyConsoleLog('Bitrise.io Stacks');
472489
})();
473490

src/js/stacks/worker.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
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

13+
if (urlObject.pathname.match(/^\/stacks-proxy$/)) {
14+
return setCorsHeaders(new Response('OK'));
15+
}
16+
717
urlObject.hostname = ORIGIN_HOST;
818

919
const rssMatch = urlObject.pathname.match(/^\/stacks\/(.*index\.xml)/);
@@ -20,7 +30,7 @@ export default {
2030
});
2131
}
2232

23-
if (urlObject.pathname.match(/^\/stacks\/.+\/.+/)) {
33+
if (urlObject.pathname.match(/^\/stacks\/.+/)) {
2434
const originalPath = urlObject.pathname;
2535
urlObject.pathname = '/stacks/subpage';
2636
const response = await fetch(urlObject);

0 commit comments

Comments
 (0)