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

[legacy-framework] allow useQuery to run during SSR or SSG #1940

Merged
merged 6 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface BlitzConfig extends Record<string, unknown> {
target?: string
experimental?: {
isomorphicResolverImports?: boolean
reactMode?: string
reactRoot?: boolean
}
cli?: {
clearConsoleOnBlitzDev?: boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/blitz-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
24 changes: 19 additions & 5 deletions packages/core/src/use-query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -49,14 +50,15 @@ export function useQuery<T extends QueryFn, TResult = PromiseReturnType<T>>(
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)

Expand All @@ -66,8 +68,20 @@ export function useQuery<T extends QueryFn, TResult = PromiseReturnType<T>>(
? () => 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<FirstParam<T>, TResult, T>(queryFn, params),
Expand Down
2 changes: 1 addition & 1 deletion test/integration/no-suspense/blitz.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
experimental: {
reactMode: "legacy",
reactRoot: false,
},
}