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

feat: use prefetch in the home page #699

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion app/all-collections/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dehydrate } from '@tanstack/react-query';

import { Suspense } from 'react';
import { dehydrate } from 'react-query/core';

import Hydrate from '../../src/components/HydrateClient';
import Wrapper from '../../src/components/common/Wrapper';
Expand Down
12 changes: 1 addition & 11 deletions app/collections/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
// import { useRouter } from 'next/navigation';
// import { useEffect } from 'react';
// import ReactGA from 'react-ga4';
// import { hasAcceptedCookies } from '@graasp/sdk';
// import { ENV, UrlSearch } from '../src/config/constants';
// import createEmotionCache from '../src/config/createEmotionCache';
// import { GA_MEASUREMENT_ID, NODE_ENV } from '../src/config/env';
// Client-side cache, shared for the whole session of the user in the browser.
// const clientSideEmotionCache = createEmotionCache();
// import { Api, DATA_KEYS, configureQueryClient } from '@graasp/query-client';
import { dehydrate } from 'react-query/core';
import { dehydrate } from '@tanstack/react-query';

import Hydrate from '../../../src/components/HydrateClient';
import Collection from '../../../src/components/collection/Collection';
Expand Down
2 changes: 1 addition & 1 deletion app/liked/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dehydrate } from 'react-query/core';
import { dehydrate } from '@tanstack/react-query';

import Hydrate from '../../src/components/HydrateClient';
import Wrapper from '../../src/components/common/Wrapper';
Expand Down
2 changes: 1 addition & 1 deletion app/members/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dehydrate } from 'react-query/core';
import { dehydrate } from '@tanstack/react-query';

import Hydrate from '../../../src/components/HydrateClient';
import Wrapper from '../../../src/components/common/Wrapper';
Expand Down
3 changes: 2 additions & 1 deletion app/oer/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dehydrate } from '@tanstack/react-query';

import { Suspense } from 'react';
import { dehydrate } from 'react-query/core';

import Hydrate from '../../src/components/HydrateClient';
import Wrapper from '../../src/components/common/Wrapper';
Expand Down
28 changes: 24 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { dehydrate } from '@tanstack/react-query';
import { Metadata } from 'next';

import { dehydrate } from 'react-query/core';
import { configureQueryClient } from '@graasp/query-client';

import Hydrate from '../src/components/HydrateClient';
import Wrapper from '../src/components/common/Wrapper';
import Home from '../src/components/pages/Home';
import { HOMEPAGE_NB_ELEMENTS_TO_SHOW } from '../src/config/constants';
import getQueryClient from '../src/config/get-query-client';
import { QUERY_CLIENT_OPTIONS } from '../src/config/queryClient';
import LIBRARY from '../src/langs/constants';
import en from '../src/langs/en.json';
import { buildSeo } from './seo';
Expand All @@ -28,10 +31,27 @@ export async function generateMetadata(): Promise<Metadata> {
}

