docs: improve references loading (#13761)

* docs: improve references loading

* remove comment

* try remove generate metadata

* improvements and fixes
This commit is contained in:
Shahed Nasser
2025-10-16 14:25:01 +03:00
committed by GitHub
parent 4eb9628514
commit e36c054780
11 changed files with 379 additions and 107 deletions

View File

@@ -1,5 +1,5 @@
# ESSENTIAL FOR DEV
NEXT_PUBLIC_ENV=development
NEXT_PUBLIC_ENV=development # use production when creating a build
NEXT_PUBLIC_BASE_URL=http://localhost:3000
NEXT_PUBLIC_BASE_PATH=/resources

View File

@@ -0,0 +1,119 @@
import { unstable_cache } from "next/cache"
import path from "path"
import fs from "fs/promises"
import mdxOptions from "@/mdx-options.mjs"
import {
typeListLinkFixerPlugin,
localLinksRehypePlugin,
workflowDiagramLinkFixerPlugin,
prerequisitesLinkFixerPlugin,
recmaInjectMdxDataPlugin,
} from "remark-rehype-plugins"
import { serialize } from "next-mdx-remote-client/serialize"
type GetRouteProps = {
params: Promise<{
slug: string[]
}>
}
export async function GET(request: Request, { params }: GetRouteProps) {
const { slug } = await params
const fileData = await loadReferencesFile(slug)
if (!fileData) {
return new Response(
JSON.stringify({
error: {
name: "NotFound",
message: "Reference file not found",
},
}),
{
status: 404,
headers: {
"Content-Type": "application/json",
},
}
)
}
return new Response(JSON.stringify(fileData.serialized), {
status: 200,
headers: {
"Content-Type": "application/json",
},
})
}
const loadReferencesFile = unstable_cache(async (slug: string[]) => {
path.join(process.cwd(), "references")
const monoRepoPath = path.resolve("..", "..", "..")
const pathname = `/references/${slug.join("/")}`
const slugChanges = (await import("@/generated/slug-changes.mjs")).slugChanges
const filesMap = (await import("@/generated/files-map.mjs")).filesMap
const fileDetails =
slugChanges.find((f) => f.newSlug === pathname) ||
filesMap.find((f) => f.pathname === pathname)
if (!fileDetails) {
return undefined
}
const fullPath = path.join(monoRepoPath, fileDetails.filePath)
const fileContent = await fs.readFile(fullPath, "utf-8")
const pluginOptions = {
filePath: fullPath,
basePath: process.cwd(),
}
const serialized = await serialize({
source: fileContent,
options: {
disableImports: true,
mdxOptions: {
development: process.env.NEXT_PUBLIC_ENV === "development",
format: "mdx",
rehypePlugins: [
...mdxOptions.options.rehypePlugins,
[
typeListLinkFixerPlugin,
{
...pluginOptions,
checkLinksType: "md",
},
],
[
workflowDiagramLinkFixerPlugin,
{
...pluginOptions,
checkLinksType: "value",
},
],
[
prerequisitesLinkFixerPlugin,
{
...pluginOptions,
checkLinksType: "value",
},
],
[localLinksRehypePlugin, pluginOptions],
],
remarkPlugins: [...mdxOptions.options.remarkPlugins],
recmaPlugins: [
[
recmaInjectMdxDataPlugin,
{ isRemoteMdx: true, mode: process.env.NODE_ENV },
],
],
},
},
})
return {
serialized,
content: fileContent,
path: fullPath,
}
})
export const dynamic = "force-static"

View File

@@ -1,21 +1,10 @@
import { MDXRemote, MDXRemoteSerializeResult } from "next-mdx-remote/rsc"
import { serialize } from "next-mdx-remote/serialize"
import path from "path"
import { promises as fs } from "fs"
import { notFound } from "next/navigation"
import {
typeListLinkFixerPlugin,
localLinksRehypePlugin,
workflowDiagramLinkFixerPlugin,
prerequisitesLinkFixerPlugin,
recmaInjectMdxDataPlugin,
} from "remark-rehype-plugins"
import MDXComponents from "@/components/MDXComponents"
import mdxOptions from "../../../mdx-options.mjs"
import { filesMap } from "../../../generated/files-map.mjs"
import { Metadata } from "next"
import { cache, Suspense } from "react"
import { Loading } from "docs-ui"
import path from "path"
import fs from "fs/promises"
import { ReferenceMDX } from "../../../components/ReferenceMDX"
import { Metadata } from "next"
import { getFrontMatterFromString } from "docs-utils"
type PageProps = {
params: Promise<{
@@ -27,60 +16,10 @@ export default async function ReferencesPage(props: PageProps) {
const params = await props.params
const { slug } = params
const fileData = await loadFile(slug)
if (!fileData) {
return notFound()
}
const pluginOptions = {
filePath: fileData.path,
basePath: process.cwd(),
}
return (
<Suspense fallback={<Loading />}>
<div className="animate animate-fadeIn">
<MDXRemote
source={fileData.content}
components={MDXComponents}
options={{
mdxOptions: {
rehypePlugins: [
...mdxOptions.options.rehypePlugins,
[
typeListLinkFixerPlugin,
{
...pluginOptions,
checkLinksType: "md",
},
],
[
workflowDiagramLinkFixerPlugin,
{
...pluginOptions,
checkLinksType: "value",
},
],
[
prerequisitesLinkFixerPlugin,
{
...pluginOptions,
checkLinksType: "value",
},
],
[localLinksRehypePlugin, pluginOptions],
],
remarkPlugins: [...mdxOptions.options.remarkPlugins],
recmaPlugins: [
[
recmaInjectMdxDataPlugin,
{ isRemoteMdx: true, mode: process.env.NODE_ENV },
],
],
},
}}
/>
<ReferenceMDX slug={slug} />
</div>
</Suspense>
)
@@ -89,11 +28,10 @@ export default async function ReferencesPage(props: PageProps) {
export async function generateMetadata({
params,
}: PageProps): Promise<Metadata> {
// read route params
const slug = (await params).slug
const metadata: Metadata = {}
const fileData = await loadFile(slug)
const fileData = await loadReferencesFile(slug)
if (!fileData) {
return metadata
@@ -106,28 +44,26 @@ export async function generateMetadata({
}
metadata.title = pageTitleMatch.groups.title
metadata.keywords = (fileData.source.frontmatter?.keywords || []) as string[]
const frontmatter = await getFrontMatterFromString(fileData.content)
metadata.keywords = (frontmatter.keywords || []) as string[]
return metadata
}
const loadFile = cache(
async (
slug: string[]
): Promise<
| {
content: string
source: MDXRemoteSerializeResult
path: string
}
| undefined
> => {
export type LoadedReferenceFile = {
content: string
path: string
}
const loadReferencesFile = cache(
async (slug: string[]): Promise<LoadedReferenceFile | undefined> => {
path.join(process.cwd(), "references")
const monoRepoPath = path.resolve("..", "..", "..")
const pathname = `/references/${slug.join("/")}`
const slugChanges = (await import("../../../generated/slug-changes.mjs"))
const slugChanges = (await import("@/generated/slug-changes.mjs"))
.slugChanges
const filesMap = (await import("@/generated/files-map.mjs")).filesMap
const fileDetails =
slugChanges.find((f) => f.newSlug === pathname) ||
filesMap.find((f) => f.pathname === pathname)
@@ -137,12 +73,9 @@ const loadFile = cache(
const fullPath = path.join(monoRepoPath, fileDetails.filePath)
const fileContent = await fs.readFile(fullPath, "utf-8")
const serialized = await serialize(fileContent, {
parseFrontmatter: true,
})
return {
content: fileContent,
source: serialized,
path: fullPath,
}
}

View File

@@ -0,0 +1,33 @@
"use client"
import { MDXClientLazy, SerializeResult } from "next-mdx-remote-client/csr"
import MDXComponents from "../MDXComponents"
import { Loading, swrFetcher } from "docs-ui"
import useSWR from "swr"
import { config } from "../../config"
import { notFound } from "next/navigation"
type ReferenceMDXProps = {
slug: string[]
}
export const ReferenceMDX = ({ slug }: ReferenceMDXProps) => {
const {
data: serializedResult,
error,
isLoading,
} = useSWR<SerializeResult | { error: { name: string; message: string } }>(
`${config.basePath}/api/references/${slug.join("/")}`,
swrFetcher
)
if (isLoading || !serializedResult) {
return <Loading />
}
if ("error" in serializedResult || error) {
return notFound()
}
return <MDXClientLazy {...serializedResult} components={MDXComponents} />
}

View File

@@ -22,12 +22,14 @@
"clsx": "^2.1.0",
"docs-ui": "*",
"next": "15.3.5",
"next-mdx-remote-client": "2",
"posthog-js": "^1.269.1",
"react": "rc",
"react-dom": "rc",
"rehype-mdx-code-props": "^2.0.0",
"remark-directive": "^3.0.0",
"remark-frontmatter": "^5.0.0"
"remark-frontmatter": "^5.0.0",
"swr": "^2.3.6"
},
"devDependencies": {
"@next/bundle-analyzer": "^15.3.5",

View File

@@ -66,17 +66,28 @@ export const CodeTabs = ({
return codeBlock.props as CodeBlockProps
}
if (
"children" in codeBlock.props &&
typeof codeBlock.props.children === "object" &&
codeBlock.props.children
) {
return getCodeBlockProps(
codeBlock.props.children as React.ReactElement<
unknown,
string | React.JSXElementConstructor<any>
>
)
if ("children" in codeBlock.props) {
if (
typeof codeBlock.props.children === "object" &&
codeBlock.props.children
) {
return getCodeBlockProps(
codeBlock.props.children as React.ReactElement<
unknown,
string | React.JSXElementConstructor<any>
>
)
} else if (typeof codeBlock.props.children === "string") {
const lang = "lang" in codeBlock.props ? codeBlock.props.lang : "ts"
return {
...codeBlock.props,
source: codeBlock.props.children,
className:
"className" in codeBlock.props
? codeBlock.props.className
: `language-${lang}`,
} as CodeBlockProps
}
}
return undefined
@@ -90,7 +101,6 @@ export const CodeTabs = ({
}
const typedChildProps = child.props as CodeTab
if (
!React.isValidElement(child) ||
!typedChildProps.label ||
!typedChildProps.value ||
!React.isValidElement(typedChildProps.children)
@@ -109,11 +119,14 @@ export const CodeTabs = ({
let codeBlockProps = codeBlock.props as CodeBlockProps
const showBadge = !codeBlockProps.title
const originalBadgeLabel = codeBlockProps.badgeLabel
const parsedCodeBlockProps = getCodeBlockProps(codeBlock) || {
source: "",
}
const commonProps = {
badgeLabel: showBadge ? undefined : originalBadgeLabel,
hasTabs: true,
className: clsx("!my-0", codeBlockProps.className),
className: clsx("!my-0", parsedCodeBlockProps.className),
}
if (
@@ -128,9 +141,7 @@ export const CodeTabs = ({
}
const modifiedProps: CodeBlockProps = {
...(getCodeBlockProps(codeBlock) || {
source: "",
}),
...parsedCodeBlockProps,
...commonProps,
}

View File

@@ -9,6 +9,7 @@ export * from "./use-generate-snippet"
export * from "./use-heading-url"
export * from "./use-current-learning-path"
export * from "./use-is-external-link"
export * from "./use-mutation-observer"
export * from "./use-keyboard-shortcut"
export * from "./use-page-scroll-manager"
export * from "./use-request-runner"

View File

@@ -1,3 +1,5 @@
"use client"
import { useEffect } from "react"
type UseMutationObserverProps = {

View File

@@ -28,3 +28,20 @@ export function getFrontMatterSync(filePath: string): FrontMatter {
return content.data.matter as FrontMatter
}
export async function getFrontMatterFromString(
fileContent: string
): Promise<FrontMatter> {
return (
await unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkFrontmatter, ["yaml"])
.use(() => {
return (tree, file) => {
matter(file)
}
})
.process(fileContent)
).data.matter as FrontMatter
}

View File

@@ -19,4 +19,5 @@ export declare type FrontMatter = {
toc_max_depth?: number
generate_toc?: boolean
hide_content_menu?: boolean
keywords?: string[]
}

View File

@@ -313,6 +313,17 @@ __metadata:
languageName: node
linkType: hard
"@babel/code-frame@npm:^7.27.1":
version: 7.27.1
resolution: "@babel/code-frame@npm:7.27.1"
dependencies:
"@babel/helper-validator-identifier": ^7.27.1
js-tokens: ^4.0.0
picocolors: ^1.1.1
checksum: 5dd9a18baa5fce4741ba729acc3a3272c49c25cb8736c4b18e113099520e7ef7b545a4096a26d600e4416157e63e87d66db46aa3fbf0a5f2286da2705c12da00
languageName: node
linkType: hard
"@babel/compat-data@npm:^7.23.5":
version: 7.23.5
resolution: "@babel/compat-data@npm:7.23.5"
@@ -471,6 +482,13 @@ __metadata:
languageName: node
linkType: hard
"@babel/helper-validator-identifier@npm:^7.27.1":
version: 7.27.1
resolution: "@babel/helper-validator-identifier@npm:7.27.1"
checksum: c558f11c4871d526498e49d07a84752d1800bf72ac0d3dad100309a2eaba24efbf56ea59af5137ff15e3a00280ebe588560534b0e894a4750f8b1411d8f78b84
languageName: node
linkType: hard
"@babel/helper-validator-option@npm:^7.23.5":
version: 7.23.5
resolution: "@babel/helper-validator-option@npm:7.23.5"
@@ -1355,6 +1373,39 @@ __metadata:
languageName: node
linkType: hard
"@mdx-js/mdx@npm:^3.1.1":
version: 3.1.1
resolution: "@mdx-js/mdx@npm:3.1.1"
dependencies:
"@types/estree": ^1.0.0
"@types/estree-jsx": ^1.0.0
"@types/hast": ^3.0.0
"@types/mdx": ^2.0.0
acorn: ^8.0.0
collapse-white-space: ^2.0.0
devlop: ^1.0.0
estree-util-is-identifier-name: ^3.0.0
estree-util-scope: ^1.0.0
estree-walker: ^3.0.0
hast-util-to-jsx-runtime: ^2.0.0
markdown-extensions: ^2.0.0
recma-build-jsx: ^1.0.0
recma-jsx: ^1.0.0
recma-stringify: ^1.0.0
rehype-recma: ^1.0.0
remark-mdx: ^3.0.0
remark-parse: ^11.0.0
remark-rehype: ^11.0.0
source-map: ^0.7.0
unified: ^11.0.0
unist-util-position-from-estree: ^2.0.0
unist-util-stringify-position: ^4.0.0
unist-util-visit: ^5.0.0
vfile: ^6.0.0
checksum: 371ed95e2bee7731f30a7ce57db66383a0b7470e66c38139427174cb456d6a40bf7d259f3652716370c1de64acfba50a1ba27eb8c556e7a431dc7940b04cb1a1
languageName: node
linkType: hard
"@mdx-js/react@npm:^3.0.1":
version: 3.0.1
resolution: "@mdx-js/react@npm:3.0.1"
@@ -1379,6 +1430,18 @@ __metadata:
languageName: node
linkType: hard
"@mdx-js/react@npm:^3.1.1":
version: 3.1.1
resolution: "@mdx-js/react@npm:3.1.1"
dependencies:
"@types/mdx": ^2.0.0
peerDependencies:
"@types/react": ">=16"
react: ">=16"
checksum: 34ca98bc2a0f969894ea144dc5c8a5294690505458cd24965cd9be854d779c193ad9192bf9143c4c18438fafd1902e100d99067e045c69319288562d497558c6
languageName: node
linkType: hard
"@medusajs/icons@npm:2.10.3":
version: 2.10.3
resolution: "@medusajs/icons@npm:2.10.3"
@@ -4926,6 +4989,15 @@ __metadata:
languageName: node
linkType: hard
"@types/mdast@npm:^4.0.4":
version: 4.0.4
resolution: "@types/mdast@npm:4.0.4"
dependencies:
"@types/unist": "*"
checksum: 84f403dbe582ee508fd9c7643ac781ad8597fcbfc9ccb8d4715a2c92e4545e5772cbd0dbdf18eda65789386d81b009967fdef01b24faf6640f817287f54d9c82
languageName: node
linkType: hard
"@types/mdx@npm:^2.0.0":
version: 2.0.11
resolution: "@types/mdx@npm:2.0.11"
@@ -7007,7 +7079,7 @@ __metadata:
languageName: node
linkType: hard
"dequal@npm:^2.0.0":
"dequal@npm:^2.0.0, dequal@npm:^2.0.3":
version: 2.0.3
resolution: "dequal@npm:2.0.3"
checksum: f98860cdf58b64991ae10205137c0e97d384c3a4edc7f807603887b7c4b850af1224a33d88012009f150861cbee4fa2d322c4cc04b9313bee312e47f6ecaa888
@@ -10590,7 +10662,7 @@ __metadata:
languageName: node
linkType: hard
"mdast-util-mdxjs-esm@npm:^2.0.0":
"mdast-util-mdxjs-esm@npm:^2.0.0, mdast-util-mdxjs-esm@npm:^2.0.1":
version: 2.0.1
resolution: "mdast-util-mdxjs-esm@npm:2.0.1"
dependencies:
@@ -11629,6 +11701,24 @@ __metadata:
languageName: node
linkType: hard
"next-mdx-remote-client@npm:2":
version: 2.1.7
resolution: "next-mdx-remote-client@npm:2.1.7"
dependencies:
"@babel/code-frame": ^7.27.1
"@mdx-js/mdx": ^3.1.1
"@mdx-js/react": ^3.1.1
remark-mdx-remove-esm: ^1.2.1
serialize-error: ^12.0.0
vfile: ^6.0.3
vfile-matter: ^5.0.1
peerDependencies:
react: ^19.1
react-dom: ^19.1
checksum: 6d7a27e9a5068509e043cdcbadc2d93559077cdcbf2c3ee429b4cc579c7a0bbc10285f6983113f9210c65063d6118ae1ffb418d14a5e7ac810a9734046b48d3f
languageName: node
linkType: hard
"next-mdx-remote@npm:5.0.0":
version: 5.0.0
resolution: "next-mdx-remote@npm:5.0.0"
@@ -13134,6 +13224,19 @@ __metadata:
languageName: node
linkType: hard
"remark-mdx-remove-esm@npm:^1.2.1":
version: 1.2.1
resolution: "remark-mdx-remove-esm@npm:1.2.1"
dependencies:
"@types/mdast": ^4.0.4
mdast-util-mdxjs-esm: ^2.0.1
unist-util-remove: ^4.0.0
peerDependencies:
unified: ^11
checksum: 795fceac27402fdf98273d6f917436a49012cbd5c0b8a75dfd4ac59e0e32b17ad3e89cfda4a5553a5d7e04a669c00d8017a3a15e89d5073f1da9c3eaa970eebe
languageName: node
linkType: hard
"remark-mdx@npm:^3.0.0":
version: 3.0.1
resolution: "remark-mdx@npm:3.0.1"
@@ -13326,6 +13429,7 @@ __metadata:
eslint-plugin-prettier: ^5.2.1
eslint-plugin-react-hooks: ^5.0.0
next: 15.3.5
next-mdx-remote-client: 2
postcss: ^8
posthog-js: ^1.269.1
react: rc
@@ -13334,6 +13438,7 @@ __metadata:
remark-directive: ^3.0.0
remark-frontmatter: ^5.0.0
remark-rehype-plugins: "*"
swr: ^2.3.6
tags: "*"
tailwind: "*"
tailwindcss: ^3.3.0
@@ -13575,6 +13680,15 @@ __metadata:
languageName: node
linkType: hard
"serialize-error@npm:^12.0.0":
version: 12.0.0
resolution: "serialize-error@npm:12.0.0"
dependencies:
type-fest: ^4.31.0
checksum: d8422db262dd28422834e0acdaaa2425ba6735f791417cfbcbceb201ddea0e41ccd2865778afeb9b33c28273f01e89a4503fa670f82c0a387de61e2b0f8d74e4
languageName: node
linkType: hard
"set-function-length@npm:^1.2.1":
version: 1.2.1
resolution: "set-function-length@npm:1.2.1"
@@ -14291,6 +14405,18 @@ __metadata:
languageName: node
linkType: hard
"swr@npm:^2.3.6":
version: 2.3.6
resolution: "swr@npm:2.3.6"
dependencies:
dequal: ^2.0.3
use-sync-external-store: ^1.4.0
peerDependencies:
react: ^16.11.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
checksum: 9534f350982e36a3ae0a13da8c0f7da7011fc979e77f306e60c4e5db0f9b84f17172c44f973441ba56bb684b69b0d9838ab40011a6b6b3e32d0cd7f3d5405f99
languageName: node
linkType: hard
"symbol-tree@npm:^3.2.4":
version: 3.2.4
resolution: "symbol-tree@npm:3.2.4"
@@ -14837,6 +14963,13 @@ turbo@latest:
languageName: node
linkType: hard
"type-fest@npm:^4.31.0":
version: 4.41.0
resolution: "type-fest@npm:4.41.0"
checksum: f5ca697797ed5e88d33ac8f1fec21921839871f808dc59345c9cf67345bfb958ce41bd821165dbf3ae591cedec2bf6fe8882098dfdd8dc54320b859711a2c1e4
languageName: node
linkType: hard
"typed-array-buffer@npm:^1.0.1, typed-array-buffer@npm:^1.0.2":
version: 1.0.2
resolution: "typed-array-buffer@npm:1.0.2"
@@ -15243,6 +15376,17 @@ turbo@latest:
languageName: node
linkType: hard
"unist-util-remove@npm:^4.0.0":
version: 4.0.0
resolution: "unist-util-remove@npm:4.0.0"
dependencies:
"@types/unist": ^3.0.0
unist-util-is: ^6.0.0
unist-util-visit-parents: ^6.0.0
checksum: 30f3ed31095dd7f3109266d39c514fab5f2da3fb656d5f78a0e3e7700f219760f2f4d8286c810ae43c241fee3f0a8dd40f8d1e5ebeee3cb810581d5e7e8d4f7d
languageName: node
linkType: hard
"unist-util-stringify-position@npm:^2.0.0":
version: 2.0.3
resolution: "unist-util-stringify-position@npm:2.0.3"
@@ -15392,6 +15536,15 @@ turbo@latest:
languageName: node
linkType: hard
"use-sync-external-store@npm:^1.4.0":
version: 1.6.0
resolution: "use-sync-external-store@npm:1.6.0"
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
checksum: 35e1179f872a53227bdf8a827f7911da4c37c0f4091c29b76b1e32473d1670ebe7bcd880b808b7549ba9a5605c233350f800ffab963ee4a4ee346ee983b6019b
languageName: node
linkType: hard
"user-guide@workspace:apps/user-guide":
version: 0.0.0-use.local
resolution: "user-guide@workspace:apps/user-guide"
@@ -15530,7 +15683,7 @@ turbo@latest:
languageName: node
linkType: hard
"vfile@npm:^6.0.1":
"vfile@npm:^6.0.1, vfile@npm:^6.0.3":
version: 6.0.3
resolution: "vfile@npm:6.0.3"
dependencies: