Core: hotfix Tanstack Router usePathname

This commit is contained in:
Fuma Nama
2025-11-18 20:12:57 +08:00
parent f93dfc8619
commit c3b8474687
3 changed files with 30 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
hotfix Tanstack Router `usePathname` inconsistency due to `useMatch` on layout.
+24 -4
View File
@@ -1,9 +1,14 @@
import { type ReactNode, useMemo } from 'react';
import { type ReactNode, useRef, useMemo } from 'react';
import { type Framework, FrameworkProvider } from '@/framework/index';
import { useParams, Link, useRouter, useMatch } from '@tanstack/react-router';
import {
useParams,
Link,
useRouter,
useRouterState,
} from '@tanstack/react-router';
const framework: Framework = {
Link({ href, prefetch, ...props }) {
Link({ href, prefetch = true, ...props }) {
return (
<Link to={href} preload={prefetch ? 'intent' : false} {...props}>
{props.children}
@@ -11,7 +16,22 @@ const framework: Framework = {
);
},
usePathname() {
return useMatch({ strict: false, select: (s) => s.pathname });
const { isLoading, pathname } = useRouterState({
select: (state) => ({
isLoading: state.isLoading,
pathname: state.location.pathname,
}),
});
const activePathname = useRef(pathname);
return useMemo(() => {
if (isLoading) {
return activePathname.current;
}
activePathname.current = pathname;
return pathname;
}, [isLoading, pathname]);
},
useRouter() {
const router = useRouter();
+1 -1
View File
@@ -3,7 +3,7 @@ import { type ComponentProps, Fragment, useState } from 'react';
import { cva } from 'class-variance-authority';
import Link from 'fumadocs-core/link';
import { cn } from '@/utils/cn';
import { BaseLinkItem, LinkItemType } from '@/layouts/shared';
import { BaseLinkItem, type LinkItemType } from '@/layouts/shared';
import {
NavigationMenu,
NavigationMenuContent,