fix: back to prefetch

This commit is contained in:
ner committed 2024-10-29 18:25:52 +02:00
1 parent a268f7c38d
commit c9ca262da8
14 files changed
+178 -25

No files matched your search

+8 -2
View File
@@ -1,10 +1,16 @@
import { FC } from 'react';
import Random from '@/components/Random';
import Trending from '@/components/Trending';
const Container = () => (
interface Props {
page: number;
}
const Container: FC<Props> = ({ page }) => (
<div className='flex flex-1 flex-col'>
<div className='xs:mb-4'>
<Random />
<Random page={page} />
<div className='h-2 w-full' />
</div>
<div className='hidden w-full rounded-lg xs:block'>
+20 -2
View File
@@ -1,14 +1,32 @@
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { Metadata } from 'next';
import UsersList from '@/components/UsersLists';
import { session } from '@/data';
import { session as getSession } from '@/data';
import usersShowsQuery from '@/data/queries/usersShows';
import { TITLE } from '@/env';
import getQueryClient from '@/lib/queryClient';
export const metadata: Metadata = {
title: `My Favorite Shows - ${TITLE}`,
description: `View your favorite shows on ${TITLE}. Keep track of films you’ve added and revisit your top picks anytime.`
};
const Favorites = async () => <UsersList session={await session()} list='favorite' label='favorites' />;
const Favorites = async () => {
const session = await getSession();
const queryClient = getQueryClient();
const list = 'favorite';
if (session) {
queryClient.prefetchInfiniteQuery(usersShowsQuery({ showType: 'movie', session, list }));
}
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<UsersList session={session} list={list} label='favorites' />
</HydrationBoundary>
);
};
export default Favorites;
+18 -1
View File
@@ -1,5 +1,22 @@
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import randomQuery, { generatePage } from '@/data/queries/random';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
const Random = () => <Container />;
const Random = () => {
const queryClient = getQueryClient();
const page = generatePage();
queryClient.prefetchInfiniteQuery(randomQuery({ page }));
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container page={page} />
</HydrationBoundary>
);
};
export default Random;
+14 -1
View File
@@ -1,6 +1,9 @@
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { Metadata } from 'next';
import popularQuery from '@/data/queries/popular';
import { TITLE } from '@/env';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
@@ -9,6 +12,16 @@ export const metadata: Metadata = {
description: `Check out the most popular movies right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`
};
const Popular = () => <Container />;
const Popular = () => {
const queryClient = getQueryClient();
queryClient.prefetchInfiniteQuery(popularQuery());
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
};
export default Popular;
+23 -1
View File
@@ -1,5 +1,27 @@
import { FC } from 'react';
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import similarQuery from '@/data/queries/similar';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
const Similar = () => <Container />;
interface Props {
searchParams: { id: string; type: Show['type'] };
}
const Similar: FC<Props> = ({ searchParams }) => {
const queryClient = getQueryClient();
const { id, type } = searchParams || {};
queryClient.prefetchInfiniteQuery(similarQuery({ showId: id, showType: type }));
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
};
export default Similar;
+28 -1
View File
@@ -1,5 +1,32 @@
import { FC } from 'react';
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import popularQuery from '@/data/queries/popular';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
const Starring = () => <Container />;
interface Props {
searchParams: Record<string, string>;
}
const Starring: FC<Props> = ({ searchParams }) => {
const queryClient = getQueryClient();
const { actorId } = searchParams || {};
queryClient.prefetchInfiniteQuery(
popularQuery({
sortBy: 'vote_average.desc',
starring: actorId
})
);
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
};
export default Starring;
+14 -1
View File
@@ -1,6 +1,9 @@
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { Metadata } from 'next';
import popularQuery from '@/data/queries/popular';
import { TITLE } from '@/env';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
@@ -9,6 +12,16 @@ export const metadata: Metadata = {
description: `Discover the top-rated movies on ${TITLE}. Find the highest-rated films and make informed viewing choices.`
};
const Top = () => <Container />;
const Top = () => {
const queryClient = getQueryClient();
queryClient.prefetchInfiniteQuery(popularQuery({ sortBy: 'vote_average.desc' }));
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
};
export default Top;
+20 -2
View File
@@ -1,14 +1,32 @@
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { Metadata } from 'next';
import UsersList from '@/components/UsersLists';
import { session } from '@/data';
import { session as getSession, session } from '@/data';
import usersShowsQuery from '@/data/queries/usersShows';
import { TITLE } from '@/env';
import getQueryClient from '@/lib/queryClient';
export const metadata: Metadata = {
title: `My watch list - ${TITLE}`,
description: `View your favorite shows on ${TITLE}. Keep track of films you’ve added and revisit your top picks anytime.`
};
const WatchList = async () => <UsersList session={await session()} list='watchlist' label='watch' />;
const WatchList = async () => {
const session = await getSession();
const queryClient = getQueryClient();
const list = 'watchlist';
if (session) {
queryClient.prefetchInfiniteQuery(usersShowsQuery({ showType: 'movie', session, list }));
}
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<UsersList session={session} list={list} label='watch' />
</HydrationBoundary>
);
};
export default WatchList;
+8 -2
View File
@@ -1,12 +1,18 @@
'use client';
import { FC } from 'react';
import Preview from '../Preview';
import Carousel from './components/Carousel';
import useRandomMovie from './useRandomMovie';
const Random = () => {
const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie();
interface Props {
page: number;
}
const Random: FC<Props> = ({ page }) => {
const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie({ page });
return (
<Preview
+6 -2
View File
@@ -10,10 +10,14 @@ const indexAtom = atom(0);
export const useSilentIndex = () => getDefaultStore().get(indexAtom);
const useRandomMovie = () => {
interface Props {
page: number;
}
const useRandomMovie = ({ page }: Props) => {
const [index, setIndex] = useAtom(indexAtom);
const { shows: movies, fetchNextPage } = useInfiniteList(randomQuery());
const { shows: movies, fetchNextPage } = useInfiniteList(randomQuery({ page }));
if (movies.length && !movies[index]) {
setIndex(0);
+9 -4
View File
@@ -7,18 +7,23 @@ const TOTAL_PAGES = 400;
export const generatePage = createUniqueRandomGenerator(TOTAL_PAGES);
const randomQuery = () => ({
export type RandomQueryProps = {
page: number;
};
const randomQuery = ({ page }: RandomQueryProps) => ({
suspense: true,
queryKey: ['random-movie'],
initialPageParam: '1',
queryFn: () =>
initialPageParam: page.toString(),
getNextPageParam: () => generatePage().toString(),
queryFn: ({ pageParam }: { pageParam: string }) =>
movieAdvisor
.get<TMDBPagination<Movie>>('/discover/movie', {
params: {
'vote_count.gte': 300,
sort_by: 'popularity.desc',
with_origin_country: 'UA|GB|JP|AU|US|IT|DE|FR',
page: generatePage()
page: pageParam
}
})
.then(mapMoviesSeriesResponseToShows)
+8 -4
View File
@@ -6,21 +6,25 @@ interface Props {
queryKey: QueryKey;
enabled?: boolean;
queryFn: ({ pageParam }: { pageParam: string }) => Promise<Pagination<Show>>;
initialPageParam?: string;
getNextPageParam?: () => string | undefined;
}
const useInfiniteList = ({ queryKey, queryFn, enabled, suspense }: Props) => {
const useInfiniteList = ({ queryKey, queryFn, enabled, suspense, initialPageParam, getNextPageParam }: Props) => {
const { data, hasNextPage, fetchNextPage, isFetched, isLoading } = (
suspense ? useSuspenseInfiniteQuery : useInfiniteQuery
)({
enabled,
queryKey,
queryFn: ({ pageParam }) => queryFn({ pageParam }),
getNextPageParam(pagination) {
getNextPageParam:
getNextPageParam ||
((pagination) => {
if (!pagination || pagination.page === pagination.total) return undefined;
return `${pagination.page + 1}`;
},
initialPageParam: '1'
}),
initialPageParam: initialPageParam ?? '1'
});
const shows = useMemo(() => {
+1 -1
View File
@@ -4,7 +4,7 @@ const makeQueryClient = () => {
return new QueryClient({
defaultOptions: {
queries: {
staleTime: Infinity,
staleTime: 3600 * 1000,
refetchOnReconnect: false,
refetchOnMount: false,
refetchOnWindowFocus: false
+1 -1
View File
@@ -1 +1 @@
if(!self.define){let e,a={};const s=(s,n)=>(s=new URL(s+".js",n).href,a[s]||new Promise((a=>{if("document"in self){const e=document.createElement("script");e.src=s,e.onload=a,document.head.appendChild(e)}else e=s,importScripts(s),a()})).then((()=>{let e=a[s];if(!e)throw new Error(`Module ${s} didn’t register its module`);return e})));self.define=(n,t)=>{const i=e||("document"in self?document.currentScript.src:"")||location.href;if(a[i])return;let c={};const r=e=>s(e,i),o={module:{uri:i},exports:c,require:r};a[i]=Promise.all(n.map((e=>o[e]||r(e)))).then((e=>(t(...e),c)))}}define(["./workbox-4754cb34"],(function(e){"use strict";importScripts(),self.skipWaiting(),e.clientsClaim(),e.precacheAndRoute([{url:"/_next/app-build-manifest.json",revision:"2a57774e1ed6331c1395fd1ea2d7b22c"},{url:"/_next/static/chunks/132-9c5907bb6e658bd4.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/152.362c71763f5f1192.js",revision:"362c71763f5f1192"},{url:"/_next/static/chunks/23-e0259c2123520d87.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/26.286553a5ff2812fc.js",revision:"286553a5ff2812fc"},{url:"/_next/static/chunks/276.c3c1130f2adade69.js",revision:"c3c1130f2adade69"},{url:"/_next/static/chunks/344.05938a041227e582.js",revision:"05938a041227e582"},{url:"/_next/static/chunks/383.78d47f7582340f53.js",revision:"78d47f7582340f53"},{url:"/_next/static/chunks/460.ac20021c7496f1d6.js",revision:"ac20021c7496f1d6"},{url:"/_next/static/chunks/497.48f1dfd129122432.js",revision:"48f1dfd129122432"},{url:"/_next/static/chunks/584-87bcccb137b07dd4.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/61.e269d4a699fba0b6.js",revision:"e269d4a699fba0b6"},{url:"/_next/static/chunks/617-ef0cc8cd639c8a92.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/621-fa4d2ff1cf645813.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/673.362c29137dd3e968.js",revision:"362c29137dd3e968"},{url:"/_next/static/chunks/695.156d21364de6e419.js",revision:"156d21364de6e419"},{url:"/_next/static/chunks/755.5cd959eac5682527.js",revision:"5cd959eac5682527"},{url:"/_next/static/chunks/792.b51012549074b93f.js",revision:"b51012549074b93f"},{url:"/_next/static/chunks/810.6105e84414768b0d.js",revision:"6105e84414768b0d"},{url:"/_next/static/chunks/922.9ce4bf0553cfb724.js",revision:"9ce4bf0553cfb724"},{url:"/_next/static/chunks/app/(site)/favorites/page-d41cd989d3aca261.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/loading-96645736797edde7.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/page-b696c31e16ee2a97.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/popular/page-8f35320a3ee7322d.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/similar/page-6c052528908c0b81.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/starring/page-7f395dbf442510bd.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/top/page-8b739e20efddb8bf.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/(site)/watch-list/page-0e517d3ae10f5b63.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/_not-found/page-97bde446d7f1c840.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/app/layout-951f602929e02aa5.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/c8bcbd21.63daf74955ffea8b.js",revision:"63daf74955ffea8b"},{url:"/_next/static/chunks/fd9d1056-d1448f0cd0a3dc26.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/framework-f66176bb897dc684.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/main-a339d49e77d6dbd3.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/main-app-51cdaff1a81e6b49.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/pages/_app-6a626577ffa902a4.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/pages/_error-1be831200e60c5c0.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/chunks/polyfills-42372ed130431b0a.js",revision:"846118c33b2c0e922d7b3a7676f81f6f"},{url:"/_next/static/chunks/webpack-cc221c8a98e29fdd.js",revision:"mwtiyaWB8nTaaSMQ6ajGA"},{url:"/_next/static/css/0d5f6452b2682b9b.css",revision:"0d5f6452b2682b9b"},{url:"/_next/static/css/de5e7a9d0c8570d7.css",revision:"de5e7a9d0c8570d7"},{url:"/_next/static/media/a9b1453781eac9e6-s.p.woff2",revision:"c754cf60aba699077f00948285bf54b5"},{url:"/_next/static/media/dd843ebd3ff03195-s.p.woff2",revision:"333341dc4117f45b7fcee6b5223a56db"},{url:"/_next/static/media/e9ecf55e71a874cd-s.woff2",revision:"62348655e2d5baed97a39f0590491d09"},{url:"/_next/static/mwtiyaWB8nTaaSMQ6ajGA/_buildManifest.js",revision:"2ec694eb52ae4f523f265a46bae4d768"},{url:"/_next/static/mwtiyaWB8nTaaSMQ6ajGA/_ssgManifest.js",revision:"b6652df95db52feb4daf4eca35380933"},{url:"/apple-touch-icon.png",revision:"6ed7ad78b222698b2872b5a7f104fe08"},{url:"/favicon-48x48.png",revision:"d2db6eb5ce8ce1da631142Line truncated
if(!self.define){let e,s={};const n=(n,c)=>(n=new URL(n+".js",c).href,s[n]||new Promise((s=>{if("document"in self){const e=document.createElement("script");e.src=n,e.onload=s,document.head.appendChild(e)}else e=n,importScripts(n),s()})).then((()=>{let e=s[n];if(!e)throw new Error(`Module ${n} didn’t register its module`);return e})));self.define=(c,t)=>{const i=e||("document"in self?document.currentScript.src:"")||location.href;if(s[i])return;let a={};const f=e=>n(e,i),r={module:{uri:i},exports:a,require:f};s[i]=Promise.all(c.map((e=>r[e]||f(e)))).then((e=>(t(...e),a)))}}define(["./workbox-4754cb34"],(function(e){"use strict";importScripts(),self.skipWaiting(),e.clientsClaim(),e.precacheAndRoute([{url:"/_next/app-build-manifest.json",revision:"80761954100f0b6a7bab0d9ae3d0c3b6"},{url:"/_next/static/BpfE9Xvc9qCfRFOyDYgsh/_buildManifest.js",revision:"2ec694eb52ae4f523f265a46bae4d768"},{url:"/_next/static/BpfE9Xvc9qCfRFOyDYgsh/_ssgManifest.js",revision:"b6652df95db52feb4daf4eca35380933"},{url:"/_next/static/chunks/132-f8dec67d40375666.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/152.b96372f7a5392cef.js",revision:"b96372f7a5392cef"},{url:"/_next/static/chunks/209-af7fe75ba3786fd1.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/23-e0259c2123520d87.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/26.286553a5ff2812fc.js",revision:"286553a5ff2812fc"},{url:"/_next/static/chunks/261-53daa415084fcdbe.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/276.c3c1130f2adade69.js",revision:"c3c1130f2adade69"},{url:"/_next/static/chunks/344.05938a041227e582.js",revision:"05938a041227e582"},{url:"/_next/static/chunks/383.78d47f7582340f53.js",revision:"78d47f7582340f53"},{url:"/_next/static/chunks/460.ac20021c7496f1d6.js",revision:"ac20021c7496f1d6"},{url:"/_next/static/chunks/497.48f1dfd129122432.js",revision:"48f1dfd129122432"},{url:"/_next/static/chunks/61.e269d4a699fba0b6.js",revision:"e269d4a699fba0b6"},{url:"/_next/static/chunks/621-fa4d2ff1cf645813.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/673.362c29137dd3e968.js",revision:"362c29137dd3e968"},{url:"/_next/static/chunks/680-c4b2a24305eaa584.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/695.156d21364de6e419.js",revision:"156d21364de6e419"},{url:"/_next/static/chunks/755.5cd959eac5682527.js",revision:"5cd959eac5682527"},{url:"/_next/static/chunks/792.b51012549074b93f.js",revision:"b51012549074b93f"},{url:"/_next/static/chunks/810.6105e84414768b0d.js",revision:"6105e84414768b0d"},{url:"/_next/static/chunks/922.9ce4bf0553cfb724.js",revision:"9ce4bf0553cfb724"},{url:"/_next/static/chunks/app/(site)/favorites/page-964a24e9ccc42f58.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/loading-96645736797edde7.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/page-4a80ee2d1b5eb1be.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/popular/page-34f5dc8e221f0fe9.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/similar/page-971819b71cfe3acc.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/starring/page-c125517f46fc2e39.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/top/page-3f8b273dfc73f0e7.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/(site)/watch-list/page-e65d1fdadc60de11.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/_not-found/page-97bde446d7f1c840.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/app/layout-e0c515cb893afbfd.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/c8bcbd21.63daf74955ffea8b.js",revision:"63daf74955ffea8b"},{url:"/_next/static/chunks/fd9d1056-d1448f0cd0a3dc26.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/framework-f66176bb897dc684.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/main-a339d49e77d6dbd3.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/main-app-51cdaff1a81e6b49.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/pages/_app-6a626577ffa902a4.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/pages/_error-1be831200e60c5c0.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/chunks/polyfills-42372ed130431b0a.js",revision:"846118c33b2c0e922d7b3a7676f81f6f"},{url:"/_next/static/chunks/webpack-2727b4b69a6c8e6c.js",revision:"BpfE9Xvc9qCfRFOyDYgsh"},{url:"/_next/static/css/0d5f6452b2682b9b.css",revision:"0d5f6452b2682b9b"},{url:"/_next/static/css/de5e7a9d0c8570d7.css",revision:"de5e7a9d0c8570d7"},{url:"/_next/static/media/a9b1453781eac9e6-s.p.woff2",revision:"c754cf60aba699077f00948285bf54b5"},{url:"/_next/static/media/dd843ebd3ff03195-s.p.woff2",revision:"333341dc4117f45b7fcee6b5223a56db"},{url:"/_next/static/media/e9ecf55e71a874cd-s.woff2",revision:"62348655e2d5baed97a39f0590491d09"},{url:"/apple-touch-icon.png",revision:"6ed7ad7Line truncated