Skip to content

Commit 338f9f5

Browse files
committed
feat: 部分链接预渲染
1 parent d2485cf commit 338f9f5

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

src/app/(app)/projects/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use client';
22

3-
import Link from 'next/link';
43
import Image from 'next/image';
54

65
import { GitHubBrandIcon } from '@/components/icons/platform/GitHubBrandIcon';
76
import { cn } from '@/lib/helper';
87
import { ProjectModel, projectList } from '~/index';
8+
import { PrefetchLink } from '@/components/modules/shared/PrefetchLink';
99

1010
export default function Projects() {
1111
return (
@@ -41,7 +41,7 @@ const ProjectCardList = ({ data }: { data: ProjectModel[] }) => (
4141
);
4242
const ProjectCard = ({ project }: { project: ProjectModel }) => {
4343
return (
44-
<Link
44+
<PrefetchLink
4545
href={project.url}
4646
key={project.id}
4747
className="group flex shrink-0 grid-cols-[60px_2fr] flex-col items-center gap-4 md:grid"
@@ -59,7 +59,7 @@ const ProjectCard = ({ project }: { project: ProjectModel }) => {
5959
{project.desc}
6060
</span>
6161
</span>
62-
</Link>
62+
</PrefetchLink>
6363
);
6464
};
6565

src/components/layout/Footer/Footer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Link from 'next/link';
22

3+
import { PrefetchLink } from '@/components/modules/shared/PrefetchLink';
34
import ThemeSwitcher from '@/components/ui/ThemeSwitcher';
45

56
const Footer = () => {
@@ -12,11 +13,11 @@ const Footer = () => {
1213
<div className="relative mx-auto max-w-7xl lg:px-8 h-full flex flex-row items-center justify-around">
1314
<div className=" flex items-center gap-x-4 font-mono">
1415
<span className="">
15-
<Link href={'/'}>
16+
<PrefetchLink href={'/'}>
1617
<span className=" relative before:content-[''] before:absolute before:bottom-[-2px] before:w-[0px] hover:before:w-[100%] before:h-[1px] before:bg-[var(--accent-color)] before:transition-all before:duration-300">
1718
关于我
1819
</span>
19-
</Link>
20+
</PrefetchLink>
2021
</span>
2122
<span className="">
2223
<Link href={'/'}>

src/components/layout/Header/HeaderCenterContent.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import { LayoutGroup, m, useMotionTemplate, useMotionValue } from 'framer-motion';
44
import React, { memo } from 'react';
55
import { usePathname, useParams } from 'next/navigation';
6-
import Link from 'next/link';
76

87
import { MenuPopover } from './MenuPopover';
98

109
import { cn } from '@/lib/helper';
1110
import { IHeaderMenu, headerMenuConfig } from '~/router';
11+
import { PrefetchLink } from '@/components/modules/shared/PrefetchLink';
1212

1313
export const HeaderCenterContent = () => {
1414
const params = useParams();
@@ -137,7 +137,7 @@ function AnimatedItem({
137137
}) {
138138
return (
139139
<div>
140-
<Link
140+
<PrefetchLink
141141
href={href}
142142
className={cn(
143143
'relative block whitespace-nowrap px-4 py-2 transition',
@@ -156,7 +156,7 @@ function AnimatedItem({
156156
layoutId="active-nav-item"
157157
/>
158158
)}
159-
</Link>
159+
</PrefetchLink>
160160
</div>
161161
);
162162
}

src/components/modules/list/PostItem.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import Link from 'next/link';
21
import { memo } from 'react';
32

43
import { PostItemHoverOverlay } from './PostItemHoverOverlay';
4+
import { PrefetchLink } from '../shared/PrefetchLink';
55

66
import dayjs from '@/lib/dayjs';
77
import { MdiClockOutline } from '@/components/icons/clock';
@@ -12,7 +12,10 @@ export const PostItem = memo<{ data: PostItemType }>(function PostItem({ data })
1212
const postLink = `/notes/${data.path}`;
1313

1414
return (
15-
<Link href={postLink} className="relative flex flex-col py-8 focus-visible:!shadow-none gap-2">
15+
<PrefetchLink
16+
href={postLink}
17+
className="relative flex flex-col py-8 focus-visible:!shadow-none gap-2"
18+
>
1619
<PostItemHoverOverlay />
1720
<h2 className="relative text-balance break-words text-2xl font-medium">{data.title}</h2>
1821

@@ -27,6 +30,6 @@ export const PostItem = memo<{ data: PostItemType }>(function PostItem({ data })
2730
<p>{data.tag}</p>
2831
</span>
2932
</div>
30-
</Link>
33+
</PrefetchLink>
3134
);
3235
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Link from 'next/link';
2+
import { useState, useCallback } from 'react';
3+
4+
interface PrefetchLinkProps extends React.ComponentProps<typeof Link> {
5+
href: string;
6+
}
7+
8+
export const PrefetchLink: React.FC<PrefetchLinkProps> = ({ href, children, ...props }) => {
9+
const [prefetch, setPrefetch] = useState(false);
10+
11+
const handleMouseEnter = useCallback(() => {
12+
setPrefetch(true);
13+
}, []);
14+
15+
return (
16+
<Link href={href} prefetch={prefetch} onMouseEnter={handleMouseEnter} {...props}>
17+
{children}
18+
</Link>
19+
);
20+
};

0 commit comments

Comments
 (0)