Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(web): align query client ownership (#40024)

yyh890e5aa294af
diff --git a/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx b/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx
index 32d2140401a..f4d9e0e038a 100644
--- a/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx
+++ b/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx
@@ -5,7 +5,7 @@ import { render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
- rootQueryClient: undefined as QueryClient | undefined,
+ queryClient: undefined as QueryClient | undefined,
profileQueryFn: vi.fn(),
workspaceQueryFn: vi.fn(),
workspaceQueryOptions: vi.fn(),
@@ -20,12 +20,9 @@ const mocks = vi.hoisted(() => ({
basePath: '',
}))
-vi.mock('@/context/query-client-server', async (importOriginal) => {
- const actual = await importOriginal<typeof import('@/context/query-client-server')>()
-
+vi.mock('@/app/get-query-client', () => {
return {
- ...actual,
- getQueryClientServer: () => mocks.rootQueryClient,
+ getQueryClient: () => mocks.queryClient,
}
})
@@ -76,7 +73,7 @@ describe('CommonLayoutHydrationBoundary', () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.basePath = ''
- mocks.rootQueryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ mocks.queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
mocks.headers.mockResolvedValue(
new Headers({
'x-dify-pathname': '/apps',
@@ -159,9 +156,6 @@ describe('CommonLayoutHydrationBoundary', () => {
})
it('should dehydrate only Common-owned queries', async () => {
- mocks.rootQueryClient?.setQueryData(['console', 'system-features'], {
- deployment_edition: 'CLOUD',
- })
const { CommonLayoutHydrationBoundary } = await import('../hydration-boundary')
const element = await CommonLayoutHydrationBoundary({ children: null })
diff --git a/web/app/(commonLayout)/datasets/new/__tests__/layout.spec.tsx b/web/app/(commonLayout)/datasets/new/__tests__/layout.spec.tsx
index 256cd2a1c7b..4c21b261638 100644
--- a/web/app/(commonLayout)/datasets/new/__tests__/layout.spec.tsx
+++ b/web/app/(commonLayout)/datasets/new/__tests__/layout.spec.tsx
@@ -9,20 +9,11 @@ const mocks = vi.hoisted(() => ({
systemFeaturesQueryOptions: { queryKey: ['console', 'system-features'] },
}))
-vi.mock('@/context/query-client-server', () => ({
- getQueryClientServer: () => ({
+vi.mock('@/features/system-features/server', () => ({
+ getSystemFeaturesQueryClient: () => ({
ensureQueryData: mocks.ensureQueryData,
}),
-}))
-
-vi.mock('@/service/server', () => ({
- serverConsoleQuery: {
- systemFeatures: {
- get: {
- queryOptions: () => mocks.systemFeaturesQueryOptions,
- },
- },
- },
+ systemFeaturesServerQueryOptions: () => mocks.systemFeaturesQueryOptions,
}))
vi.mock('@/next/navigation', () => ({
diff --git a/web/app/(commonLayout)/datasets/new/layout.tsx b/web/app/(commonLayout)/datasets/new/layout.tsx
index 9df5dcadd6c..4d34b42ff44 100644
--- a/web/app/(commonLayout)/datasets/new/layout.tsx
+++ b/web/app/(commonLayout)/datasets/new/layout.tsx
@@ -1,11 +1,13 @@
import type { ReactNode } from 'react'
-import { getQueryClientServer } from '@/context/query-client-server'
+import {
+ getSystemFeaturesQueryClient,
+ systemFeaturesServerQueryOptions,
+} from '@/features/system-features/server'
import { redirect } from '@/next/navigation'
-import { serverConsoleQuery } from '@/service/server'
export default async function Layout({ children }: { children: ReactNode }) {
- const systemFeatures = await getQueryClientServer().ensureQueryData(
- serverConsoleQuery.systemFeatures.get.queryOptions(),
+ const systemFeatures = await getSystemFeaturesQueryClient().ensureQueryData(
+ systemFeaturesServerQueryOptions(),
)
if (!systemFeatures.knowledge_fs_enabled) redirect('/datasets')
diff --git a/web/app/(commonLayout)/deployments/__tests__/layout.spec.tsx b/web/app/(commonLayout)/deployments/__tests__/layout.spec.tsx
index d609f167574..778496ff659 100644
--- a/web/app/(commonLayout)/deployments/__tests__/layout.spec.tsx
+++ b/web/app/(commonLayout)/deployments/__tests__/layout.spec.tsx
@@ -9,20 +9,11 @@ const mocks = vi.hoisted(() => ({
}),
}))
-vi.mock('@/context/query-client-server', () => ({
- getQueryClientServer: () => ({
+vi.mock('@/features/system-features/server', () => ({
+ getSystemFeaturesQueryClient: () => ({
ensureQueryData: mocks.ensureQueryData,
}),
-}))
-
-vi.mock('@/service/server', () => ({
- serverConsoleQuery: {
- systemFeatures: {
- get: {
- queryOptions: () => mocks.systemFeaturesQueryOptions,
- },
- },
- },
+ systemFeaturesServerQueryOptions: () => mocks.systemFeaturesQueryOptions,
}))
vi.mock('@/features/deployments/deploy-drawer', () => ({
diff --git a/web/app/(commonLayout)/deployments/layout.tsx b/web/app/(commonLayout)/deployments/layout.tsx
index e5f8a29756e..72515f4557e 100644
--- a/web/app/(commonLayout)/deployments/layout.tsx
+++ b/web/app/(commonLayout)/deployments/layout.tsx
@@ -1,12 +1,14 @@
import type { ReactNode } from 'react'
-import { getQueryClientServer } from '@/context/query-client-server'
import { DeployDrawer } from '@/features/deployments/deploy-drawer'
+import {
+ getSystemFeaturesQueryClient,
+ systemFeaturesServerQueryOptions,
+} from '@/features/system-features/server'
import { notFound } from '@/next/navigation'
-import { serverConsoleQuery } from '@/service/server'
export default async function DeploymentsLayout({ children }: { children: ReactNode }) {
- const systemFeatures = await getQueryClientServer().ensureQueryData(
- serverConsoleQuery.systemFeatures.get.queryOptions(),
+ const systemFeatures = await getSystemFeaturesQueryClient().ensureQueryData(
+ systemFeaturesServerQueryOptions(),
)
if (!systemFeatures.enable_app_deploy) notFound()
diff --git a/web/app/(commonLayout)/hydration-boundary.tsx b/web/app/(commonLayout)/hydration-boundary.tsx
index 1737a3374fd..17d5d1e8be9 100644
--- a/web/app/(commonLayout)/hydration-boundary.tsx
+++ b/web/app/(commonLayout)/hydration-boundary.tsx
@@ -1,6 +1,6 @@
import type { ReactNode } from 'react'
import { dehydrate, HydrationBoundary } from '@tanstack/react-query'
-import { makeQueryClient } from '@/context/query-client-server'
+import { getQueryClient } from '@/app/get-query-client'
import { serverUserProfileQueryOptions } from '@/features/account-profile/server'
import { headers } from '@/next/headers'
import { redirect } from '@/next/navigation'
@@ -55,7 +55,7 @@ const handleProfileError = async (error: unknown) => {
}
export async function CommonLayoutHydrationBoundary({ children }: { children: ReactNode }) {
- const queryClient = makeQueryClient()
+ const queryClient = getQueryClient()
const accountProfileUrl = resolveServerConsoleApiUrl(ACCOUNT_PROFILE_PATH)
if (accountProfileUrl) {
diff --git a/web/app/__tests__/get-query-client.spec.ts b/web/app/__tests__/get-query-client.spec.ts
new file mode 100644
index 00000000000..69dffbac941
--- /dev/null
+++ b/web/app/__tests__/get-query-client.spec.ts
@@ -0,0 +1,30 @@
+import { dehydrate } from '@tanstack/react-query'
+import { getQueryClient } from '../get-query-client'
+
+describe('getQueryClient', () => {
+ it('includes pending queries in dehydrated state', async () => {
+ const queryClient = getQueryClient()
+ const queryKey = ['pending-dehydration']
+ let resolveQuery!: (value: string) => void
+ const queryPromise = new Promise<string>((resolve) => {
+ resolveQuery = resolve
+ })
+ const prefetchPromise = queryClient.prefetchQuery({
+ queryKey,
+ queryFn: () => queryPromise,
+ })
+
+ expect(dehydrate(queryClient).queries).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ queryKey,
+ state: expect.objectContaining({ status: 'pending' }),
+ }),
+ ]),
+ )
+
+ resolveQuery('ready')
+ await prefetchPromise
+ queryClient.removeQueries({ queryKey })
+ })
+})
diff --git a/web/app/__tests__/layout.spec.tsx b/web/app/__tests__/layout.spec.tsx
index bfddd400f02..71989fef893 100644
--- a/web/app/__tests__/layout.spec.tsx
+++ b/web/app/__tests__/layout.spec.tsx
@@ -7,8 +7,13 @@ const mocks = vi.hoisted(() => ({
requestHeaders: new Headers(),
}))
-vi.mock('@/context/query-client-server', () => ({
- getQueryClientServer: () => queryClient,
+vi.mock('@/features/system-features/server', () => ({
+ getSystemFeaturesQueryClient: () => queryClient,
+ systemFeaturesServerQueryOptions: () => ({
+ queryKey: ['console', 'system-features'],
+ queryFn: mocks.getSystemFeatures,
+ retry: false,
+ }),
}))
vi.mock('@/env', async (importOriginal) => {
@@ -20,20 +25,6 @@ vi.mock('@/env', async (importOriginal) => {
}
})
-vi.mock('@/service/server', () => ({
- serverConsoleQuery: {
- systemFeatures: {
- get: {
- queryOptions: () => ({
- queryKey: ['console', 'system-features'],
- queryFn: mocks.getSystemFeatures,
- retry: false,
- }),
- },
- },
- },
-}))
-
vi.mock('@/i18n-config/server', () => ({
getLocaleOnServer: async () => 'en-US',
}))
diff --git a/web/app/components/base/analytics-consent/__tests__/cloud-analytics.spec.tsx b/web/app/components/base/analytics-consent/__tests__/cloud-analytics.spec.tsx
index ca421514126..acba0c61f11 100644
--- a/web/app/components/base/analytics-consent/__tests__/cloud-analytics.spec.tsx
+++ b/web/app/components/base/analytics-consent/__tests__/cloud-analytics.spec.tsx
@@ -23,21 +23,12 @@ const { configState, getSystemFeatures, mockHeadersGet, systemFeaturesQueryKey }
}),
)
-vi.mock('@/context/query-client-server', () => ({
- getQueryClientServer: () => queryClient,
-}))
-
-vi.mock('@/service/server', () => ({
- serverConsoleQuery: {
- systemFeatures: {
- get: {
- queryOptions: () => ({
- queryFn: getSystemFeatures,
- queryKey: systemFeaturesQueryKey,
- }),
- },
- },
- },
+vi.mock('@/features/system-features/server', () => ({
+ getSystemFeaturesQueryClient: () => queryClient,
+ systemFeaturesServerQueryOptions: () => ({
+ queryFn: getSystemFeatures,
+ queryKey: systemFeaturesQueryKey,
+ }),
}))
vi.mock('@/config', () => ({
diff --git a/web/app/components/base/analytics-consent/cloud-analytics.tsx b/web/app/components/base/analytics-consent/cloud-analytics.tsx
index f6e9f0e26c1..8e3c0f2ce6f 100644
--- a/web/app/components/base/analytics-consent/cloud-analytics.tsx
+++ b/web/app/components/base/analytics-consent/cloud-analytics.tsx
@@ -1,8 +1,10 @@
import { COOKIEYES_SITE_KEY, IS_PROD, WEB_PREFIX } from '@/config'
-import { getQueryClientServer } from '@/context/query-client-server'
+import {
+ getSystemFeaturesQueryClient,
+ systemFeaturesServerQueryOptions,
+} from '@/features/system-features/server'
import { headers } from '@/next/headers'
import Script from '@/next/script'
-import { serverConsoleQuery } from '@/service/server'
import { GoogleAnalyticsTagScripts, GoogleConsentDefaults } from '../ga'
import { CloudAnalyticsRuntime } from './cloud-analytics-runtime'
import { isCloudAnalyticsRequest } from './request-boundary'
@@ -10,8 +12,8 @@ import { isCloudAnalyticsRequest } from './request-boundary'
const CURRENT_PATHNAME_HEADER = 'x-dify-pathname'
export async function CloudAnalytics() {
- const queryClient = getQueryClientServer()
- const systemFeaturesQuery = serverConsoleQuery.systemFeatures.get.queryOptions()
+ const queryClient = getSystemFeaturesQueryClient()
+ const systemFeaturesQuery = systemFeaturesServerQueryOptions()
const systemFeatures = queryClient.getQueryData(systemFeaturesQuery.queryKey)
if (!systemFeatures) return null
diff --git a/web/app/components/base/zendesk/__tests__/index.spec.tsx b/web/app/components/base/zendesk/__tests__/index.spec.tsx
index ea0cd4abebf..b3131d16d67 100644
--- a/web/app/components/base/zendesk/__tests__/index.spec.tsx
+++ b/web/app/components/base/zendesk/__tests__/index.spec.tsx
@@ -32,22 +32,13 @@ vi.mock('@/config', () => ({
},
}))
-vi.mock('@/context/query-client-server', () => ({
- getQueryClientServer: () => queryClient,
-}))
-
-vi.mock('@/service/server', () => ({
- serverConsoleQuery: {
- systemFeatures: {
- get: {
- queryOptions: vi.fn(() => ({
- queryKey: systemFeaturesQueryKey,
- queryFn: getSystemFeatures,
- retry: false,
- })),
- },
- },
- },
+vi.mock('@/features/system-features/server', () => ({
+ getSystemFeaturesQueryClient: () => queryClient,
+ systemFeaturesServerQueryOptions: vi.fn(() => ({
+ queryKey: systemFeaturesQueryKey,
+ queryFn: getSystemFeatures,
+ retry: false,
+ })),
}))
// Mock next/headers
diff --git a/web/app/components/base/zendesk/index.tsx b/web/app/components/base/zendesk/index.tsx
index 0299654eda8..c8b1ba2bd99 100644
--- a/web/app/components/base/zendesk/index.tsx
+++ b/web/app/components/base/zendesk/index.tsx
@@ -1,15 +1,17 @@
import { memo } from 'react'
import { IS_PROD, ZENDESK_WIDGET_KEY } from '@/config'
-import { getQueryClientServer } from '@/context/query-client-server'
+import {
+ getSystemFeaturesQueryClient,
+ systemFeaturesServerQueryOptions,
+} from '@/features/system-features/server'
import { headers } from '@/next/headers'
import Script from '@/next/script'
-import { serverConsoleQuery } from '@/service/server'
const Zendesk = async () => {
if (!ZENDESK_WIDGET_KEY) return null
- const queryClient = getQueryClientServer()
- const systemFeaturesQuery = serverConsoleQuery.systemFeatures.get.queryOptions()
+ const queryClient = getSystemFeaturesQueryClient()
+ const systemFeaturesQuery = systemFeaturesServerQueryOptions()
await queryClient.prefetchQuery(systemFeaturesQuery)
const systemFeatures = queryClient.getQueryData(systemFeaturesQuery.queryKey)
if (!systemFeatures || systemFeatures.deployment_edition !== 'CLOUD') return null
diff --git a/web/app/components/datasets/create/__tests__/index.spec.tsx b/web/app/components/datasets/create/__tests__/index.spec.tsx
index 0087256f9c1..ed47b36d7d1 100644
--- a/web/app/components/datasets/create/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/create/__tests__/index.spec.tsx
@@ -69,7 +69,7 @@ vi.mock('@/context/permission-state', async () => {
isLoadingWorkspacePermissionKeys: mockIsLoadingWorkspacePermissionKeys,
}))
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => ({
diff --git a/web/app/components/datasets/documents/__tests__/index.spec.tsx b/web/app/components/datasets/documents/__tests__/index.spec.tsx
index 928e5e9f34b..14cff5eaad3 100644
--- a/web/app/components/datasets/documents/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/documents/__tests__/index.spec.tsx
@@ -72,7 +72,7 @@ vi.mock('@/context/permission-state', async () => {
workspacePermissionKeys: ['dataset.create_and_management'],
}))
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => ({
diff --git a/web/app/components/datasets/documents/components/document-list/__tests__/index.spec.tsx b/web/app/components/datasets/documents/components/document-list/__tests__/index.spec.tsx
index b07c2636406..966d6c3781b 100644
--- a/web/app/components/datasets/documents/components/document-list/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/documents/components/document-list/__tests__/index.spec.tsx
@@ -62,7 +62,7 @@ vi.mock('@/context/permission-state', async () => {
workspacePermissionKeys: ['dataset.create_and_management'],
}))
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => ({
diff --git a/web/app/components/datasets/documents/create-from-pipeline/__tests__/index.spec.tsx b/web/app/components/datasets/documents/create-from-pipeline/__tests__/index.spec.tsx
index ced722c353e..8c196b9e432 100644
--- a/web/app/components/datasets/documents/create-from-pipeline/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/documents/create-from-pipeline/__tests__/index.spec.tsx
@@ -57,7 +57,7 @@ vi.mock('@/context/permission-state', async () => {
}))
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => ({
diff --git a/web/app/components/datasets/hit-testing/__tests__/index.spec.tsx b/web/app/components/datasets/hit-testing/__tests__/index.spec.tsx
index 38765e44fb2..a3480b34bfa 100644
--- a/web/app/components/datasets/hit-testing/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/hit-testing/__tests__/index.spec.tsx
@@ -131,7 +131,7 @@ vi.mock('@/context/permission-state', async () => {
return createPermissionStateModuleMock(() => mockConsoleState)
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => mockConsoleState)
diff --git a/web/app/components/datasets/list/dataset-card/__tests__/index.spec.tsx b/web/app/components/datasets/list/dataset-card/__tests__/index.spec.tsx
index 990c13c5c38..46dd4ff15f4 100644
--- a/web/app/components/datasets/list/dataset-card/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/list/dataset-card/__tests__/index.spec.tsx
@@ -78,7 +78,7 @@ vi.mock('@/context/permission-state', async () => {
return createPermissionStateModuleMock(() => mockConsoleState)
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => mockConsoleState)
diff --git a/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts b/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts
index e25afd3c663..bed86036f61 100644
--- a/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts
+++ b/web/app/components/datasets/settings/form/hooks/__tests__/use-form-state.spec.ts
@@ -46,7 +46,7 @@ vi.mock('@/context/permission-state', async () => {
workspacePermissionKeys: [],
}))
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => ({
diff --git a/web/app/components/plugins/marketplace/__tests__/hydration-server.spec.tsx b/web/app/components/plugins/marketplace/__tests__/hydration-server.spec.tsx
index 4e8c34004bb..d29c203519b 100644
--- a/web/app/components/plugins/marketplace/__tests__/hydration-server.spec.tsx
+++ b/web/app/components/plugins/marketplace/__tests__/hydration-server.spec.tsx
@@ -30,21 +30,18 @@ vi.mock('@/service/client', () => ({
},
}))
-let rootQueryClient: QueryClient
-
-vi.mock('@/context/query-client-server', async (importOriginal) => {
- const actual = await importOriginal<typeof import('@/context/query-client-server')>()
+let queryClient: QueryClient
+vi.mock('@/app/get-query-client', () => {
return {
- ...actual,
- getQueryClientServer: () => rootQueryClient,
+ getQueryClient: () => queryClient,
}
})
describe('HydrateQueryClient', () => {
beforeEach(() => {
vi.clearAllMocks()
- rootQueryClient = new QueryClient({
+ queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, gcTime: 0 } },
})
mockCollections.mockResolvedValue({
@@ -95,9 +92,6 @@ describe('HydrateQueryClient', () => {
})
it('should dehydrate only Marketplace-owned queries', async () => {
- rootQueryClient.setQueryData(['console', 'system-features'], {
- deployment_edition: 'CLOUD',
- })
const { HydrateQueryClient } = await import('../hydration-server')
const element = await HydrateQueryClient({
diff --git a/web/app/components/plugins/marketplace/hydration-server.tsx b/web/app/components/plugins/marketplace/hydration-server.tsx
index 5412f97cb6c..9da59135d56 100644
--- a/web/app/components/plugins/marketplace/hydration-server.tsx
+++ b/web/app/components/plugins/marketplace/hydration-server.tsx
@@ -2,7 +2,7 @@ import type { SearchParams } from 'nuqs/server'
import type { MarketplaceSearchParams } from './search-params'
import { dehydrate, HydrationBoundary } from '@tanstack/react-query'
import { createLoader } from 'nuqs/server'
-import { makeQueryClient } from '@/context/query-client-server'
+import { getQueryClient } from '@/app/get-query-client'
import { marketplaceQuery } from '@/service/client'
import { PLUGIN_CATEGORY_WITH_COLLECTIONS } from './constants'
import { marketplaceSearchParamsParsers } from './search-params'
@@ -21,7 +21,7 @@ async function getDehydratedState(searchParams?: Promise<SearchParams>) {
return
}
- const queryClient = makeQueryClient()
+ const queryClient = getQueryClient()
await queryClient.prefetchQuery({
queryKey: marketplaceQuery.collections.queryKey({
diff --git a/web/app/components/plugins/marketplace/index.tsx b/web/app/components/plugins/marketplace/index.tsx
index d3dd6946b0a..98f1edd8de5 100644
--- a/web/app/components/plugins/marketplace/index.tsx
+++ b/web/app/components/plugins/marketplace/index.tsx
@@ -1,6 +1,6 @@
import type { SearchParams } from 'nuqs'
import { PluginInstallPermissionProviderGuard } from '@/app/components/plugins/install-plugin/components/plugin-install-permission-provider'
-import { TanStackQueryProvider } from '@/context/query-client'
+import { TanStackQueryProvider } from '@/app/query-provider'
import Description from './description'
import { HydrateQueryClient } from './hydration-server'
import ListWrapper from './list/list-wrapper'
diff --git a/web/app/components/rag-pipeline/components/rag-pipeline-header/publisher/__tests__/index.spec.tsx b/web/app/components/rag-pipeline/components/rag-pipeline-header/publisher/__tests__/index.spec.tsx
index ea916849896..89ff198d01b 100644
--- a/web/app/components/rag-pipeline/components/rag-pipeline-header/publisher/__tests__/index.spec.tsx
+++ b/web/app/components/rag-pipeline/components/rag-pipeline-header/publisher/__tests__/index.spec.tsx
@@ -8,7 +8,7 @@ import { render } from '@/test/console/render'
import Publisher from '../index'
import { Popup } from '../popup'
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { atom } = await import('jotai')
return {
deploymentEditionAtom: atom('CLOUD'),
diff --git a/web/app/components/step-by-step-tour/__tests__/state.spec.ts b/web/app/components/step-by-step-tour/__tests__/state.spec.ts
index 7d842cfb6cd..c0378899913 100644
--- a/web/app/components/step-by-step-tour/__tests__/state.spec.ts
+++ b/web/app/components/step-by-step-tour/__tests__/state.spec.ts
@@ -79,7 +79,7 @@ vi.mock('@/context/workspace-state', async () => {
return { currentWorkspaceIdAtom: atom('workspace-1') }
})
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { atom } = await vi.importActual<typeof import('jotai')>('jotai')
return { deploymentEditionAtom: atom('CLOUD') }
})
diff --git a/web/app/components/step-by-step-tour/state.ts b/web/app/components/step-by-step-tour/state.ts
index d76481133d9..c960d09b067 100644
--- a/web/app/components/step-by-step-tour/state.ts
+++ b/web/app/components/step-by-step-tour/state.ts
@@ -14,8 +14,8 @@ import type {
import { atom } from 'jotai'
import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query'
import { selectAtom } from 'jotai/utils'
-import { deploymentEditionAtom } from '@/context/system-features-state'
import { currentWorkspaceIdAtom } from '@/context/workspace-state'
+import { deploymentEditionAtom } from '@/features/system-features/state'
import { consoleQuery } from '@/service/client'
const stepByStepTourStateQueryKey = () =>
diff --git a/web/app/get-query-client.ts b/web/app/get-query-client.ts
new file mode 100644
index 00000000000..8526a53dc4e
--- /dev/null
+++ b/web/app/get-query-client.ts
@@ -0,0 +1,26 @@
+import { defaultShouldDehydrateQuery, environmentManager, QueryClient } from '@tanstack/react-query'
+
+const STALE_TIME = 1000 * 60 * 5
+
+function makeQueryClient() {
+ return new QueryClient({
+ defaultOptions: {
+ queries: {
+ staleTime: STALE_TIME,
+ },
+ dehydrate: {
+ shouldDehydrateQuery: (query) =>
+ defaultShouldDehydrateQuery(query) || query.state.status === 'pending',
+ shouldRedactErrors: () => false,
+ },
+ },
+ })
+}
+
+let browserQueryClient: QueryClient | undefined
+
+export function getQueryClient() {
+ if (environmentManager.isServer()) return makeQueryClient()
+ if (!browserQueryClient) browserQueryClient = makeQueryClient()
+ return browserQueryClient
+}
diff --git a/web/app/layout.tsx b/web/app/layout.tsx
index 2a0f2cbf561..425d651a341 100644
--- a/web/app/layout.tsx
+++ b/web/app/layout.tsx
@@ -6,18 +6,20 @@ import { Provider as JotaiProvider } from 'jotai/react'
import { ThemeProvider } from 'next-themes'
import { NuqsAdapter } from 'nuqs/adapters/next/app'
import { IS_PROD } from '@/config'
-import { TanStackQueryProvider } from '@/context/query-client'
-import { getQueryClientServer } from '@/context/query-client-server'
import { getDatasetMap } from '@/env'
import { SystemFeaturesBootstrapBoundary } from '@/features/system-features/bootstrap-boundary'
+import {
+ getSystemFeaturesQueryClient,
+ systemFeaturesServerQueryOptions,
+} from '@/features/system-features/server'
import { getLocaleOnServer } from '@/i18n-config/server'
import { headers } from '@/next/headers'
-import { serverConsoleQuery } from '@/service/server'
import { CloudAnalytics } from './components/base/analytics-consent/cloud-analytics'
import { PartnerStackCookieRecorder } from './components/billing/partner-stack/cookie-recorder'
import { AgentationLoader } from './components/devtools/agentation-loader'
import { ReactScanLoader } from './components/devtools/react-scan/loader'
import { I18nServerProvider } from './components/provider/i18n-server'
+import { TanStackQueryProvider } from './query-provider'
import './styles/globals.css'
import './styles/markdown.css'
@@ -29,8 +31,8 @@ export const viewport: Viewport = {
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const datasetMap = getDatasetMap()
- const queryClient = getQueryClientServer()
- const systemFeaturesQuery = serverConsoleQuery.systemFeatures.get.queryOptions()
+ const queryClient = getSystemFeaturesQueryClient()
+ const systemFeaturesQuery = systemFeaturesServerQueryOptions()
const [locale, requestHeaders] = await Promise.all([
getLocaleOnServer(),
headers(),
diff --git a/web/context/query-client.tsx b/web/app/query-provider.tsx
similarity index 73%
rename from web/context/query-client.tsx
rename to web/app/query-provider.tsx
index 6d8e03e9723..a3c907d9b5f 100644
--- a/web/context/query-client.tsx
+++ b/web/app/query-provider.tsx
@@ -5,21 +5,11 @@ import type { ReactNode } from 'react'
import { QueryClientProvider, QueryErrorResetBoundary } from '@tanstack/react-query'
import { queryClientAtom } from 'jotai-tanstack-query'
import { useHydrateAtoms } from 'jotai/react/utils'
-import { isServer } from '@/utils/client'
-import { makeQueryClient } from './query-client-server'
-
-let browserQueryClient: QueryClient | undefined
-
-function getQueryClient() {
- if (isServer) {
- return makeQueryClient()
- }
- if (!browserQueryClient) browserQueryClient = makeQueryClient()
- return browserQueryClient
-}
+import { getQueryClient } from './get-query-client'
export function TanStackQueryProvider({ children }: { children: ReactNode }) {
const queryClient = getQueryClient()
+
return (
<QueryClientProvider client={queryClient}>
<QueryErrorResetBoundary>
diff --git a/web/context/i18n.ts b/web/context/i18n.ts
index 864660816bb..6470ed5a52d 100644
--- a/web/context/i18n.ts
+++ b/web/context/i18n.ts
@@ -4,7 +4,7 @@ import type { DocPathWithoutLang, DocsProduct } from '@/types/doc-paths'
import { useAtomValue } from 'jotai'
import { useCallback } from 'react'
import { useTranslation } from '#i18n'
-import { deploymentEditionAtom } from '@/context/system-features-state'
+import { deploymentEditionAtom } from '@/features/system-features/state'
import { getDocLanguage, getLanguage, getPricingPageLanguage } from '@/i18n-config/language'
import { docPathProductAvailability } from '@/types/doc-paths'
diff --git a/web/context/provider-context-provider.tsx b/web/context/provider-context-provider.tsx
index 1ebfb7f3fb4..c4dff304d57 100644
--- a/web/context/provider-context-provider.tsx
+++ b/web/context/provider-context-provider.tsx
@@ -17,7 +17,7 @@ import {
ModelTypeEnum,
} from '@/app/components/header/account-setting/model-provider-page/declarations'
import { ZENDESK_FIELD_IDS } from '@/config'
-import { deploymentEditionAtom } from '@/context/system-features-state'
+import { deploymentEditionAtom } from '@/features/system-features/state'
import { fetchCurrentPlanInfo } from '@/service/billing'
import { useModelListByType, useModelProviders } from '@/service/use-common'
import { useEducationStatus } from '@/service/use-education'
diff --git a/web/context/query-client-server.ts b/web/context/query-client-server.ts
deleted file mode 100644
index 69bd29ae975..00000000000
--- a/web/context/query-client-server.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { QueryClient } from '@tanstack/react-query'
-import { cache } from 'react'
-
-const STALE_TIME = 1000 * 60 * 5 // 5 minutes
-
-export function makeQueryClient() {
- return new QueryClient({
- defaultOptions: {
- queries: {
- staleTime: STALE_TIME,
- },
- },
- })
-}
-
-export const getQueryClientServer = cache(makeQueryClient)
diff --git a/web/context/version-state.ts b/web/context/version-state.ts
index 4568bc3d77c..d1ee8475bcb 100644
--- a/web/context/version-state.ts
+++ b/web/context/version-state.ts
@@ -2,11 +2,11 @@
import { atom } from 'jotai'
import { atomWithQuery } from 'jotai-tanstack-query'
+import { brandingEnabledAtom } from '@/features/system-features/state'
import { consoleQuery } from '@/service/client'
import { accountProfileMetaAtom } from './account-state'
import { initialLangGeniusVersionInfo } from './app-context-defaults'
import { getLangGeniusVersionInfo } from './app-context-normalizers'
-import { brandingEnabledAtom } from './system-features-state'
const versionQueryAtom = atomWithQuery((get) => {
const meta = get(accountProfileMetaAtom)
diff --git a/web/context/zendesk-conversation-sync.ts b/web/context/zendesk-conversation-sync.ts
index 072dedb1b7d..dfc9a0634d4 100644
--- a/web/context/zendesk-conversation-sync.ts
+++ b/web/context/zendesk-conversation-sync.ts
@@ -5,8 +5,8 @@ import { atom } from 'jotai'
import { atomEffect } from 'jotai-effect'
import { setZendeskConversationFields } from '@/app/components/base/zendesk/utils'
import { ZENDESK_FIELD_IDS } from '@/config'
+import { deploymentEditionAtom } from '@/features/system-features/state'
import { userProfileAtom } from './account-state'
-import { deploymentEditionAtom } from './system-features-state'
import { langGeniusVersionInfoAtom } from './version-state'
import { currentWorkspaceAtom } from './workspace-state'
diff --git a/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/chat.spec.tsx b/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/chat.spec.tsx
index 5d710dbcaae..c00f7077649 100644
--- a/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/chat.spec.tsx
+++ b/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/chat.spec.tsx
@@ -35,7 +35,7 @@ const mockConsoleState = vi.hoisted(() => ({
deploymentEdition: 'COMMUNITY' as 'CLOUD' | 'COMMUNITY' | 'ENTERPRISE',
}))
-vi.mock('@/context/system-features-state', async () => {
+vi.mock('@/features/system-features/state', async () => {
const { createSystemFeaturesStateModuleMock } = await import('@/test/console/state-fixture')
return createSystemFeaturesStateModuleMock(() => mockConsoleState)
})
diff --git a/web/features/agent-v2/agent-detail/configure/components/preview/chat-session.tsx b/web/features/agent-v2/agent-detail/configure/components/preview/chat-session.tsx
index 2213e7c2a6a..cf1a779acea 100644
--- a/web/features/agent-v2/agent-detail/configure/components/preview/chat-session.tsx
+++ b/web/features/agent-v2/agent-detail/configure/components/preview/chat-session.tsx
@@ -16,10 +16,10 @@ import { useAtomValue } from 'jotai'
import { useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import ChatInputArea from '@/app/components/base/chat/chat/chat-input-area'
-import { deploymentEditionAtom } from '@/context/system-features-state'
import { agentComposerDraftAtom } from '@/features/agent-v2/agent-composer/store'
import { agentComposerModelAtom } from '@/features/agent-v2/agent-composer/store-modules/model'
import { agentComposerPromptAtom } from '@/features/agent-v2/agent-composer/store-modules/prompt'
+import { deploymentEditionAtom } from '@/features/system-features/state'
import { buildChatConfig, getAgentSoulInputs, getAgentSoulInputsForm } from './chat-config'
import { AgentPreviewChatConversation } from './chat-conversation'
diff --git a/web/features/home/page.tsx b/web/features/home/page.tsx
index d50ceb36671..b119a486ce4 100644
--- a/web/features/home/page.tsx
+++ b/web/features/home/page.tsx
@@ -1,6 +1,10 @@
-import { defaultShouldDehydrateQuery, dehydrate, HydrationBoundary } from '@tanstack/react-query'
+import { dehydrate, HydrationBoundary } from '@tanstack/react-query'
import { Suspense } from 'react'
-import { getQueryClientServer, makeQueryClient } from '@/context/query-client-server'
+import { getQueryClient } from '@/app/get-query-client'
+import {
+ getSystemFeaturesQueryClient,
+ systemFeaturesServerQueryOptions,
+} from '@/features/system-features/server'
import { getLocaleOnServer } from '@/i18n-config/server'
import { getServerConsoleClientContext, serverConsoleQuery } from '@/service/server'
import { HomeContent } from './home-content/home-content'
@@ -8,7 +12,7 @@ import { HomeShell } from './home-shell'
import { HomeSkeleton } from './home-skeleton'
export async function HomePage() {
- const homeQueryClient = makeQueryClient()
+ const homeQueryClient = getQueryClient()
const [locale, context] = await Promise.all([
getLocaleOnServer(),
getServerConsoleClientContext(),
@@ -28,9 +32,7 @@ export async function HomePage() {
)
const enableExploreBanner = (
- await getQueryClientServer().ensureQueryData(
- serverConsoleQuery.systemFeatures.get.queryOptions(),
- )
+ await getSystemFeaturesQueryClient().ensureQueryData(systemFeaturesServerQueryOptions())
).enable_explore_banner
if (enableExploreBanner) {
void homeQueryClient.prefetchQuery(
@@ -41,11 +43,7 @@ export async function HomePage() {
)
}
- const dehydratedState = dehydrate(homeQueryClient, {
- shouldDehydrateQuery: (query) =>
- defaultShouldDehydrateQuery(query) || query.state.status === 'pending',
- shouldRedactErrors: () => false,
- })
+ const dehydratedState = dehydrate(homeQueryClient)
return (
<HydrationBoundary state={dehydratedState}>
diff --git a/web/features/system-features/README.md b/web/features/system-features/README.md
index d656ee4b171..40b73a02d84 100644
--- a/web/features/system-features/README.md
+++ b/web/features/system-features/README.md
@@ -4,10 +4,16 @@ Loads deployment-wide capabilities for server prefetching, client hydration, and
## Internal Modules
-None.
+- `bootstrap-boundary.tsx`
+- `client.ts`
+- `server.ts`
+- `state.ts`
## External Modules
+- `app/get-query-client`
- `app/components/full-screen-loading`
-- `context/server-console-client`
+- `service/client`
+- `service/server`
- `utils/client`
+- `utils/query-atoms`
diff --git a/web/features/system-features/server.ts b/web/features/system-features/server.ts
new file mode 100644
index 00000000000..fdae9d30c8a
--- /dev/null
+++ b/web/features/system-features/server.ts
@@ -0,0 +1,9 @@
+import { cache } from 'react'
+import { getQueryClient } from '@/app/get-query-client'
+import { serverConsoleQuery } from '@/service/server'
+import 'server-only'
+
+export const getSystemFeaturesQueryClient = cache(getQueryClient)
+
+export const systemFeaturesServerQueryOptions = () =>
+ serverConsoleQuery.systemFeatures.get.queryOptions()
diff --git a/web/context/system-features-state.ts b/web/features/system-features/state.ts
similarity index 86%
rename from web/context/system-features-state.ts
rename to web/features/system-features/state.ts
index 2b6929dd862..1bb2153212d 100644
--- a/web/context/system-features-state.ts
+++ b/web/features/system-features/state.ts
@@ -1,8 +1,8 @@
'use client'
import { atom } from 'jotai'
-import { systemFeaturesQueryOptions } from '@/features/system-features/client'
import { atomWithResolvedSuspenseQuery } from '@/utils/query-atoms'
+import { systemFeaturesQueryOptions } from './client'
const systemFeaturesQueryAtom = atomWithResolvedSuspenseQuery(() => systemFeaturesQueryOptions())