Skip to content

Commit

Permalink
feat: add ssr using tanstack query
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Nov 21, 2024
1 parent 42fb878 commit 42785f7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@radix-ui/react-switch": "^1.1.1",
"@t3-oss/env-nextjs": "^0.11.1",
"@tanstack/react-query": "^5.60.2",
"@tanstack/react-query-devtools": "^5.61.0",
"autoprefixer": "^10.4.20",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app/explorer/components/InfiniteScrollGuilds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const InfiniteScrollGuilds = () => {

const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } =
useInfiniteQuery({
queryKey: ["guilds", search],
queryKey: ["guilds", search || ""],
queryFn: fetchGuilds,
initialPageParam: 1,
enabled: search !== undefined,
Expand Down
23 changes: 21 additions & 2 deletions src/app/explorer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { Card } from "@/components/ui/Card";
import { Skeleton } from "@/components/ui/Skeleton";
import { env } from "@/lib/env";
import { Plus, SignIn } from "@phosphor-icons/react/dist/ssr";
import {
HydrationBoundary,
QueryClient,
dehydrate,
} from "@tanstack/react-query";
import { Suspense } from "react";
import { GuildCard } from "./components/GuildCard";
import { InfiniteScrollGuilds } from "./components/InfiniteScrollGuilds";
Expand All @@ -26,7 +31,19 @@ const GuildCardSkeleton = () => {
);
};

export default function Explorer() {
export default async function Explorer() {
const queryClient = new QueryClient();
await queryClient.prefetchInfiniteQuery({
queryKey: ["guilds", ""],
initialPageParam: 1,
queryFn: async ({ pageParam }: { pageParam: number }) =>
(
await fetch(
`${env.NEXT_PUBLIC_API}/guild/search?page=${pageParam}&pageSize=${24}&search=`,
)
).json(),
});

return (
<main className="container mx-auto grid max-w-screen-lg gap-8 px-4 py-8">
<section className="pt-6 pb-8">
Expand All @@ -39,7 +56,9 @@ export default function Explorer() {
<Search />
</section>

<InfiniteScrollGuilds />
<HydrationBoundary state={dehydrate(queryClient)}>
<InfiniteScrollGuilds />
</HydrationBoundary>
</main>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { wagmiConfig } from "@/config/wagmi";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { Provider as JotaiProvider } from "jotai";
import { ThemeProvider } from "next-themes";
import type { FunctionComponent, PropsWithChildren } from "react";
Expand All @@ -23,6 +24,7 @@ export const Providers: FunctionComponent<PropsWithChildren> = ({
<WagmiProvider config={wagmiConfig}>
<QueryClientProvider client={queryClient}>
{children}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</WagmiProvider>
</ThemeProvider>
Expand Down

0 comments on commit 42785f7

Please sign in to comment.