Skip to content

Commit

Permalink
docs: enable prefetch for dumi Link (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Dec 6, 2024
1 parent 5321c19 commit 9c2bf53
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .dumi/pages/index/components/MainBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createStyles } from 'antd-style';
import classnames from 'classnames';
import { Link, useLocation } from 'dumi';
import { useLocation } from 'dumi';
import React from 'react';

import { Button } from 'antd';
import useLocale from '../../../hooks/useLocale';
import useLottie from '../../../hooks/useLottie';
import Link from '../../../theme/common/Link';
import { getLocalizedPathname, isZhCN } from '../../../theme/utils';
import Container from '../common/Container';
import SiteContext from './SiteContext';
Expand Down
2 changes: 1 addition & 1 deletion .dumi/theme/builtins/ComponentOverview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ const Overview: React.FC = () => {
{cardContent}
</a>
) : (
<Link to={url} prefetch key={component.title}>
<Link to={url} key={component.title}>
{cardContent}
</Link>
);
Expand Down
8 changes: 5 additions & 3 deletions .dumi/theme/builtins/DemoWrapper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useContext } from 'react';
import { BugOutlined, CodeOutlined, ExperimentOutlined } from '@ant-design/icons';
import { XProvider } from '@ant-design/x';
import { Tooltip, Button } from 'antd';
import { Button, Tooltip } from 'antd';
import classNames from 'classnames';
import { DumiDemoGrid, FormattedMessage } from 'dumi';
import React, { Suspense, useContext } from 'react';

import useLayoutState from '../../../hooks/useLayoutState';
import useLocale from '../../../hooks/useLocale';
Expand Down Expand Up @@ -106,7 +106,9 @@ const DemoWrapper: typeof DumiDemoGrid = ({ items }) => {
</Tooltip>
</span>
<XProvider theme={{ cssVar: enableCssVar, hashed: !enableCssVar }}>
<DumiDemoGrid items={demos} />
<Suspense>
<DumiDemoGrid items={demos} />
</Suspense>
</XProvider>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion .dumi/theme/builtins/LocaleLink/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { Link } from 'dumi';

import useLocale from '../../../hooks/useLocale';
import Link from '../../../theme/common/Link';

type LinkProps = Parameters<typeof Link>[0];

Expand Down
98 changes: 41 additions & 57 deletions .dumi/theme/common/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,72 +1,56 @@
import { Link as DumiLink, useLocation, useNavigate } from 'dumi';
import nprogress from 'nprogress';
import { Link as DumiLink, useAppData, useLocation, useNavigate } from 'dumi';
import type { MouseEvent, MouseEventHandler } from 'react';
import React, { forwardRef, useLayoutEffect, useTransition } from 'react';
import React, { useMemo, forwardRef } from 'react';

export interface LinkProps {
to: string | { pathname?: string; search?: string; hash?: string };
style?: React.CSSProperties;
className?: string;
onClick?: MouseEventHandler;
component?: React.ComponentType<any>;
children?: React.ReactNode;
}

nprogress.configure({ showSpinner: false });

const Link = forwardRef<HTMLAnchorElement, React.PropsWithChildren<LinkProps>>((props, ref) => {
const { to, children, component, ...rest } = props;
const [isPending, startTransition] = useTransition();
const navigate = useNavigate();
const { pathname } = useLocation();

const href = React.useMemo<string>(() => {
if (typeof to === 'object') {
return `${to.pathname || pathname}${to.search || ''}${to.hash || ''}`;
}
return to;
}, [to]);

const handleClick = (e: MouseEvent<HTMLAnchorElement>) => {
props.onClick?.(e);
if (!href?.startsWith('http')) {
// Should support open in new tab
if (!e.metaKey && !e.ctrlKey && !e.shiftKey) {
e.preventDefault();
startTransition(() => {
if (href) {
navigate(href);
}
});
const Link = forwardRef<HTMLAnchorElement, React.PropsWithChildren<LinkProps>>(
({ component, children, to, ...rest }, ref) => {
const { pathname } = useLocation();
const { preloadRoute } = useAppData();
const navigate = useNavigate();
const href = useMemo<string>(() => {
if (typeof to === 'object') {
return `${to.pathname || pathname}${to.search || ''}${to.hash || ''}`;
}
return to;
}, [to]);
const onClick = (e: MouseEvent<HTMLAnchorElement>) => {
rest.onClick?.(e);
if (!href?.startsWith('http')) {
// Should support open in new tab
if (!e.metaKey && !e.ctrlKey && !e.shiftKey) {
e.preventDefault();
navigate(href);
}
}
};
if (component) {
return React.createElement(
component,
{
...rest,
ref,
href,
onClick,
onMouseEnter: () => preloadRoute?.(href),
},
children,
);
}
};

useLayoutEffect(() => {
if (isPending) {
nprogress.start();
} else {
nprogress.done();
}
}, [isPending]);

if (component) {
return React.createElement(
component,
{
...rest,
ref,
onClick: handleClick,
href,
},
children,
return (
<DumiLink ref={ref} {...rest} to={href} prefetch>
{children}
</DumiLink>
);
}

return (
<DumiLink ref={ref} onClick={handleClick} {...rest} to={href} prefetch>
{children}
</DumiLink>
);
});
},
);

export default Link;
3 changes: 2 additions & 1 deletion .dumi/theme/slots/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {
import { TinyColor } from '@ctrl/tinycolor';
import { createStyles } from 'antd-style';
import getAlphaColor from 'antd/es/theme/util/getAlphaColor';
import { FormattedMessage, Link } from 'dumi';
import { FormattedMessage } from 'dumi';
import RcFooter from 'rc-footer';
import type { FooterColumn } from 'rc-footer/lib/column';
import React, { useContext } from 'react';

import useLocale from '../../../hooks/useLocale';
import useLocation from '../../../hooks/useLocation';
import Link from '../../../theme/common/Link';
import SiteContext from '../SiteContext';
import AdditionalInfo from './AdditionalInfo';

Expand Down
4 changes: 4 additions & 0 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { version } from './package.json';

export default defineConfig({
plugins: ['dumi-plugin-color-chunk'],

// For <Link prefetch />
routePrefetch: {},
manifest: {},

conventionRoutes: {
// to avoid generate routes for .dumi/pages/index/components/xx
exclude: [/index\/components\//],
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@
"@types/markdown-it": "^14.1.2",
"@types/minimist": "^1.2.5",
"@types/node": "^22.5.5",
"@types/nprogress": "^0.2.3",
"@types/ora": "^3.2.0",
"@types/pixelmatch": "^5.2.6",
"@types/pngjs": "^6.0.5",
Expand Down Expand Up @@ -211,7 +210,6 @@
"mockdate": "^3.0.5",
"node-fetch": "^3.3.2",
"node-notifier": "^10.0.1",
"nprogress": "^0.2.0",
"open": "^10.1.0",
"ora": "^8.1.0",
"pixelmatch": "^6.0.0",
Expand Down

0 comments on commit 9c2bf53

Please sign in to comment.