From e1e657d73bb9804d61c8ddfdb93715accab51160 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Tue, 26 Mar 2024 11:11:25 -0400 Subject: [PATCH] fix: ssg for FernLink --- packages/ui/app/src/components/FernLink.tsx | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/ui/app/src/components/FernLink.tsx b/packages/ui/app/src/components/FernLink.tsx index bc81752ec9..dbc842da50 100644 --- a/packages/ui/app/src/components/FernLink.tsx +++ b/packages/ui/app/src/components/FernLink.tsx @@ -1,8 +1,8 @@ import { ExternalLinkIcon } from "@radix-ui/react-icons"; import Link from "next/link"; -import { useRouter } from "next/router"; import { ReactElement, type ComponentProps } from "react"; import { format, parse, resolve, type UrlObject } from "url"; +import { useNavigationContext } from "../contexts/navigation-context"; interface FernLinkProps extends ComponentProps { showExternalLinkIcon?: boolean; @@ -28,17 +28,9 @@ export function FernLink({ showExternalLinkIcon, ...props }: FernLinkProps): Rea } function FernRelativeLink(props: ComponentProps) { - const router = useRouter(); - // SSG will render the route as /static/x.docs.buildwithfern.com/slug which is incorrect. - // We will delay the rendering of the link until the router is ready to ensure the correct pathname is used. - if (router.isReady) { - const href = resolveRelativeUrl(router.asPath, formatUrlString(props.href)); - return ; - } else { - // TODO: in order for SEO to work correctly, we need to render this link on the server side - // which will require hacking the `Link` internal implementation - return {props.children}; - } + const { selectedSlug } = useNavigationContext(); + const href = resolveRelativeUrl(`/${selectedSlug}`, formatUrlString(props.href)); + return ; } export function toUrlObject(url: string | UrlObject): UrlObject {