Skip to content

Commit 77acb42

Browse files
committed
feat: use next-cookie for the pages/app router type
1 parent ce4b639 commit 77acb42

File tree

6 files changed

+66
-48
lines changed

6 files changed

+66
-48
lines changed

apps/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@fumadocs/mdx-remote": "^1.3.0",
16+
"cookies-next": "^5.1.0",
1617
"fumadocs-core": "15.2.15",
1718
"fumadocs-mdx": "11.6.2",
1819
"fumadocs-ui": "15.2.15",

apps/docs/src/app/docs/[[...slug]]/page.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { DocsLayout } from '@/components/layout';
2-
import { type RouterType, routerTypeCookie } from '@/lib/const';
32
import { getPage } from '@/lib/page';
4-
import { getDocsLayoutTree, getPageTreePeers } from '@/lib/pageTree';
3+
import { getPageTreePeers } from '@/lib/pageTree';
54
import { type Page, type Source, source } from '@/lib/source';
65
import { getDocId, getDocUrl, parseDocId } from '@/lib/utils';
76
import { getMDXComponents } from '@/mdx-components';
@@ -12,15 +11,12 @@ import { createRelativeLink } from 'fumadocs-ui/mdx';
1211
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/page';
1312
import { type Locale, useLocale } from 'next-intl';
1413
import { getLocale } from 'next-intl/server';
15-
import { cookies } from 'next/headers';
1614
import { notFound } from 'next/navigation';
1715

1816
export default async function Docs(props: {
1917
params: Promise<{ slug: string[] }>;
2018
}) {
2119
const locale: Locale = await getLocale();
22-
const routerType = (await cookies()).get(routerTypeCookie)
23-
?.value as RouterType;
2420
const params = await props.params;
2521
const slug = params.slug || [];
2622
const docUrl = getDocUrl(slug);
@@ -54,11 +50,7 @@ export default async function Docs(props: {
5450
const isIndex = page.file.name === 'index';
5551

5652
return (
57-
<DocsLayout
58-
routerType={routerType}
59-
docId={docId}
60-
pageTree={getDocsLayoutTree(source.pageTree, docId, routerType)}
61-
>
53+
<DocsLayout docId={docId} pageTree={source.pageTree}>
6254
<DocsPage
6355
toc={toc}
6456
full={page.data.full}

apps/docs/src/components/layout.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use client';
22

33
import { useBaseOptions } from '@/hooks/useLayoutOptions';
4+
import { useRouterType } from '@/hooks/useRouterType';
45
import type { RouterType } from '@/lib/const';
6+
import { getDocsLayoutTree } from '@/lib/pageTree';
57
import { parseDocId } from '@/lib/utils';
68
import {
79
NextAppIcon,
@@ -22,15 +24,15 @@ export function DocsLayout({
2224
children,
2325
pageTree,
2426
docId,
25-
routerType,
2627
}: {
2728
children: ReactNode;
2829
pageTree: PageTree.Root;
2930
docId: string;
30-
routerType: RouterType;
3131
}) {
32+
const routerType = useRouterType();
3233
const baseOptions = useBaseOptions();
3334
const t = useTranslations('sidebar');
35+
const tree = getDocsLayoutTree(pageTree, docId, routerType);
3436
const { appDocsRoot, pagesDocsRoot, docsRoot, docUrl, isPages } =
3537
parseDocId(docId);
3638
const options = [
@@ -53,7 +55,7 @@ export function DocsLayout({
5355
const docsLayout: DocsLayoutProps = {
5456
...baseOptions,
5557
links: [],
56-
tree: pageTree,
58+
tree,
5759
sidebar: {
5860
banner: (
5961
<>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use client';
2+
3+
import { type RouterType, routerTypeCookie } from '@/lib/const';
4+
import { parseDocId } from '@/lib/utils';
5+
import { getCookie, setCookie } from 'cookies-next';
6+
import { usePathname } from 'next/navigation';
7+
import { useEffect, useState } from 'react';
8+
9+
export function useRouterType() {
10+
const pathname = usePathname();
11+
const [routerType, setRouterType] = useState<RouterType>(() => {
12+
const docId = `en${pathname}`;
13+
const { isPages, isApp } = parseDocId(docId);
14+
if (isPages) {
15+
return 'pages';
16+
}
17+
if (isApp) {
18+
return 'app';
19+
}
20+
return (getCookie(routerTypeCookie) as RouterType) || 'app';
21+
});
22+
23+
useEffect(() => {
24+
const docId = `en${pathname}`;
25+
const { isPages, isApp } = parseDocId(docId);
26+
if (isPages) {
27+
setCookie(routerTypeCookie, 'pages');
28+
setRouterType('pages');
29+
}
30+
if (isApp) {
31+
setCookie(routerTypeCookie, 'app');
32+
setRouterType('app');
33+
}
34+
}, [pathname]);
35+
36+
return routerType;
37+
}

apps/docs/src/middleware.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)