File size: 1,058 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { headers } from 'next/headers'
import React from 'react'
import { HydrationBoundary, dehydrate } from '@tanstack/react-query'
import { Temporal } from '@js-temporal/polyfill'
import { ClientComponent } from './client-component'
import { makeQueryClient } from './make-query-client'
import { queryExampleAction } from './_action'

export default function Home() {
  const queryClient = makeQueryClient()

  queryClient.prefetchQuery({
    queryKey: ['data'],
    queryFn: async () => {
      const { count } = await (
        await fetch('http://localhost:3000/count', {
          headers: await headers(),
        })
      ).json()

      return {
        text: 'data from server',
        date: Temporal.PlainDate.from('2024-01-01'),
        count,
      }
    },
  })

  const state = dehydrate(queryClient)

  return (
    <main>
      <HydrationBoundary state={state}>
        <ClientComponent />
      </HydrationBoundary>
      <form action={queryExampleAction}>
        <button type="submit">Increment</button>
      </form>
    </main>
  )
}