diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index 8303082f12..545a798ea8 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -74,7 +74,7 @@ export interface BlitzConfig extends Record { target?: string experimental?: { isomorphicResolverImports?: boolean - reactMode?: string + reactRoot?: boolean } cli?: { clearConsoleOnBlitzDev?: boolean diff --git a/packages/core/src/blitz-data.tsx b/packages/core/src/blitz-data.tsx index 400441aab4..6b9e895290 100644 --- a/packages/core/src/blitz-data.tsx +++ b/packages/core/src/blitz-data.tsx @@ -12,7 +12,7 @@ export function _getBlitzRuntimeData(): BlitzRuntimeData { const config = getConfig() return { sessionCookiePrefix: (config._meta.packageName || "blitz").replace(/[^a-zA-Z0-9-_]/g, "_"), - suspenseEnabled: config.experimental?.reactMode !== "legacy", + suspenseEnabled: config.experimental?.reactRoot !== false, } } diff --git a/packages/core/src/use-query-hooks.ts b/packages/core/src/use-query-hooks.ts index 5b497be874..a2187cb9ee 100644 --- a/packages/core/src/use-query-hooks.ts +++ b/packages/core/src/use-query-hooks.ts @@ -7,9 +7,10 @@ import { UseQueryResult, } from "react-query" import {useSession} from "./auth/auth-client" +import {getBlitzRuntimeData} from "./blitz-data" import {useRouter} from "./router" import {FirstParam, PromiseReturnType, QueryFn} from "./types" -import {isClient} from "./utils" +import {isServer} from "./utils" import { emptyQueryFn, getQueryCacheFunctions, @@ -49,14 +50,15 @@ export function useQuery>( throw new Error("useQuery is missing the first argument - it must be a query function") } - const suspense = - options?.enabled === false || options?.enabled === null ? false : options?.suspense + const suspenseConfig = getBlitzRuntimeData().suspenseEnabled + let enabled = isServer && suspenseConfig ? false : options?.enabled ?? options?.enabled !== null + const suspense = enabled === false ? false : options?.suspense const session = useSession({suspense}) if (session.isLoading) { - options.enabled = false + enabled = false } - const routerIsReady = useRouter().isReady && isClient + const routerIsReady = useRouter().isReady || (isServer && suspenseConfig) const enhancedResolverRpcClient = sanitizeQuery(queryFn) const queryKey = getQueryKey(queryFn, params) @@ -66,8 +68,20 @@ export function useQuery>( ? () => enhancedResolverRpcClient(params, {fromQueryHook: true}) : (emptyQueryFn as any), ...options, + enabled, }) + if ( + queryRest.isIdle && + isServer && + suspenseConfig !== false && + !data && + (!options || !("suspense" in options) || options.suspense) && + (!options || !("enabled" in options) || options.enabled) + ) { + throw new Promise(() => {}) + } + const rest = { ...queryRest, ...getQueryCacheFunctions, TResult, T>(queryFn, params), diff --git a/test/integration/no-suspense/blitz.config.js b/test/integration/no-suspense/blitz.config.js index e7f11da192..d26118ef79 100644 --- a/test/integration/no-suspense/blitz.config.js +++ b/test/integration/no-suspense/blitz.config.js @@ -1,5 +1,5 @@ module.exports = { experimental: { - reactMode: "legacy", + reactRoot: false, }, }