-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2dc25f
commit 6e6605b
Showing
44 changed files
with
487 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
"use server"; | ||
|
||
import { CustomerAnalytics } from "@/components/analytics/CustomerAnalytics"; | ||
import Preload, { PreloadHref } from "@/components/preload"; | ||
import { ThemeScript } from "@/components/themes/ThemeScript"; | ||
import { createCachedDocsLoader } from "@/server/docs-loader"; | ||
import { RgbaColor } from "@/server/types"; | ||
import { getDocsDomainApp } from "@/server/xfernhost/app"; | ||
import { DocsV2Read } from "@fern-api/fdr-sdk/client/types"; | ||
import { withDefaultProtocol } from "@fern-api/ui-core-utils"; | ||
import { Toaster } from "@fern-docs/components"; | ||
import { getEdgeFlags } from "@fern-docs/edge-config"; | ||
import { EdgeFlags } from "@fern-docs/utils"; | ||
import { compact, uniqBy } from "es-toolkit/array"; | ||
import { Metadata, Viewport } from "next/types"; | ||
import tinycolor from "tinycolor2"; | ||
import { toImageDescriptor } from "../seo"; | ||
|
||
export default async function Layout({ | ||
children, | ||
params, | ||
}: { | ||
children: React.ReactNode; | ||
params: { | ||
domain: string; | ||
}; | ||
}) { | ||
const docsLoader = await createCachedDocsLoader(params.domain); | ||
const [config, edgeFlags, files, colors] = await Promise.all([ | ||
docsLoader.getConfig(), | ||
getEdgeFlags(params.domain), | ||
docsLoader.getFiles(), | ||
docsLoader.getColors(), | ||
]); | ||
const preloadHrefs = generatePreloadHrefs( | ||
config?.typographyV2, | ||
files, | ||
edgeFlags | ||
); | ||
return ( | ||
<> | ||
{preloadHrefs.map((href) => ( | ||
<Preload key={href.href} href={href.href} options={href.options} /> | ||
))} | ||
<ThemeScript colors={colors} /> | ||
<Toaster /> | ||
{children} | ||
<CustomerAnalytics /> | ||
</> | ||
); | ||
} | ||
|
||
export async function generateViewport(): Promise<Viewport> { | ||
const domain = getDocsDomainApp(); | ||
const docsLoader = await createCachedDocsLoader(domain); | ||
const colors = await docsLoader.getColors(); | ||
const dark = maybeToHex( | ||
colors.dark?.background ?? colors.dark?.accentPrimary | ||
); | ||
const light = maybeToHex( | ||
colors.light?.background ?? colors.light?.accentPrimary | ||
); | ||
return { | ||
themeColor: compact([ | ||
dark ? { color: dark, media: "(prefers-color-scheme: dark)" } : undefined, | ||
light | ||
? { color: light, media: "(prefers-color-scheme: light)" } | ||
: undefined, | ||
]), | ||
}; | ||
} | ||
|
||
function maybeToHex(color: RgbaColor | undefined): string | undefined { | ||
if (color == null) { | ||
return undefined; | ||
} | ||
return tinycolor(color).toHexString(); | ||
} | ||
|
||
export async function generateMetadata({ | ||
params, | ||
}: { | ||
params: { | ||
domain: string; | ||
}; | ||
}): Promise<Metadata> { | ||
const docsLoader = await createCachedDocsLoader(params.domain, params.domain); | ||
const [files, config, baseUrl] = await Promise.all([ | ||
docsLoader.getFiles(), | ||
docsLoader.getConfig(), | ||
docsLoader.getBaseUrl(), | ||
]); | ||
const index = config?.metadata?.noindex ? false : undefined; | ||
const follow = config?.metadata?.nofollow ? false : undefined; | ||
|
||
return { | ||
metadataBase: new URL( | ||
baseUrl.basePath || "/", | ||
withDefaultProtocol(docsLoader.domain) | ||
), | ||
applicationName: config?.title, | ||
title: { | ||
template: config?.title ? "%s | " + config?.title : "%s", | ||
default: config?.title ?? "Documentation", | ||
}, | ||
robots: { index, follow }, | ||
openGraph: { | ||
title: config?.metadata?.["og:title"], | ||
description: config?.metadata?.["og:description"], | ||
locale: config?.metadata?.["og:locale"], | ||
url: config?.metadata?.["og:url"], | ||
siteName: config?.metadata?.["og:site_name"], | ||
images: toImageDescriptor( | ||
files, | ||
config?.metadata?.["og:image"], | ||
config?.metadata?.["og:image:width"], | ||
config?.metadata?.["og:image:height"] | ||
), | ||
}, | ||
twitter: { | ||
site: config?.metadata?.["twitter:site"], | ||
creator: config?.metadata?.["twitter:handle"], | ||
title: config?.metadata?.["twitter:title"], | ||
description: config?.metadata?.["twitter:description"], | ||
images: toImageDescriptor(files, config?.metadata?.["twitter:image"]), | ||
}, | ||
icons: { | ||
icon: config?.favicon | ||
? toImageDescriptor(files, { | ||
type: "fileId", | ||
value: config.favicon, | ||
})?.url | ||
: undefined, | ||
}, | ||
}; | ||
} | ||
|
||
function generatePreloadHrefs( | ||
typography: DocsV2Read.LoadDocsForUrlResponse["definition"]["config"]["typographyV2"], | ||
files: Record<string, { src: string }>, | ||
edgeFlags: EdgeFlags | ||
): PreloadHref[] { | ||
const toReturn: PreloadHref[] = []; | ||
|
||
const fontVariants = compact([ | ||
typography?.bodyFont?.variants, | ||
typography?.headingsFont?.variants, | ||
typography?.codeFont?.variants, | ||
]).flat(); | ||
|
||
fontVariants.forEach((variant) => { | ||
try { | ||
const file = files[variant.fontFile]; | ||
if (file != null) { | ||
toReturn.push({ | ||
href: file.src, | ||
options: { | ||
as: "font", | ||
crossOrigin: "anonymous", | ||
type: `font/${getFontExtension(file.src)}`, | ||
}, | ||
}); | ||
} | ||
} catch {} | ||
}); | ||
|
||
if (edgeFlags.isApiPlaygroundEnabled) { | ||
toReturn.push({ | ||
href: "/api/fern-docs/auth/api-key-injection", | ||
options: { as: "fetch" }, | ||
}); | ||
} | ||
|
||
toReturn.push({ | ||
href: edgeFlags.isSearchV2Enabled | ||
? "/api/fern-docs/search/v2/key" | ||
: "/api/fern-docs/search/v1/key", | ||
options: { as: "fetch" }, | ||
}); | ||
|
||
return uniqBy(toReturn, (href) => href.href); | ||
} | ||
|
||
function getFontExtension(url: string): string { | ||
const ext = new URL(url).pathname.split(".").pop(); | ||
if (ext == null) { | ||
throw new Error("No extension found for font: " + url); | ||
} | ||
return ext; | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/fern-docs/bundle/src/app/[domain]/~dynamic/[[...slug]]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use server"; | ||
|
||
import { cookies } from "next/headers"; | ||
import { Metadata } from "next/types"; | ||
import { DocsPageComponent, generateDocsPageMetadata } from "../../docs-page"; | ||
|
||
export default async function Page({ | ||
params, | ||
}: { | ||
params: { slug?: string[]; domain: string }; | ||
}) { | ||
const fern_token = cookies().get("fern_token")?.value; | ||
return <DocsPageComponent params={params} fern_token={fern_token} />; | ||
} | ||
|
||
export async function generateMetadata({ | ||
params, | ||
}: { | ||
params: { slug?: string[]; domain: string }; | ||
}): Promise<Metadata> { | ||
const fern_token = cookies().get("fern_token")?.value; | ||
return generateDocsPageMetadata({ params, fern_token }); | ||
} |
Oops, something went wrong.