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

refactor(react-query-next-experimental): use isServer to determine server environment #7536

Merged
merged 9 commits into from
Jun 8, 2024
16 changes: 12 additions & 4 deletions docs/framework/react/guides/advanced-ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ The first step of any React Query setup is always to create a `queryClient` and
'use client'

// Since QueryClientProvider relies on useContext under the hood, we have to put 'use client' on top
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClient,
QueryClientProvider,
isServer,
} from '@tanstack/react-query'

function makeQueryClient() {
return new QueryClient({
Expand All @@ -47,7 +51,7 @@ function makeQueryClient() {
let browserQueryClient: QueryClient | undefined = undefined

function getQueryClient() {
if (typeof window === 'undefined') {
if (isServer) {
// Server: always make a new query client
return makeQueryClient()
} else {
Expand Down Expand Up @@ -441,7 +445,11 @@ To achieve this, wrap your app in the `ReactQueryStreamedHydration` component:
// app/providers.tsx
'use client'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClient,
QueryClientProvider,
isServer,
} from '@tanstack/react-query'
import * as React from 'react'
import { ReactQueryStreamedHydration } from '@tanstack/react-query-next-experimental'

Expand All @@ -460,7 +468,7 @@ function makeQueryClient() {
let browserQueryClient: QueryClient | undefined = undefined

function getQueryClient() {
if (typeof window === 'undefined') {
if (isServer) {
// Server: always make a new query client
return makeQueryClient()
} else {
Expand Down
8 changes: 6 additions & 2 deletions docs/framework/react/guides/suspense.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ To achieve this, wrap your app in the `ReactQueryStreamedHydration` component:
// app/providers.tsx
'use client'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClient,
QueryClientProvider,
isServer,
} from '@tanstack/react-query'
import * as React from 'react'
import { ReactQueryStreamedHydration } from '@tanstack/react-query-next-experimental'

Expand All @@ -137,7 +141,7 @@ function makeQueryClient() {
let browserQueryClient: QueryClient | undefined = undefined

function getQueryClient() {
if (typeof window === 'undefined') {
if (isServer) {
// Server: always make a new query client
return makeQueryClient()
} else {
Expand Down
8 changes: 6 additions & 2 deletions examples/react/nextjs-app-prefetching/app/get-query-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { QueryClient, defaultShouldDehydrateQuery } from '@tanstack/react-query'
import {
QueryClient,
defaultShouldDehydrateQuery,
isServer,
} from '@tanstack/react-query'

function makeQueryClient() {
return new QueryClient({
Expand All @@ -19,7 +23,7 @@ function makeQueryClient() {
let browserQueryClient: QueryClient | undefined = undefined

export function getQueryClient() {
if (typeof window === 'undefined') {
if (isServer) {
// Server: always make a new query client
return makeQueryClient()
} else {
Expand Down
4 changes: 2 additions & 2 deletions examples/react/nextjs-suspense-streaming/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use client'
import { useSuspenseQuery } from '@tanstack/react-query'
import { isServer, useSuspenseQuery } from '@tanstack/react-query'
import { Suspense } from 'react'

export const runtime = 'edge' // 'nodejs' (default) | 'edge'

function getBaseURL() {
if (typeof window !== 'undefined') {
if (!isServer) {
return ''
}
if (process.env.VERCEL_URL) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use client'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import {
QueryClient,
QueryClientProvider,
isServer,
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import * as React from 'react'
import { ReactQueryStreamedHydration } from '@tanstack/react-query-next-experimental'
Expand All @@ -18,7 +22,7 @@ function makeQueryClient() {
let browserQueryClient: QueryClient | undefined = undefined

function getQueryClient() {
if (typeof window === 'undefined') {
if (isServer) {
return makeQueryClient()
} else {
if (!browserQueryClient) browserQueryClient = makeQueryClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function createHydrationStreamProvider<TShape>() {

// <server stuff>
const [stream] = React.useState<Array<TShape>>(() => {
if (typeof window !== 'undefined') {
if (!isServer) {
return {
push() {
// no-op on the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
defaultShouldDehydrateQuery,
dehydrate,
hydrate,
isServer,
useQueryClient,
} from '@tanstack/react-query'
import * as React from 'react'
Expand Down Expand Up @@ -40,7 +41,7 @@ export function ReactQueryStreamedHydration(props: {
const [trackedKeys] = React.useState(() => new Set<string>())

// <server only>
if (typeof window === 'undefined') {
if (isServer) {
// Do we need to care about unsubscribing? I don't think so to be honest
queryClient.getQueryCache().subscribe((event) => {
switch (event.type) {
Expand Down
Loading