Skip to content

Commit 0e7d502

Browse files
committed
more prefetching
1 parent 37a1136 commit 0e7d502

File tree

28 files changed

+132
-80
lines changed

28 files changed

+132
-80
lines changed

apps/1-vite-componentdidmount/src/AppLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function AppLayout({ children }: AppLayoutProps) {
2828
return (
2929
<AppShell
3030
header={{ height: 60 }}
31-
navbar={{ width: 300, breakpoint: "sm", collapsed: { mobile: !opened } }}
31+
navbar={{ width: 200, breakpoint: "sm", collapsed: { mobile: !opened } }}
3232
padding="xl"
3333
>
3434
<AppShell.Header>

apps/2-vite-useeffect/src/AppLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function AppLayout({ children }: AppLayoutProps) {
5555
return (
5656
<AppShell
5757
header={{ height: 60 }}
58-
navbar={{ width: 300, breakpoint: "sm", collapsed: { mobile: !opened } }}
58+
navbar={{ width: 200, breakpoint: "sm", collapsed: { mobile: !opened } }}
5959
padding="xl"
6060
>
6161
<AppShell.Header>

apps/2-vite-useeffect/src/pages/PostPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const PostPage = () => {
3939
const [isErrorLoadingComments, setIsErrorLoadingComments] = useState(false);
4040
const [isDeletingComment, setIsDeletingComment] = useState(false);
4141
const [deletingCommentId, setDeletingCommentId] = useState<number | null>(
42-
null
42+
null,
4343
);
4444
const [isPostingComment, setIsPostingComment] = useState(false);
4545

@@ -85,7 +85,7 @@ export const PostPage = () => {
8585
}
8686
try {
8787
const fetchUrl = new URL(
88-
`http://localhost:3333/posts/${postId}/comments`
88+
`http://localhost:3333/posts/${postId}/comments`,
8989
);
9090
const response = await fetch(fetchUrl.href);
9191
const fetchedComments = (await response.json()) as IComment[];

apps/2-vite-useeffect/src/pages/UserPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const UserPage = () => {
9494
<Text>Email: {user?.email}</Text>
9595
<Text>Phone: {user?.phone}</Text>
9696
<Text>Website: {user?.website}</Text>
97-
<Text>
97+
<Text component="div">
9898
Address:
9999
<address>
100100
{user?.address.street}, {user?.address.suite},<br />

apps/2-vite-useeffect/src/pages/UsersPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const UsersPage = () => {
6363
),
6464
},
6565
],
66-
[]
66+
[],
6767
);
6868

6969
return (

apps/3-vite-reactquery/src/AppLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function AppLayout({ children }: AppLayoutProps) {
5555
return (
5656
<AppShell
5757
header={{ height: 60 }}
58-
navbar={{ width: 300, breakpoint: "sm", collapsed: { mobile: !opened } }}
58+
navbar={{ width: 200, breakpoint: "sm", collapsed: { mobile: !opened } }}
5959
padding="xl"
6060
>
6161
<AppShell.Header>

apps/3-vite-reactquery/src/pages/PostPage.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const PostPage = () => {
4747
queryKey: ["users", post?.userId],
4848
queryFn: async () => {
4949
const response = await fetch(
50-
`http://localhost:3333/users/${post?.userId}`
50+
`http://localhost:3333/users/${post?.userId}`,
5151
);
5252
return response.json() as Promise<IUser>;
5353
},
@@ -64,7 +64,7 @@ export const PostPage = () => {
6464
queryKey: ["posts", postId, "comments"],
6565
queryFn: async () => {
6666
const response = await fetch(
67-
`http://localhost:3333/posts/${postId}/comments`
67+
`http://localhost:3333/posts/${postId}/comments`,
6868
);
6969
return response.json() as Promise<IComment[]>;
7070
},
@@ -82,7 +82,7 @@ export const PostPage = () => {
8282
`http://localhost:3333/comments/${commentId}`,
8383
{
8484
method: "DELETE",
85-
}
85+
},
8686
);
8787
return response.json() as Promise<IComment>;
8888
},
@@ -91,7 +91,7 @@ export const PostPage = () => {
9191
onError: (err, commentId) => {
9292
console.error(
9393
`Error deleting comment ${commentId}. Rolling UI back`,
94-
err
94+
err,
9595
);
9696
alert("Error deleting comment");
9797
},
@@ -133,7 +133,7 @@ export const PostPage = () => {
133133
// Optimistically update to the new value
134134
queryClient.setQueryData(
135135
["posts", postId, "comments"],
136-
(oldComments: any) => [...oldComments, newComment]
136+
(oldComments: any) => [...oldComments, newComment],
137137
);
138138

139139
// Return a context object with the snapshot value
@@ -144,7 +144,7 @@ export const PostPage = () => {
144144
onError: (err, _newComment, context) => {
145145
queryClient.setQueryData(
146146
["posts", postId, "comments"],
147-
context?.previousComments
147+
context?.previousComments,
148148
);
149149
console.error("Error posting comment. Rolling UI back", err);
150150
},

apps/3-vite-reactquery/src/pages/UserPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const UserPage = () => {
7070
<Text>Email: {user?.email}</Text>
7171
<Text>Phone: {user?.phone}</Text>
7272
<Text>Website: {user?.website}</Text>
73-
<Text>
73+
<Text component="div">
7474
Address:
7575
<address>
7676
{user?.address.street}, {user?.address.suite},<br />

apps/3.1-vite-reactquery-refactor/src/AppLayout.tsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,34 @@ import { useDisclosure } from "@mantine/hooks";
1010
import { IconHome, IconUsersGroup } from "@tabler/icons-react";
1111
import { useMemo } from "react";
1212
import { Link, useLocation } from "react-router-dom";
13+
import { prefetchUsers } from "./hooks/useGetUsers";
14+
import { useQueryClient } from "@tanstack/react-query";
15+
import { prefetchPosts } from "./hooks/useGetPosts";
1316

1417
interface AppLayoutProps {
1518
children: React.ReactNode;
1619
}
1720

18-
const links = [
19-
{
20-
icon: <IconHome size="1rem" />,
21-
color: "blue",
22-
label: "Home Feed",
23-
href: "/",
24-
},
25-
{
26-
icon: <IconUsersGroup size="1rem" />,
27-
color: "teal",
28-
label: "Users",
29-
href: "/users",
30-
},
31-
];
32-
3321
export function AppLayout({ children }: AppLayoutProps) {
3422
const { pathname } = useLocation();
23+
const queryClient = useQueryClient();
24+
25+
const links = [
26+
{
27+
icon: <IconHome size="1rem" />,
28+
color: "blue",
29+
label: "Home Feed",
30+
href: "/",
31+
prefetch: () => prefetchPosts(queryClient),
32+
},
33+
{
34+
icon: <IconUsersGroup size="1rem" />,
35+
color: "teal",
36+
label: "Users",
37+
href: "/users",
38+
prefetch: () => prefetchUsers(queryClient),
39+
},
40+
];
3541

3642
const breadCrumbLinks = useMemo(() => {
3743
const routes = pathname.split("/");
@@ -55,7 +61,7 @@ export function AppLayout({ children }: AppLayoutProps) {
5561
return (
5662
<AppShell
5763
header={{ height: 60 }}
58-
navbar={{ width: 300, breakpoint: "sm", collapsed: { mobile: !opened } }}
64+
navbar={{ width: 200, breakpoint: "sm", collapsed: { mobile: !opened } }}
5965
padding="xl"
6066
>
6167
<AppShell.Header>
@@ -66,7 +72,12 @@ export function AppLayout({ children }: AppLayoutProps) {
6672
</AppShell.Header>
6773
<AppShell.Navbar p="md">
6874
{links.map((link) => (
69-
<Anchor component={Link} key={link.label} to={link.href}>
75+
<Anchor
76+
onMouseEnter={link.prefetch}
77+
component={Link}
78+
key={link.label}
79+
to={link.href}
80+
>
7081
{link.label}
7182
</Anchor>
7283
))}

apps/3.1-vite-reactquery-refactor/src/AppProviders.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { theme } from "./theme";
77
const ReactQueryDevtoolsProduction = lazy(() =>
88
import("@tanstack/react-query-devtools/production").then((d) => ({
99
default: d.ReactQueryDevtools,
10-
}))
10+
})),
1111
);
1212

1313
const queryClient = new QueryClient({

0 commit comments

Comments
 (0)