Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6c4d47

Browse files
bcpeinhardtaslilac
andauthored
fix: add version information to default docs links (#14205)
add version information to default docs links --------- Co-authored-by: Kayla Washburn-Love <[email protected]>
1 parent 2e05329 commit d6c4d47

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

site/src/components/ErrorBoundary/RuntimeErrorState.tsx

+1-15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { CoderIcon } from "components/Icons/CoderIcon";
1010
import { Loader } from "components/Loader/Loader";
1111
import { Margins } from "components/Margins/Margins";
1212
import { Stack } from "components/Stack/Stack";
13+
import { getStaticBuildInfo } from "utils/buildInfo";
1314

1415
const fetchDynamicallyImportedModuleError =
1516
"Failed to fetch dynamically imported module";
@@ -116,21 +117,6 @@ export const RuntimeErrorState: FC<RuntimeErrorStateProps> = ({ error }) => {
116117
);
117118
};
118119

119-
// During the build process, we inject the build info into the HTML
120-
const getStaticBuildInfo = () => {
121-
const buildInfoJson = document
122-
.querySelector("meta[property=build-info]")
123-
?.getAttribute("content");
124-
125-
if (buildInfoJson) {
126-
try {
127-
return JSON.parse(buildInfoJson) as BuildInfoResponse;
128-
} catch {
129-
return undefined;
130-
}
131-
}
132-
};
133-
134120
const styles = {
135121
root: {
136122
paddingTop: 32,

site/src/utils/buildInfo.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { BuildInfoResponse } from "api/typesGenerated";
2+
3+
let CACHED_BUILD_INFO: BuildInfoResponse | undefined;
4+
5+
// During the build process, we inject the build info into the HTML
6+
export const getStaticBuildInfo = () => {
7+
if (CACHED_BUILD_INFO) {
8+
return CACHED_BUILD_INFO;
9+
}
10+
11+
const buildInfoJson = document
12+
.querySelector("meta[property=build-info]")
13+
?.getAttribute("content");
14+
15+
if (buildInfoJson) {
16+
try {
17+
CACHED_BUILD_INFO = JSON.parse(buildInfoJson) as BuildInfoResponse;
18+
} catch {
19+
return undefined;
20+
}
21+
}
22+
23+
return CACHED_BUILD_INFO;
24+
};

site/src/utils/docs.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
const DEFAULT_DOCS_URL = "https://coder.com/docs";
1+
import { getStaticBuildInfo } from "./buildInfo";
2+
3+
function defaultDocsUrl(): string {
4+
const docsUrl = "https://coder.com/docs";
5+
// If we can get the specific version, we want to include that in default docs URL.
6+
let version = getStaticBuildInfo()?.version;
7+
if (!version) {
8+
return docsUrl;
9+
}
10+
11+
// Strip the postfix version info that's not part of the link.
12+
const i = version?.indexOf("-") ?? -1;
13+
if (i >= 0) {
14+
version = version.slice(0, i);
15+
}
16+
return `${docsUrl}/@${version}`;
17+
}
218

319
// Add cache to avoid DOM reading all the time
420
let CACHED_DOCS_URL: string | undefined;
@@ -12,8 +28,9 @@ const getBaseDocsURL = () => {
1228
const docsUrl = document
1329
.querySelector<HTMLMetaElement>('meta[property="docs-url"]')
1430
?.getAttribute("content");
31+
1532
const isValidDocsURL = docsUrl && isURL(docsUrl);
16-
CACHED_DOCS_URL = isValidDocsURL ? docsUrl : DEFAULT_DOCS_URL;
33+
CACHED_DOCS_URL = isValidDocsURL ? docsUrl : defaultDocsUrl();
1734
}
1835
return CACHED_DOCS_URL;
1936
};

0 commit comments

Comments
 (0)