|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import type { ReactNode } from 'react'; |
| 4 | +import Image from 'next/image'; |
| 5 | +import Link from 'next/link'; |
| 6 | +import { |
| 7 | + useParams, |
| 8 | + usePathname as useRoutePathname, |
| 9 | + useRouter, |
| 10 | +} from 'next/navigation'; |
| 11 | +import { FrameworkProvider, type Framework } from 'fumadocs-core/framework'; |
| 12 | +import { RootProvider } from 'fumadocs-ui/provider/base'; |
| 13 | +import { i18n as i18nConfig } from '@/lib/i18n'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Map an internal Next.js route pathname onto the public URL path the browser |
| 17 | + * shows for it. |
| 18 | + * |
| 19 | + * Every page is routed under `app/[lang]/…`, but `hideLocale: 'default-locale'` |
| 20 | + * keeps the default locale out of the public URL: `/docs/quickstart` is served |
| 21 | + * by rewriting to the `/en/docs/quickstart` route (see `middleware.ts`). The two |
| 22 | + * renderers therefore observe different pathnames for one page — the build-time |
| 23 | + * prerender knows only the internal route (`/en/docs/quickstart`), the browser |
| 24 | + * knows only the public URL (`/docs/quickstart`). |
| 25 | + * |
| 26 | + * Everything a pathname is compared against — the `url` of every page-tree node, |
| 27 | + * produced by `loader({ i18n })` in `lib/source.ts` — is in the *public* space, |
| 28 | + * so the internal form is the one that has to be normalised away. Feeding the |
| 29 | + * raw route pathname to Fumadocs makes the server look the current page up in |
| 30 | + * the page tree and miss, so it renders no active sidebar item, no expanded |
| 31 | + * folder, no page title in the TOC trigger and no prev/next footer — while the |
| 32 | + * client, at the public URL, renders all four. That divergence is the hydration |
| 33 | + * mismatch behind React #418 on unprefixed routes. |
| 34 | + * |
| 35 | + * Mirrors Fumadocs' own URL rules for `hideLocale` (`loader`'s `getUrl`), so the |
| 36 | + * two stay in agreement: `'never'` keeps every prefix, `'default-locale'` hides |
| 37 | + * only the default language, `'always'` hides every supported one. Only a whole |
| 38 | + * leading segment that is a supported locale is removed, so a page such as |
| 39 | + * `/end-to-end` is left alone. |
| 40 | + */ |
| 41 | +export function toPublicPathname(pathname: string): string { |
| 42 | + const segments = pathname.split('/'); |
| 43 | + const locale = segments[1]; |
| 44 | + if (!locale || !(i18nConfig.languages as readonly string[]).includes(locale)) { |
| 45 | + return pathname; |
| 46 | + } |
| 47 | + |
| 48 | + const hidden = |
| 49 | + i18nConfig.hideLocale === 'always' || |
| 50 | + (i18nConfig.hideLocale === 'default-locale' && |
| 51 | + locale === i18nConfig.defaultLanguage); |
| 52 | + if (!hidden) return pathname; |
| 53 | + |
| 54 | + const rest = segments.slice(2).join('/'); |
| 55 | + return rest ? `/${rest}` : '/'; |
| 56 | +} |
| 57 | + |
| 58 | +function usePublicPathname(): string { |
| 59 | + return toPublicPathname(useRoutePathname()); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * `RootProvider` wired to Next.js exactly as `fumadocs-ui/provider/next` does, |
| 64 | + * except that the `usePathname` handed to Fumadocs reports the public URL. |
| 65 | + * |
| 66 | + * `FrameworkProvider` is the single point at which a pathname enters Fumadocs — |
| 67 | + * all of its components read it through `usePathname` from |
| 68 | + * `fumadocs-core/framework` — so normalising it here fixes every consumer at |
| 69 | + * once. This is why the provider is assembled by hand rather than imported from |
| 70 | + * `fumadocs-ui/provider/next`: that entry point wraps `RootProvider` in its own |
| 71 | + * `FrameworkProvider`, which would take precedence over one layered outside it. |
| 72 | + */ |
| 73 | +export function DocsRootProvider({ |
| 74 | + locale, |
| 75 | + locales, |
| 76 | + children, |
| 77 | +}: { |
| 78 | + locale: string; |
| 79 | + locales: { name: string; locale: string }[]; |
| 80 | + children: ReactNode; |
| 81 | +}) { |
| 82 | + return ( |
| 83 | + <FrameworkProvider |
| 84 | + usePathname={usePublicPathname} |
| 85 | + useRouter={useRouter} |
| 86 | + useParams={useParams} |
| 87 | + // Next's `Link`/`Image` require `href`/`src`; Fumadocs types the same |
| 88 | + // slots with those props optional. Fumadocs always supplies them, so the |
| 89 | + // gap is in the caller's favour and only the declarations disagree — |
| 90 | + // `fumadocs-ui/provider/next` hands Next's components to these very slots |
| 91 | + // at runtime. The casts state that contract; they do not widen it. |
| 92 | + Link={Link as Framework['Link']} |
| 93 | + Image={Image as Framework['Image']} |
| 94 | + > |
| 95 | + <RootProvider i18n={{ locale, locales }}>{children}</RootProvider> |
| 96 | + </FrameworkProvider> |
| 97 | + ); |
| 98 | +} |
0 commit comments