Skip to content

Commit

Permalink
fix: add canonical url for the default version (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Jul 26, 2024
1 parent ce121f2 commit 2070cfe
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 84 deletions.
23 changes: 0 additions & 23 deletions packages/commons/fdr-utils/src/getUnversionedSlug.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/fdr-sdk/src/navigation/NodeCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class NodeCollector {
traverseNavigation(rootNode, (node, _index, parents) => {
this.idToNode.set(node.id, node);

// if the node is the default version, make a copy of it and "prune" the version slug from all children nodes
const parent = parents[parents.length - 1];
if (
node.type === "version" &&
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { FernNavigation } from "../../generated";
import { toDefaultSlug } from "../pruneVersionNode";

describe("toDefaultSlug", () => {
it("should return the default slug", () => {
expect(
toDefaultSlug(
FernNavigation.Slug("basepath/version/page"),
FernNavigation.Slug("basepath"),
FernNavigation.Slug("basepath/version"),
),
).toEqual(FernNavigation.Slug("basepath/page"));
});

it("should be noop if the slug doesn't start with the version slug", () => {
expect(
toDefaultSlug(
FernNavigation.Slug("basepath/page"),
FernNavigation.Slug("basepath"),
FernNavigation.Slug("basepath/version"),
),
).toEqual(FernNavigation.Slug("basepath/page"));
});
});
23 changes: 0 additions & 23 deletions packages/fdr-sdk/src/navigation/utils/getUnversionedSlug.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/fdr-sdk/src/navigation/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export * from "./followRedirect";
export * from "./getApiReferenceId";
export * from "./getNoIndexFromFrontmatter";
export * from "./getPageId";
export * from "./getUnversionedSlug";
export { toDefaultSlug } from "./pruneVersionNode";
export * from "./slugjoin";
export * from "./traverseNavigation";
5 changes: 5 additions & 0 deletions packages/fdr-sdk/src/navigation/utils/pruneVersionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export function pruneVersionNode<T extends NavigationNode>(
return node;
}

/**
* All versioned slugs are prefixed with the version slug, but the default version may be accessed without the version prefix.
* toDefaultSlug creates the "unversioned" slug for pages under the default version.
* This should be treated as the cannonical slug for that page.
*/
export function toDefaultSlug(
slug: FernNavigation.Slug,
rootSlug: FernNavigation.Slug,
Expand Down
10 changes: 8 additions & 2 deletions packages/ui/app/src/atoms/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ CURRENT_VERSION_ATOM.debugLabel = "CURRENT_VERSION_ATOM";
export const UNVERSIONED_SLUG_ATOM = atom<string>((get) => {
const slug = get(SLUG_ATOM);
const currentVersion = get(CURRENT_VERSION_ATOM);
const basePath = get(BASEPATH_ATOM);
return FernNavigation.utils.getUnversionedSlug(slug, currentVersion?.slug, basePath);
if (currentVersion == null) {
return slug;
}

// the root slug is the basepath without the leading slash (defaults to ""):
const rootSlug = FernNavigation.utils.slugjoin(get(BASEPATH_ATOM) ?? "");

return FernNavigation.utils.toDefaultSlug(slug, rootSlug, currentVersion.slug);
});
UNVERSIONED_SLUG_ATOM.debugLabel = "UNVERSIONED_SLUG_ATOM";

Expand Down
2 changes: 2 additions & 0 deletions packages/ui/app/src/next-app/utils/__test__/getSeoProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ describe("getSeoProps", () => {
noindex: undefined,
},
parents: [],
currentVersion: undefined,
root: { slug: FernNavigation.Slug("") } as FernNavigation.RootNode,
},
true,
);
Expand Down
14 changes: 13 additions & 1 deletion packages/ui/app/src/next-app/utils/getSeoProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export function getSeoProps(
pages: Record<string, DocsV1Read.PageContent>,
files: Record<string, DocsV1Read.File_>,
apis: Record<string, APIV1Read.ApiDefinition>,
{ node, parents }: Pick<FernNavigation.utils.Node.Found, "node" | "parents">,
{
root,
node,
parents,
currentVersion,
}: Pick<FernNavigation.utils.Node.Found, "node" | "parents" | "currentVersion" | "root">,
isSeoDisabled: boolean,
): NextSeoProps {
const additionalMetaTags: MetaTag[] = [];
Expand All @@ -38,6 +43,13 @@ export function getSeoProps(
breadcrumbList: getBreadcrumbList(domain, pages, parents, node),
};

// if the current version is the default version, the page is duplicated (/v1/page and /page).
// the canonical link should point to `/page`.
if (currentVersion != null && currentVersion.default) {
const canonicalSlug = FernNavigation.utils.toDefaultSlug(node.slug, root.slug, currentVersion.slug);
seo.canonical = `https://${domain}/${canonicalSlug}`;
}

const pageId = FernNavigation.utils.getPageId(node);

let ogMetadata: DocsV1Read.MetadataConfig = metadata ?? {};
Expand Down

0 comments on commit 2070cfe

Please sign in to comment.