Skip to content

Commit 84c4d8a

Browse files
rsbhclaude
andauthored
fix: skip prefetching for sidebar top links (#86)
* fix: skip prefetching for sidebar top links Add data-no-prefetch attribute check in PrefetchProvider to skip links that shouldn't be prefetched. Apply to content and API top links in sidebar which are index routes, not doc pages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: extract data-no-prefetch attr name to constant Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: use template literal for querySelector interpolation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 872b936 commit 84c4d8a

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

packages/chronicle/src/components/ui/PrefetchProvider.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useEffect } from 'react';
22
import { prefetchPageData } from '@/lib/preload';
33

4+
const NO_PREFETCH_ATTR = 'data-no-prefetch';
5+
46
function resolvePathname(href: string | null): string | null {
57
if (!href) return null;
68
try {
@@ -16,14 +18,14 @@ export function PrefetchProvider({ children }: { children: React.ReactNode }) {
1618
useEffect(() => {
1719
const handleMouseOver = (e: MouseEvent) => {
1820
const anchor = (e.target as HTMLElement).closest?.('a[href]');
19-
if (!anchor) return;
21+
if (!anchor || anchor.hasAttribute(NO_PREFETCH_ATTR)) return;
2022
const pathname = resolvePathname(anchor.getAttribute('href'));
2123
if (pathname) prefetchPageData(pathname);
2224
};
2325

2426
const handleFocusIn = (e: FocusEvent) => {
2527
const anchor = (e.target as HTMLElement).closest?.('a[href]');
26-
if (!anchor) return;
28+
if (!anchor || anchor.hasAttribute(NO_PREFETCH_ATTR)) return;
2729
const pathname = resolvePathname(anchor.getAttribute('href'));
2830
if (pathname) prefetchPageData(pathname);
2931
};
@@ -45,7 +47,7 @@ export function PrefetchProvider({ children }: { children: React.ReactNode }) {
4547
);
4648

4749
const observeLinks = () => {
48-
document.querySelectorAll('a[href]:not([data-prefetch-observed])').forEach((link) => {
50+
document.querySelectorAll(`a[href]:not([data-prefetch-observed]):not([${NO_PREFETCH_ATTR}])`).forEach((link) => {
4951
const pathname = resolvePathname(link.getAttribute('href'));
5052
if (pathname) {
5153
link.setAttribute('data-prefetch-observed', '');

packages/chronicle/src/themes/default/Layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export function Layout({
138138
<DocumentTextIcon width={16} height={16} />
139139
)}
140140
classNames={{ root: styles.topLinkItem, text: styles.topLinkText }}
141-
render={<RouterLink to={entry.href} />}
141+
render={<RouterLink to={entry.href} data-no-prefetch />}
142142
>
143143
{entry.label}
144144
</Sidebar.Item>
@@ -154,7 +154,7 @@ export function Layout({
154154
<CodeBracketSquareIcon width={16} height={16} />
155155
)}
156156
classNames={{ root: styles.topLinkItem, text: styles.topLinkText }}
157-
render={<RouterLink to={api.basePath} />}
157+
render={<RouterLink to={api.basePath} data-no-prefetch />}
158158
>
159159
{api.name} API
160160
</Sidebar.Item>

0 commit comments

Comments
 (0)