Skip to content

Commit 81c10fc

Browse files
committed
Don't fail docs page when release registry is not reachable
(For example because you don't have wifi and work on docs locally)
1 parent 22213f3 commit 81c10fc

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/components/PreCodeBlock.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {CopyButton} from '@/components/CopyButton';
2-
import React, {DetailedHTMLProps, HTMLAttributes, Children} from 'react';
2+
import React, {DetailedHTMLProps, HTMLAttributes} from 'react';
3+
import config from '../../docs.config';
34

45
interface PreProps
56
extends DetailedHTMLProps<HTMLAttributes<HTMLPreElement>, HTMLPreElement> {
@@ -24,19 +25,23 @@ async function getLatestVersion() {
2425
'https://releaseregistry.sourcegraph.com/v1/releases/sourcegraph/latest'
2526
);
2627

27-
const response = await fetch(url.toString(), {
28-
method: 'GET',
29-
headers: {
30-
Accept: 'application/json'
28+
try {
29+
const response = await fetch(url.toString(), {
30+
method: 'GET',
31+
headers: {
32+
Accept: 'application/json'
33+
}
34+
});
35+
36+
if (!response.ok) {
37+
throw new Error(`HTTP error! status: ${response.status}`);
3138
}
32-
});
3339

34-
if (!response.ok) {
35-
throw new Error(`HTTP error! status: ${response.status}`);
40+
const releaseInfo: ReleaseInfo = await response.json();
41+
return releaseInfo.version;
42+
} catch (error) {
43+
return config.DOCS_LATEST_VERSION;
3644
}
37-
38-
const releaseInfo: ReleaseInfo = await response.json();
39-
return releaseInfo.version;
4045
}
4146

4247
function trimPrefix(str: string, prefix: string) {

0 commit comments

Comments
 (0)