const Page = async () => {
const { queryOptions } = configureQueryClient(QUERY_CLIENT_OPTIONS);
const queryClient = getQueryClient();
// await queryClient.prefetchQuery(['items', 'collections', 'all'], () =>
// Api.getAllPublishedItems({}, { API_HOST: GRAASP_API_HOST, axios }),
// );

await Promise.all([
// TODO: uncomment when prefetch works correctly.
// queryClient.prefetchQuery(
// queryOptions.publishedItemsForMemberOptions(GRAASPER_ID),
// ),
queryClient.prefetchQuery(
queryOptions.mostLikedPublishedItemsOptions({
limit: HOMEPAGE_NB_ELEMENTS_TO_SHOW,
}),
),
// TODO: Error: Hydration failed because the initial UI does not match what was rendered on the server.
// queryClient.prefetchQuery(
// queryOptions.mostRecentPublishedItemsOptions({
// limit: HOMEPAGE_NB_ELEMENTS_TO_SHOW,
// }),
// ),
]);

const dehydratedState = dehydrate(queryClient);

return (
Expand Down
7 changes: 3 additions & 4 deletions app/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use client';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';

// import { ReactQueryDevtools } from "react-query-devtools";
import { useEffect, useState } from 'react';
import ReactGA from 'react-ga4';
import { QueryClient, QueryClientProvider } from 'react-query';
import { ToastContainer } from 'react-toastify';

import { hasAcceptedCookies } from '@graasp/sdk';
Expand All @@ -15,7 +14,8 @@ import { GA_MEASUREMENT_ID, NODE_ENV } from '../src/config/env';

// eslint-disable-next-line react/function-component-definition
export default function Providers(props: { children: React.ReactNode }) {
const [client] = useState(new QueryClient());
// We pass a function to call it only once at initial rendering (see lazy initialization for more).
const [client] = useState(() => new QueryClient());
const pathname = usePathname();
const { replace } = useRouter();
const searchParams = useSearchParams();
Expand Down Expand Up @@ -53,7 +53,6 @@ export default function Providers(props: { children: React.ReactNode }) {
return (
<QueryClientProvider client={client}>
{children}
{/* <ReactQueryDevtools initialIsOpen={false} /> */}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we enable the devtools with ssr ? I think it would be nice :)

<ToastContainer stacked theme="colored" />
</QueryClientProvider>
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@emotion/react": "11.13.0",
"@emotion/server": "11.11.0",
"@emotion/styled": "11.13.0",
"@graasp/query-client": "3.26.0",
"@graasp/query-client": "github:graasp/graasp-query-client#947-prefetch-home-page",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reminder

"@graasp/sdk": "4.31.0",
"@graasp/stylis-plugin-rtl": "2.2.0",
"@graasp/translations": "1.35.0",
Expand All @@ -42,6 +42,7 @@
"@mui/lab": "5.0.0-alpha.170",
"@mui/material": "5.16.5",
"@sentry/nextjs": "8.26.0",
"@tanstack/react-query": "5.59.0",
"@testing-library/jest-dom": "6.4.8",
"@testing-library/react": "16.0.0",
"@testing-library/user-event": "14.5.2",
Expand Down Expand Up @@ -101,7 +102,6 @@
"husky": "9.1.4",
"nyc": "17.0.0",
"prettier": "3.3.3",
"react-query": "3.39.3",
"standard-version": "9.5.0",
"typescript": "5.5.4",
"wait-on": "7.2.0"
Expand Down
7 changes: 5 additions & 2 deletions src/components/HydrateClient.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
'use client';

import { HydrateProps, Hydrate as RQHydrate } from 'react-query';
import {
HydrationBoundaryProps,
HydrationBoundary as RQHydrate,
} from '@tanstack/react-query';

const Hydrate = (props: HydrateProps) => {
const Hydrate = (props: HydrationBoundaryProps) => {
// eslint-disable-next-line react/jsx-props-no-spreading
return <RQHydrate {...props} />;
};
Expand Down
6 changes: 4 additions & 2 deletions src/components/QueryClientContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { DehydratedState } from '@tanstack/react-query';

import React from 'react';
import type { DehydratedState } from 'react-query';

import { configureQueryClient } from '@graasp/query-client';

import notifier from '../config/notifier';
import { QUERY_CLIENT_OPTIONS } from '../config/queryClient';

const QueryClientContext = React.createContext(
Expand All @@ -14,7 +16,7 @@ type Props = {
};

const QueryClientProvider = ({ children, dehydratedState }: Props) => {
const value = configureQueryClient(QUERY_CLIENT_OPTIONS);
const value = configureQueryClient({ ...QUERY_CLIENT_OPTIONS, notifier });
const {
QueryClientProvider: Provider,
queryClient,
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/Wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';

import { ErrorBoundary } from '@sentry/nextjs';
import type { DehydratedState } from '@tanstack/react-query';

import { Trans } from 'react-i18next';
import type { DehydratedState } from 'react-query';
import 'react-toastify/dist/ReactToastify.css';

import { Box, Stack, SxProps } from '@mui/material';
Expand Down
23 changes: 15 additions & 8 deletions src/components/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use client';

import { useQuery } from '@tanstack/react-query';

import { useContext } from 'react';

import { Box } from '@mui/material';
Expand All @@ -24,20 +26,25 @@ import StyledBackgroundContainer from '../layout/StyledBackgroundContainer';
const Home = () => {
const { t } = useLibraryTranslation();

const { hooks } = useContext(QueryClientContext);
const { queryOptions } = useContext(QueryClientContext);

const { data: graasperCollections, isLoading: isGraasperCollectionsLoading } =
hooks.usePublishedItemsForMember(GRAASPER_ID);
useQuery(queryOptions.publishedItemsForMemberOptions(GRAASPER_ID));

const {
data: mostLikedCollections,
isLoading: isMostLikedCollectionsLoading,
} = hooks.useMostLikedPublishedItems({
limit: HOMEPAGE_NB_ELEMENTS_TO_SHOW,
});
const { data: recentCollections, isLoading: isMostRecentLoading } =
hooks.useMostRecentPublishedItems({
} = useQuery(
queryOptions.mostLikedPublishedItemsOptions({
limit: HOMEPAGE_NB_ELEMENTS_TO_SHOW,
}),
);

const { data: recentCollections, isLoading: isMostRecentLoading } = useQuery(
queryOptions.mostRecentPublishedItemsOptions({
limit: HOMEPAGE_NB_ELEMENTS_TO_SHOW,
});
}),
);

return (
<MainWrapper>
Expand Down
3 changes: 2 additions & 1 deletion src/config/get-query-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { QueryClient } from '@tanstack/react-query';

import { cache } from 'react';
import { QueryClient } from 'react-query/core';

const getQueryClient = cache(() => new QueryClient());
export default getQueryClient;
4 changes: 1 addition & 3 deletions src/config/queryClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DOMAIN, GRAASP_API_HOST } from './env';
import notifier from './notifier';

// eslint-disable-next-line import/prefer-default-export
export const QUERY_CLIENT_OPTIONS = {
Expand All @@ -11,10 +10,9 @@ export const QUERY_CLIENT_OPTIONS = {
refetchOnMount: false,
// avoid refetching when same data are closely fetched
staleTime: 1000, // ms
cacheTime: 1000, // ms
gcTime: 1000, // ms
},
DOMAIN,
notifier,
SHOW_NOTIFICATIONS: true,
WS_HOST: `${GRAASP_API_HOST?.replace('http', 'ws')}/ws`,
};
Loading