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

ENG-4634 feat(repo): adds initial data hydration pattern to template playground routes #885

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Changes from 1 commit
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
Next Next commit
Updates the template provider to add hydration config and adds a play…
…ground-hydration example for the initialData hydration pattern
jonathanprozzi committed Nov 5, 2024
commit 22a4124a376e61c2e7ab504d41471f077a370971
32 changes: 21 additions & 11 deletions apps/template/app/.client/providers.tsx
Original file line number Diff line number Diff line change
@@ -2,9 +2,20 @@ import { wagmiConfig } from '@lib/utils/wagmi'
import type { PrivyClientConfig } from '@privy-io/react-auth'
import { PrivyProvider } from '@privy-io/react-auth'
import { WagmiProvider } from '@privy-io/wagmi'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
HydrationBoundary,
QueryClient,
QueryClientProvider,
} from '@tanstack/react-query'

const queryClient = new QueryClient()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
// Disable automatic refetching on window focus for SSR
refetchOnWindowFocus: false,
},
},
})

const privyConfig: PrivyClientConfig = {
embeddedWallets: {
@@ -22,21 +33,20 @@ const privyConfig: PrivyClientConfig = {
export default function Providers({
privyAppId,
children,
dehydratedState,
}: {
privyAppId: string
children: React.ReactNode
dehydratedState?: unknown
}) {
return (
<PrivyProvider
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
appId={privyAppId as string}
config={privyConfig}
>
<PrivyProvider appId={privyAppId as string} config={privyConfig}>
<QueryClientProvider client={queryClient}>
<WagmiProvider config={wagmiConfig} reconnectOnMount={false}>
{children}
</WagmiProvider>
<HydrationBoundary state={dehydratedState}>
<WagmiProvider config={wagmiConfig} reconnectOnMount={false}>
{children}
</WagmiProvider>
</HydrationBoundary>
</QueryClientProvider>
</PrivyProvider>
)
58 changes: 58 additions & 0 deletions apps/template/app/routes/app+/playground-hydration.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {
fetcher,
GetAtomsDocument,
GetAtomsQuery,
GetAtomsQueryVariables,
useGetAtomsQuery,
} from '@0xintuition/graphql'

import { json } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
import {
dehydrate,
HydrationBoundary,
QueryClient,
} from '@tanstack/react-query'

export async function loader() {
const queryClient = new QueryClient()

console.log('🟡 Server: Starting prefetch')
await queryClient.prefetchQuery({
queryKey: ['get-atoms-query'],
queryFn: () =>
fetcher<GetAtomsQuery, GetAtomsQueryVariables>(GetAtomsDocument, {})(),
})
console.log('🟢 Server: Prefetch complete')

return json({
dehydratedState: dehydrate(queryClient),
})
}

function Atoms() {
const { data: atomsData } = useGetAtomsQuery(
{},
{
queryKey: ['get-atoms-query'],
},
)

return (
<div>
<h1>Playground Hydration</h1>
<pre>{JSON.stringify(atomsData?.atoms || [], null, 2)}</pre>
</div>
)
}

export default function PlaygroundHydration() {
console.log('🔵 Client: Component rendering')
const { dehydratedState } = useLoaderData<typeof loader>()

return (
<HydrationBoundary state={dehydratedState}>
<Atoms />
</HydrationBoundary>
)
}