Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data prefetching #189

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading