Skip to content

Commit 8cc8435

Browse files
perf: optimize overview page loading (#15)
* perf: reduce query and disable prefetch * perf: reduce query in creating profiles * chore: bump version to 1.0.0
1 parent 9b46c70 commit 8cc8435

File tree

8 files changed

+43
-29
lines changed

8 files changed

+43
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prosopon",
3-
"version": "0.4.0",
3+
"version": "1.0.0",
44
"type": "module",
55
"scripts": {
66
"dev": "next dev",

src/app/(main)/(dashboard)/(overview)/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Announcement from "@/components/announcement";
55
import Instructions from "@/components/instructions";
66
import { checkAuth } from "@/lib/auth";
77
import { redirect } from "next/navigation";
8-
import { findUserByIdWithProfilesAndTextures } from "@/queries/user";
8+
import { findUserByIdWithResourceCount } from "@/queries/user";
99
import { Metadata } from "next";
1010
import { SITE_NAME, YGG_API_PREFIX } from "@/lib/constants";
1111

@@ -17,7 +17,7 @@ export default async function OverviewPage() {
1717
const currentAuth = await checkAuth(false);
1818
if (!currentAuth || currentAuth.error || !currentAuth.id) redirect("/login");
1919

20-
const user = await findUserByIdWithProfilesAndTextures(currentAuth.id!);
20+
const user = await findUserByIdWithResourceCount(currentAuth.id!);
2121

2222
return (
2323
<>
@@ -28,8 +28,8 @@ export default async function OverviewPage() {
2828
<AccountConfig verified={user?.verified} />
2929
<h3 className="text-lg">我的资源</h3>
3030
<Resources
31-
profiles={user?.profiles}
32-
closet={user?.closet}
31+
profiles={user?._count.profiles}
32+
closet={user?._count.closet}
3333
isAdmin={user?.role === "ADMIN"}
3434
/>
3535
</div>

src/components/account-config.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Link from "next/link";
33

44
export default function AccountConfig({ verified }) {
55
return (
6-
<Link href="account">
6+
<Link prefetch={false} href={{ pathname: "account" }}>
77
<div className="group border rounded-md h-12 p-3 my-4 flex bg-background hover:bg-accent transition-colors duration-300 ease-out">
88
{verified ? (
99
<BadgeCheck

src/components/resources-empty.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function ResourcesEmpty() {
1414
<p className="text-muted-foreground text-center break-all max-w-60 my-1.5">
1515
创建角色, 上传材质和披风
1616
</p>
17-
<Link href="profile">
17+
<Link prefetch={false} href={{ pathname: "profile" }}>
1818
<Button className="p-1 rounded-sm h-9 w-30 bg-foreground my-4 text-background hover:bg-gray-800">
1919
创建角色
2020
</Button>

src/components/resources.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MAX_PROFILES } from "@/lib/constants";
66
export default function Resources({ profiles, closet, isAdmin }) {
77
return (
88
<>
9-
{profiles.length === 0 && closet.length === 0 ? (
9+
{profiles === 0 && closet === 0 ? (
1010
<div className="border rounded-md md:min-h-50 p-5 my-4 bg-background">
1111
<ResourcesEmpty />
1212
</div>
@@ -18,13 +18,17 @@ export default function Resources({ profiles, closet, isAdmin }) {
1818
<span className="text-lg font-bold">角色</span>
1919
</div>
2020
<p className="text-7xl w-full text-center my-13">
21-
{profiles.length}
21+
{profiles}
2222
<span className="text-2xl text-muted-foreground">
2323
/{isAdmin ? "\u221e" : MAX_PROFILES}
2424
</span>
2525
</p>
2626
<div className="w-full flex flex-col-reverse flex-auto">
27-
<Link href="profile" className="hover:text-sky-700 text-lg text-center">
27+
<Link
28+
prefetch={false}
29+
href={{ pathname: "profile" }}
30+
className="hover:text-sky-700 text-lg text-center"
31+
>
2832
管理角色
2933
<ChevronRight className="inline" />
3034
</Link>
@@ -36,11 +40,15 @@ export default function Resources({ profiles, closet, isAdmin }) {
3640
<span className="text-lg font-bold">材质</span>
3741
</div>
3842
<p className="text-7xl w-full text-center my-13">
39-
{closet.length}
43+
{closet}
4044
<span className="text-2xl text-muted-foreground">/&infin;</span>
4145
</p>
4246
<div className="w-full flex flex-col-reverse flex-auto">
43-
<Link href="closet" className="hover:text-sky-700 text-lg text-center">
47+
<Link
48+
prefetch={false}
49+
href={{ pathname: "closet" }}
50+
className="hover:text-sky-700 text-lg text-center"
51+
>
4452
管理材质
4553
<ChevronRight className="inline" />
4654
</Link>

src/components/sidebar-entry.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22

33
import { SidebarMenuButton, SidebarMenuItem } from "@/components/ui/sidebar";
44
import Link from "next/link";
5-
import {
6-
Boxes,
7-
Compass,
8-
File,
9-
Settings,
10-
Shirt,
11-
Smile,
12-
TableOfContents,
13-
UserRound,
14-
} from "lucide-react";
5+
import { Compass, Shirt, Smile, TableOfContents, UserRound } from "lucide-react";
156
import { usePathname } from "next/dist/client/components/navigation";
167
import { ENTRIES } from "@/lib/constants";
178

@@ -28,7 +19,7 @@ export default function SidebarEntry({ entry }) {
2819
pathname[0] === entry.id || ("/" + pathname[0] === ENTRIES.dashboard.id && isOverview)
2920
}
3021
>
31-
<Link href={"/" + (isOverview ? "" : entry.url)}>
22+
<Link prefetch={false} href={"/" + (isOverview ? "" : entry.url)}>
3223
{entry.id === "overview" && <Compass />}
3324
{entry.id === "profile" && <Smile />}
3425
{entry.id === "closet" && <Shirt />}

src/lib/actions/profile.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function createProfile(data: z.infer<typeof createProfileParam>) {
1616
if (user.role !== "ADMIN") {
1717
const profiles = await prisma.profile.findMany({
1818
where: { userId: user.id },
19+
select: { id: true },
1920
});
2021

2122
if (profiles.length >= MAX_PROFILES) {
@@ -29,12 +30,6 @@ export async function createProfile(data: z.infer<typeof createProfileParam>) {
2930
}
3031
const { name } = validated.data;
3132

32-
const existing = await prisma.profile.findUnique({
33-
where: { name },
34-
});
35-
36-
if (existing) return { success: false, message: "Profile already exists" };
37-
3833
const SITE_NAMESPACE = uuidv5(SITE_DOMAIN, uuidv5.DNS);
3934

4035
const id = createId();

src/queries/user.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ export async function findUserById(id: string) {
1313
});
1414
}
1515

16+
export async function findUserByIdWithResourceCount(id: string) {
17+
return prisma.user.findUnique({
18+
where: { id },
19+
select: {
20+
id: true,
21+
email: true,
22+
name: true,
23+
role: true,
24+
verified: true,
25+
createdAt: true,
26+
_count: {
27+
select: {
28+
closet: true,
29+
profiles: true,
30+
},
31+
},
32+
},
33+
});
34+
}
35+
1636
export async function findUserByIdWithProfilesAndTextures(id: string) {
1737
return prisma.user.findUnique({
1838
where: { id },

0 commit comments

Comments
 (0)