fix: cache

This commit is contained in:
ner committed 2024-03-20 20:00:38 +02:00
1 parent 7f77f960d9
commit 095f7361fa
8 files changed
+85 -44

No files matched your search

+3 -5
View File
@@ -5,20 +5,18 @@ import getQueryClient from '@/lib/queryClient';
import Container from './container';
export default async function Popular() {
const queryClient = getQueryClient();
await queryClient.prefetchInfiniteQuery({
queryClient.prefetchInfiniteQuery({
queryKey: ['popular'],
queryFn: () => popularMovies({}),
queryFn: () => popularMovies(),
initialPageParam: '1'
});
export default async function Popular() {
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
}
export const revalidate = 3600;
+3 -5
View File
@@ -5,20 +5,18 @@ import getQueryClient from '@/lib/queryClient';
import Container from './container';
export default async function Top() {
const queryClient = getQueryClient();
await queryClient.prefetchInfiniteQuery({
queryClient.prefetchInfiniteQuery({
queryKey: ['top'],
queryFn: () => topMovies({}),
queryFn: () => topMovies(),
initialPageParam: '1'
});
export default async function Top() {
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
}
export const revalidate = 3600;
+4 -5
View File
@@ -1,4 +1,5 @@
import React, { ReactNode } from 'react';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import type { Metadata } from 'next';
import { cookies } from 'next/headers';
@@ -18,16 +19,14 @@ export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang='en' className={theme}>
<head>
<meta
name='viewport'
content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'
></meta>
<meta name='theme-color' content='black'></meta>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' />
<meta name='theme-color' content='black' />
</head>
<body>
<Providers theme={theme}>
<Header />
<main>{children}</main>
<ReactQueryDevtools initialIsOpen={false} />
</Providers>
</body>
</html>
+5 -2
View File
@@ -29,7 +29,7 @@ export const randomMovies = async ({ filters }: { filters: Partial<Filters> }):
.then((data: unknown) => (data as MovieDBResponse).results);
};
export const popularMovies = async ({ page }: { page?: string }): Promise<MovieDBResponse> => {
export const popularMovies = async ({ page }: { page?: string } = {}): Promise<MovieDBResponse> => {
return server.get('/discover/movie', {
params: {
page,
@@ -43,7 +43,10 @@ export const popularMovies = async ({ page }: { page?: string }): Promise<MovieD
});
};
export const topMovies = async ({ page, starring }: { page?: string; starring?: string }): Promise<MovieDBResponse> => {
export const topMovies = async ({
page,
starring
}: { page?: string; starring?: string } = {}): Promise<MovieDBResponse> => {
return server.get('/discover/movie', {
params: {
page,
+13 -2
View File
@@ -1,5 +1,16 @@
import { cache } from 'react';
import { QueryClient } from '@tanstack/react-query';
import { QueryClient, QueryClientConfig } from '@tanstack/react-query';
const getQueryClient = cache(() => new QueryClient());
export const queryClientOptions: QueryClientConfig = {
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
staleTime: Infinity,
refetchOnReconnect: false,
refetchOnMount: false
}
}
};
const getQueryClient = cache(() => new QueryClient(queryClientOptions));
export default getQueryClient;
+53 -14
View File
@@ -25,6 +25,7 @@
"@radix-ui/react-toggle": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.7",
"@tanstack/react-query": "^5.17.3",
"@tanstack/react-query-devtools": "^5.28.4",
"axios": "^1.4.0",
"class-variance-authority": "^0.7.0",
"classnames": "^2.3.2",
@@ -1973,20 +1974,29 @@
}
},
"node_modules/@tanstack/query-core": {
"version": "5.17.3",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.17.3.tgz",
"integrity": "sha512-QCbOWxifjke/GR1jcDYoaoTZXBTGFivgUMXQ06EvCP7XoEfjzLGMAlPhdHMK+dMR6kQluhzwTQ+CnIGmhrM2JA==",
"version": "5.28.4",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.28.4.tgz",
"integrity": "sha512-uQZqOFqLWUvXNIQZ63XdKzg22NtHzgCBUfDmjDHi3BoF+nUYeBNvMi/xFPtFrMhqRzG2Ir4mYaGsWZzmiEjXpA==",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/query-devtools": {
"version": "5.28.3",
"resolved": "https://registry.npmjs.org/@tanstack/query-devtools/-/query-devtools-5.28.3.tgz",
"integrity": "sha512-Kxch05PVnxLUAyRiz2gXYQhXATHfXbQQwvz858YPjYYQyi7yk8SyS9Z5hyw90bRb0pd3rjelXyGFCsNMWvEghw==",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.17.3",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.17.3.tgz",
"integrity": "sha512-mSRqnP0Rdh9hmJ9mX77xWDiHnu1uGtnNV4w6Crr63+FYUs08iACTv7m3X8Gcl52A6QLBlsLR8zNxTEOTrHejaw==",
"version": "5.28.4",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.28.4.tgz",
"integrity": "sha512-BErcoB/QQG6YwLSUKnaGxF+lSc270RH2w3kMBpG0i4YzDCsFs2pdxPX1WVknQvFk9bNgukMb158hc2Zb4SdwSA==",
"dependencies": {
"@tanstack/query-core": "5.17.3"
"@tanstack/query-core": "5.28.4"
},
"funding": {
"type": "github",
@@ -1996,6 +2006,22 @@
"react": "^18.0.0"
}
},
"node_modules/@tanstack/react-query-devtools": {
"version": "5.28.4",
"resolved": "https://registry.npmjs.org/@tanstack/react-query-devtools/-/react-query-devtools-5.28.4.tgz",
"integrity": "sha512-oS+3INjCIX4Nkh0IAV6LH2mgLqJjkcd/DDxp8dwdWCGloLrz6IBj+bOuuI2wD0zb8r7l45wIAYIhl4Z6XyTupQ==",
"dependencies": {
"@tanstack/query-devtools": "5.28.3"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"@tanstack/react-query": "^5.28.4",
"react": "^18.0.0"
}
},
"node_modules/@types/js-cookie": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/js-cookie/-/js-cookie-3.0.3.tgz",
@@ -8138,16 +8164,29 @@
"requires": {}
},
"@tanstack/query-core": {
"version": "5.17.3",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.17.3.tgz",
"integrity": "sha512-QCbOWxifjke/GR1jcDYoaoTZXBTGFivgUMXQ06EvCP7XoEfjzLGMAlPhdHMK+dMR6kQluhzwTQ+CnIGmhrM2JA=="
"version": "5.28.4",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.28.4.tgz",
"integrity": "sha512-uQZqOFqLWUvXNIQZ63XdKzg22NtHzgCBUfDmjDHi3BoF+nUYeBNvMi/xFPtFrMhqRzG2Ir4mYaGsWZzmiEjXpA=="
},
"@tanstack/query-devtools": {
"version": "5.28.3",
"resolved": "https://registry.npmjs.org/@tanstack/query-devtools/-/query-devtools-5.28.3.tgz",
"integrity": "sha512-Kxch05PVnxLUAyRiz2gXYQhXATHfXbQQwvz858YPjYYQyi7yk8SyS9Z5hyw90bRb0pd3rjelXyGFCsNMWvEghw=="
},
"@tanstack/react-query": {
"version": "5.17.3",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.17.3.tgz",
"integrity": "sha512-mSRqnP0Rdh9hmJ9mX77xWDiHnu1uGtnNV4w6Crr63+FYUs08iACTv7m3X8Gcl52A6QLBlsLR8zNxTEOTrHejaw==",
"version": "5.28.4",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.28.4.tgz",
"integrity": "sha512-BErcoB/QQG6YwLSUKnaGxF+lSc270RH2w3kMBpG0i4YzDCsFs2pdxPX1WVknQvFk9bNgukMb158hc2Zb4SdwSA==",
"requires": {
"@tanstack/query-core": "5.17.3"
"@tanstack/query-core": "5.28.4"
}
},
"@tanstack/react-query-devtools": {
"version": "5.28.4",
"resolved": "https://registry.npmjs.org/@tanstack/react-query-devtools/-/react-query-devtools-5.28.4.tgz",
"integrity": "sha512-oS+3INjCIX4Nkh0IAV6LH2mgLqJjkcd/DDxp8dwdWCGloLrz6IBj+bOuuI2wD0zb8r7l45wIAYIhl4Z6XyTupQ==",
"requires": {
"@tanstack/query-devtools": "5.28.3"
}
},
"@types/js-cookie": {
+1
View File
@@ -26,6 +26,7 @@
"@radix-ui/react-toggle": "^1.0.3",
"@radix-ui/react-tooltip": "^1.0.7",
"@tanstack/react-query": "^5.17.3",
"@tanstack/react-query-devtools": "^5.28.4",
"axios": "^1.4.0",
"class-variance-authority": "^0.7.0",
"classnames": "^2.3.2",
+3 -11
View File
@@ -4,20 +4,12 @@ import { ReactNode, useState } from 'react';
import ModalContainer from 'react-modal-promise';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { queryClientOptions } from '@/lib/queryClient';
import ThemeProvider from '@/providers/Theme';
const Providers = ({ children, theme }: { children: ReactNode; theme?: Theme }) => {
const [queryClient] = useState(
() =>
new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
staleTime: Number.POSITIVE_INFINITY
}
}
})
);
const [queryClient] = useState(() => new QueryClient(queryClientOptions));
return (
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={theme}>{children}</ThemeProvider>