Skip to content

Commit

Permalink
refactor(react-query-next-experimental): use isServer to determine se…
Browse files Browse the repository at this point in the history
…rver environment (#7536)

* docs: use isServer on ssr guides

* refactor: use isServer on nextjs examples

* refactor(react-query-next-experimental): use isServer instead of typeof window

* chore(docs): fix prettier

---------

Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
  • Loading branch information
stakbucks and TkDodo committed Jun 8, 2024
1 parent dd4ee0a commit 4e1a04b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
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 {
isServer,
QueryClient,
QueryClientProvider,
} 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 {
isServer,
QueryClient,
QueryClientProvider,
} 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 {
isServer,
QueryClient,
QueryClientProvider,
} 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

0 comments on commit 4e1a04b

Please sign in to comment.