From e918d62f99e6d4b561d13a9c8a280f1eb0c2da0c Mon Sep 17 00:00:00 2001 From: Benjamin Peinhardt Date: Wed, 7 Aug 2024 15:31:45 +0000 Subject: [PATCH 1/5] init --- .../ErrorBoundary/RuntimeErrorState.tsx | 16 +------------ site/src/utils/buildInfo.ts | 24 +++++++++++++++++++ site/src/utils/docs.ts | 12 ++++++++-- 3 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 site/src/utils/buildInfo.ts diff --git a/site/src/components/ErrorBoundary/RuntimeErrorState.tsx b/site/src/components/ErrorBoundary/RuntimeErrorState.tsx index b9a3c7e3254db..7466259fa8f46 100644 --- a/site/src/components/ErrorBoundary/RuntimeErrorState.tsx +++ b/site/src/components/ErrorBoundary/RuntimeErrorState.tsx @@ -10,6 +10,7 @@ import { CoderIcon } from "components/Icons/CoderIcon"; import { Loader } from "components/Loader/Loader"; import { Margins } from "components/Margins/Margins"; import { Stack } from "components/Stack/Stack"; +import { getStaticBuildInfo } from "utils/buildInfo"; const fetchDynamicallyImportedModuleError = "Failed to fetch dynamically imported module"; @@ -116,21 +117,6 @@ export const RuntimeErrorState: FC = ({ error }) => { ); }; -// During the build process, we inject the build info into the HTML -const getStaticBuildInfo = () => { - const buildInfoJson = document - .querySelector("meta[property=build-info]") - ?.getAttribute("content"); - - if (buildInfoJson) { - try { - return JSON.parse(buildInfoJson) as BuildInfoResponse; - } catch { - return undefined; - } - } -}; - const styles = { root: { paddingTop: 32, diff --git a/site/src/utils/buildInfo.ts b/site/src/utils/buildInfo.ts new file mode 100644 index 0000000000000..d6da4be1685a5 --- /dev/null +++ b/site/src/utils/buildInfo.ts @@ -0,0 +1,24 @@ +import type { BuildInfoResponse } from "api/typesGenerated"; + +let CACHED_BUILD_INFO: BuildInfoResponse | undefined; + +// During the build process, we inject the build info into the HTML +export const getStaticBuildInfo = () => { + if (CACHED_BUILD_INFO) { + return CACHED_BUILD_INFO; + } + + const buildInfoJson = document + .querySelector("meta[property=build-info]") + ?.getAttribute("content"); + + if (buildInfoJson) { + try { + CACHED_BUILD_INFO = JSON.parse(buildInfoJson) as BuildInfoResponse; + } catch { + return undefined; + } + } + + return CACHED_BUILD_INFO; +}; diff --git a/site/src/utils/docs.ts b/site/src/utils/docs.ts index 1d921d0b2038c..c91ea46fb0e96 100644 --- a/site/src/utils/docs.ts +++ b/site/src/utils/docs.ts @@ -1,3 +1,5 @@ +import { getStaticBuildInfo } from "./buildInfo"; + const DEFAULT_DOCS_URL = "https://coder.com/docs"; // Add cache to avoid DOM reading all the time @@ -12,8 +14,14 @@ const getBaseDocsURL = () => { const docsUrl = document .querySelector('meta[property="docs-url"]') ?.getAttribute("content"); - const isValidDocsURL = docsUrl && isURL(docsUrl); - CACHED_DOCS_URL = isValidDocsURL ? docsUrl : DEFAULT_DOCS_URL; + + CACHED_DOCS_URL = docsUrl && isURL(docsUrl) ? docsUrl : DEFAULT_DOCS_URL; + + // If we can get the specific version, we want to include that in docs links + const version = getStaticBuildInfo()?.version.split("-")[0]; + if (version) { + CACHED_DOCS_URL = `${CACHED_DOCS_URL}/@${version}`; + } } return CACHED_DOCS_URL; }; From 499f4d351d483b50f6527b744ec1256de54fecf5 Mon Sep 17 00:00:00 2001 From: Benjamin Peinhardt Date: Wed, 7 Aug 2024 15:40:11 +0000 Subject: [PATCH 2/5] re-extract isValidDocsUrl for clarity --- site/src/utils/docs.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/site/src/utils/docs.ts b/site/src/utils/docs.ts index c91ea46fb0e96..a4a44471af4b8 100644 --- a/site/src/utils/docs.ts +++ b/site/src/utils/docs.ts @@ -15,7 +15,8 @@ const getBaseDocsURL = () => { .querySelector('meta[property="docs-url"]') ?.getAttribute("content"); - CACHED_DOCS_URL = docsUrl && isURL(docsUrl) ? docsUrl : DEFAULT_DOCS_URL; + const isValidDocsURL = docsUrl && isURL(docsUrl); + CACHED_DOCS_URL = isValidDocsURL ? docsUrl : DEFAULT_DOCS_URL; // If we can get the specific version, we want to include that in docs links const version = getStaticBuildInfo()?.version.split("-")[0]; From d573e102f4aed8e4946b4ff457b86c134d227a4a Mon Sep 17 00:00:00 2001 From: Benjamin Peinhardt Date: Wed, 7 Aug 2024 15:58:33 +0000 Subject: [PATCH 3/5] only add version info to default url --- site/src/utils/docs.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/site/src/utils/docs.ts b/site/src/utils/docs.ts index a4a44471af4b8..3da07da3f0aa6 100644 --- a/site/src/utils/docs.ts +++ b/site/src/utils/docs.ts @@ -1,6 +1,11 @@ import { getStaticBuildInfo } from "./buildInfo"; -const DEFAULT_DOCS_URL = "https://coder.com/docs"; +function defaultDocsUrl(): string { + const docsUrl = "https://coder.com/docs"; + // If we can get the specific version, we want to include that in default docs URL. + const version = getStaticBuildInfo()?.version.split("-")[0]; + return version ? `${docsUrl}/@${version}` : docsUrl; +} // Add cache to avoid DOM reading all the time let CACHED_DOCS_URL: string | undefined; @@ -16,13 +21,7 @@ const getBaseDocsURL = () => { ?.getAttribute("content"); const isValidDocsURL = docsUrl && isURL(docsUrl); - CACHED_DOCS_URL = isValidDocsURL ? docsUrl : DEFAULT_DOCS_URL; - - // If we can get the specific version, we want to include that in docs links - const version = getStaticBuildInfo()?.version.split("-")[0]; - if (version) { - CACHED_DOCS_URL = `${CACHED_DOCS_URL}/@${version}`; - } + CACHED_DOCS_URL = isValidDocsURL ? docsUrl : defaultDocsUrl(); } return CACHED_DOCS_URL; }; From be2a76ccb92f2edbede02e2ffdc18bd5012a056f Mon Sep 17 00:00:00 2001 From: Benjamin Peinhardt <61021968+bcpeinhardt@users.noreply.github.com> Date: Thu, 8 Aug 2024 17:13:35 -0500 Subject: [PATCH 4/5] Update site/src/utils/docs.ts Co-authored-by: Kayla Washburn-Love --- site/src/utils/docs.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/site/src/utils/docs.ts b/site/src/utils/docs.ts index 3da07da3f0aa6..824a1a621b96f 100644 --- a/site/src/utils/docs.ts +++ b/site/src/utils/docs.ts @@ -3,7 +3,11 @@ import { getStaticBuildInfo } from "./buildInfo"; function defaultDocsUrl(): string { const docsUrl = "https://coder.com/docs"; // If we can get the specific version, we want to include that in default docs URL. - const version = getStaticBuildInfo()?.version.split("-")[0]; + let version = getStaticBuildInfo()?.version; + const i = version?.indexOf("-") ?? -1; + if (version.index >= 0) { + version = version.slice(0, i); + } return version ? `${docsUrl}/@${version}` : docsUrl; } From 52d60dc40d935b9e92a72983c16feb3fc61363b5 Mon Sep 17 00:00:00 2001 From: Benjamin Peinhardt Date: Thu, 8 Aug 2024 22:27:13 +0000 Subject: [PATCH 5/5] refactor slightly --- site/src/utils/docs.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/site/src/utils/docs.ts b/site/src/utils/docs.ts index 824a1a621b96f..ce357777634c9 100644 --- a/site/src/utils/docs.ts +++ b/site/src/utils/docs.ts @@ -4,11 +4,16 @@ function defaultDocsUrl(): string { const docsUrl = "https://coder.com/docs"; // If we can get the specific version, we want to include that in default docs URL. let version = getStaticBuildInfo()?.version; + if (!version) { + return docsUrl; + } + + // Strip the postfix version info that's not part of the link. const i = version?.indexOf("-") ?? -1; - if (version.index >= 0) { + if (i >= 0) { version = version.slice(0, i); } - return version ? `${docsUrl}/@${version}` : docsUrl; + return `${docsUrl}/@${version}`; } // Add cache to avoid DOM reading all the time