mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-11-24 09:12:05 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| af08c4ac08 | |||
| e9890dfa4c | |||
| 5c2986e1c5 |
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"gitbook": patch
|
||||
---
|
||||
|
||||
Reduce background downloads during search by prefetching the result visitors hover or select.
|
||||
@@ -8,6 +8,7 @@ import { getPageResultPresentation } from './getPageResultHref';
|
||||
import { HighlightQuery } from './HighlightQuery';
|
||||
import type { ComputedPageResult } from './search-types';
|
||||
import { SearchResultItem } from './SearchResultItem';
|
||||
import { usePrefetchWhenActive } from './usePrefetchWhenActive';
|
||||
import { tString, useLanguage } from '@/intl/client';
|
||||
import { tcls } from '@/lib/tailwind';
|
||||
|
||||
@@ -24,6 +25,7 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
|
||||
const language = useLanguage();
|
||||
|
||||
const { preview: bestSection, description, href } = getPageResultPresentation(item);
|
||||
usePrefetchWhenActive(href, active);
|
||||
const preview = bestSection
|
||||
? [bestSection.title, bestSection.body].filter(Boolean).join(' · ')
|
||||
: undefined;
|
||||
@@ -50,6 +52,7 @@ export const SearchPageResultItem = React.forwardRef(function SearchPageResultIt
|
||||
...style,
|
||||
}}
|
||||
{...rest}
|
||||
prefetch={false}
|
||||
>
|
||||
<Breadcrumbs breadcrumbs={item.breadcrumbs} />
|
||||
<p className="line-clamp-1 font-heading text-base font-semibold leading-snug text-tint-strong">
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Favicon } from '../utils';
|
||||
import { HighlightQuery } from './HighlightQuery';
|
||||
import type { ComputedRecordResult } from './search-types';
|
||||
import { SearchResultItem } from './SearchResultItem';
|
||||
import { usePrefetchWhenActive } from './usePrefetchWhenActive';
|
||||
import { tString, useLanguage } from '@/intl/client';
|
||||
|
||||
export const SearchRecordResultItem = React.forwardRef(function SearchRecordResultItem(
|
||||
@@ -20,6 +21,7 @@ export const SearchRecordResultItem = React.forwardRef(function SearchRecordResu
|
||||
const { query, item, active, style, ...rest } = props;
|
||||
const language = useLanguage();
|
||||
|
||||
usePrefetchWhenActive(item.href, active);
|
||||
const domain = getDomain(item.href);
|
||||
|
||||
return (
|
||||
@@ -46,6 +48,7 @@ export const SearchRecordResultItem = React.forwardRef(function SearchRecordResu
|
||||
aria-label={tString(language, 'search_page_result_title', item.title)}
|
||||
style={{ ...style }}
|
||||
{...rest}
|
||||
prefetch={false}
|
||||
>
|
||||
<p className="line-clamp-2 text-base font-semibold leading-snug text-tint-strong">
|
||||
<HighlightQuery query={query} text={item.title} />
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { PrefetchKind } from 'next/dist/client/components/router-reducer/router-reducer-types';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import React from 'react';
|
||||
|
||||
import { defaultIsExternalClient, hasSideEffectQueryParams, LinkContext } from '../primitives';
|
||||
import { isExternalLink, isSiteAuthLink } from '../utils/link';
|
||||
|
||||
const RESULT_PREFETCH_DELAY_MS = 200;
|
||||
|
||||
export function usePrefetchWhenActive(href: string, active: boolean) {
|
||||
const router = useRouter();
|
||||
const { isExternalServer = isExternalLink, isExternalClient = defaultIsExternalClient } =
|
||||
React.useContext(LinkContext);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (
|
||||
!active ||
|
||||
isExternalServer(href) ||
|
||||
isSiteAuthLink(href) ||
|
||||
hasSideEffectQueryParams(href) ||
|
||||
(window.self !== window.top && isExternalClient(href))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const timeout = setTimeout(
|
||||
() => router.prefetch(href, { kind: PrefetchKind.FULL }),
|
||||
RESULT_PREFETCH_DELAY_MS
|
||||
);
|
||||
return () => clearTimeout(timeout);
|
||||
}, [active, href, isExternalClient, isExternalServer, router]);
|
||||
}
|
||||
@@ -74,7 +74,7 @@ function getTargetProps(
|
||||
/**
|
||||
* Check if the link is external with the origin.
|
||||
*/
|
||||
function defaultIsExternalClient(href: string) {
|
||||
export function defaultIsExternalClient(href: string) {
|
||||
return isExternalLink(href, window.location.origin);
|
||||
}
|
||||
|
||||
@@ -183,7 +183,7 @@ export function Link(props: LinkProps) {
|
||||
* NextLink automatically prefetches links (RSC requests) on hover/viewport, which would fire those
|
||||
* side effects without any user intent, so such links should not be prefetched.
|
||||
*/
|
||||
function hasSideEffectQueryParams(href: string): boolean {
|
||||
export function hasSideEffectQueryParams(href: string): boolean {
|
||||
const baseURL = typeof window !== 'undefined' ? window.location.origin : 'http://localhost';
|
||||
const linkURL = URL.canParse(href) ? new URL(href) : new URL(href, baseURL);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user