Skip to content

Commit

Permalink
⚡️ perf: prefetch page to improve performance (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 22, 2023
1 parent 8c8221e commit 6211be9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
52 changes: 27 additions & 25 deletions src/features/SideBar/TopActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionIcon } from '@lobehub/ui';
import { Bot, MessageSquare } from 'lucide-react';
import { usePathname, useRouter } from 'next/navigation';
import Link from 'next/link';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

Expand All @@ -14,37 +14,39 @@ export interface TopActionProps {
}

const TopActions = memo<TopActionProps>(({ tab, setTab }) => {
const pathname = usePathname();
const router = useRouter();
const { t } = useTranslation('common');
const switchBackToChat = useSessionStore((s) => s.switchBackToChat);

return (
<>
<ActionIcon
active={tab === SidebarTabKey.Chat}
icon={MessageSquare}
onClick={() => {
// 如果已经在 chat 路径下了,那么就不用再跳转了
if (pathname?.startsWith('/chat')) return;
<Link
href={'/chat'}
onClick={(e) => {
e.preventDefault();
switchBackToChat();
setTab(SidebarTabKey.Chat);
}}
placement={'right'}
size="large"
title={t('tab.chat')}
/>
<ActionIcon
active={tab === SidebarTabKey.Market}
icon={Bot}
onClick={() => {
if (pathname?.startsWith('/market')) return;
router.push('/market');
setTab(SidebarTabKey.Market);
}}
placement={'right'}
size="large"
title={t('tab.market')}
/>
>
<ActionIcon
active={tab === SidebarTabKey.Chat}
icon={MessageSquare}
placement={'right'}
size="large"
title={t('tab.chat')}
/>
</Link>
<Link href={'/market'}>
<ActionIcon
active={tab === SidebarTabKey.Market}
icon={Bot}
onClick={() => {
setTab(SidebarTabKey.Market);
}}
placement={'right'}
size="large"
title={t('tab.market')}
/>
</Link>
</>
);
});
Expand Down
6 changes: 6 additions & 0 deletions src/layout/GlobalLayout/StoreHydration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const StoreHydration = memo(() => {
},
[router],
);
useEffect(() => {
router.prefetch('/chat');
router.prefetch('/market');
router.prefetch('/settings/common');
router.prefetch('/settings/agent');
}, [router]);

return null;
});
Expand Down

0 comments on commit 6211be9

Please sign in to comment.