Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/chronicle/src/components/ui/PrefetchProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useEffect } from 'react';
import { prefetchPageData } from '@/lib/preload';

const NO_PREFETCH_ATTR = 'data-no-prefetch';

function resolvePathname(href: string | null): string | null {
if (!href) return null;
try {
Expand All @@ -16,14 +18,14 @@ export function PrefetchProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
const handleMouseOver = (e: MouseEvent) => {
const anchor = (e.target as HTMLElement).closest?.('a[href]');
if (!anchor) return;
if (!anchor || anchor.hasAttribute(NO_PREFETCH_ATTR)) return;
const pathname = resolvePathname(anchor.getAttribute('href'));
if (pathname) prefetchPageData(pathname);
};

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

const observeLinks = () => {
document.querySelectorAll('a[href]:not([data-prefetch-observed])').forEach((link) => {
document.querySelectorAll(`a[href]:not([data-prefetch-observed]):not([${NO_PREFETCH_ATTR}])`).forEach((link) => {
const pathname = resolvePathname(link.getAttribute('href'));
if (pathname) {
link.setAttribute('data-prefetch-observed', '');
Expand Down
4 changes: 2 additions & 2 deletions packages/chronicle/src/themes/default/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function Layout({
<DocumentTextIcon width={16} height={16} />
)}
classNames={{ root: styles.topLinkItem, text: styles.topLinkText }}
render={<RouterLink to={entry.href} />}
render={<RouterLink to={entry.href} data-no-prefetch />}
>
{entry.label}
</Sidebar.Item>
Expand All @@ -154,7 +154,7 @@ export function Layout({
<CodeBracketSquareIcon width={16} height={16} />
)}
classNames={{ root: styles.topLinkItem, text: styles.topLinkText }}
render={<RouterLink to={api.basePath} />}
render={<RouterLink to={api.basePath} data-no-prefetch />}
>
{api.name} API
</Sidebar.Item>
Expand Down
Loading