Skip to content

fix: add version information to default docs links #14205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions site/src/components/ErrorBoundary/RuntimeErrorState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -116,21 +117,6 @@ export const RuntimeErrorState: FC<RuntimeErrorStateProps> = ({ 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,
Expand Down
24 changes: 24 additions & 0 deletions site/src/utils/buildInfo.ts
Original file line number Diff line number Diff line change
@@ -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;
};
21 changes: 19 additions & 2 deletions site/src/utils/docs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
const DEFAULT_DOCS_URL = "https://coder.com/docs";
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.
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 (i >= 0) {
version = version.slice(0, i);
}
return `${docsUrl}/@${version}`;
}

// Add cache to avoid DOM reading all the time
let CACHED_DOCS_URL: string | undefined;
Expand All @@ -12,8 +28,9 @@ const getBaseDocsURL = () => {
const docsUrl = document
.querySelector<HTMLMetaElement>('meta[property="docs-url"]')
?.getAttribute("content");

const isValidDocsURL = docsUrl && isURL(docsUrl);
CACHED_DOCS_URL = isValidDocsURL ? docsUrl : DEFAULT_DOCS_URL;
CACHED_DOCS_URL = isValidDocsURL ? docsUrl : defaultDocsUrl();
}
return CACHED_DOCS_URL;
};
Expand Down
Loading