Skip to content

Commit

Permalink
add data prefetching
Browse files Browse the repository at this point in the history
  • Loading branch information
alan2207 committed Aug 3, 2024
1 parent 1508d6d commit 83512ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { useQueryClient } from '@tanstack/react-query';

import { Link } from '@/components/ui/link';
import { Spinner } from '@/components/ui/spinner';
import { Table } from '@/components/ui/table';
import { formatDate } from '@/utils/format';

import { getDiscussionQueryOptions } from '../api/get-discussion';
import { useDiscussions } from '../api/get-discussions';

import { DeleteDiscussion } from './delete-discussion';

export const DiscussionsList = () => {
const discussionsQuery = useDiscussions();
const queryClient = useQueryClient();

if (discussionsQuery.isLoading) {
return (
Expand Down Expand Up @@ -39,7 +43,17 @@ export const DiscussionsList = () => {
title: '',
field: 'id',
Cell({ entry: { id } }) {
return <Link to={`./${id}`}>View</Link>;
return (
<Link
onMouseEnter={() => {
// Prefetch the discussion data when the user hovers over the link
queryClient.prefetchQuery(getDiscussionQueryOptions(id));
}}
to={`./${id}`}
>
View
</Link>
);
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions docs/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,9 @@ Use `srcset` to load the most optimal image for the clients screen size.
### Web vitals

Since Google started taking web vitals in account when indexing websites, you should keep an eye on web vitals scores from [Lighthouse](https://web.dev/measure/) and [Pagespeed Insights](https://pagespeed.web.dev/).

### Data prefetching

It is possible to prefetch data before the user navigates to a page. This can be done by using the `queryClient.prefetchQuery` method from the `@tanstack/react-query` library. This method allows you to prefetch data for a specific query. This can be useful when you know that the user will navigate to a specific page and you want to prefetch the data before the user navigates to the page. This can help to improve the performance of the application by reducing the time it takes to load the data when the user navigates to the page.

[Data Prefetching Example Code](../apps/react-vite/src/features/discussions/components/discussions-list.tsx)

0 comments on commit 83512ea

Please sign in to comment.