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
+95 -54

No files matched your search

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