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.

fix(web): prefetch skill feature flag for initial navigation (#41731)

Zhu Leid4d2a4a98852
diff --git a/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx b/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx
index 3e8b05dbfab..142754d4ea1 100644
--- a/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx
+++ b/web/app/(commonLayout)/__tests__/hydration-boundary.spec.tsx
@@ -1,6 +1,6 @@
import type { DehydratedState } from '@tanstack/react-query'
import type { ReactElement } from 'react'
-import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
+import { QueryClient, QueryClientProvider, queryOptions, useQuery } from '@tanstack/react-query'
import { render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
@@ -11,6 +11,8 @@ const mocks = vi.hoisted(() => ({
workspaceQueryOptions: vi.fn(),
permissionQueryFn: vi.fn(),
permissionQueryOptions: vi.fn(),
+ featuresQueryFn: vi.fn(),
+ featuresQueryOptions: vi.fn(),
redirect: vi.fn((url: string) => {
throw new Error(`NEXT_REDIRECT:${url}`)
}),
@@ -53,6 +55,11 @@ vi.mock('@/service/console/server', () => ({
vi.mock('@/service/console', () => ({
consoleQuery: {
+ features: {
+ get: {
+ queryOptions: (...args: unknown[]) => mocks.featuresQueryOptions(...args),
+ },
+ },
workspaces: {
current: {
summary: {
@@ -125,6 +132,12 @@ describe('CommonLayoutHydrationBoundary', () => {
queryFn: mocks.permissionQueryFn,
retry: false,
})
+ mocks.featuresQueryFn.mockResolvedValue({ enable_skill: true })
+ mocks.featuresQueryOptions.mockReturnValue({
+ queryKey: ['console', 'features', 'get'],
+ queryFn: mocks.featuresQueryFn,
+ retry: false,
+ })
})
it('should prefetch common layout queries', async () => {
@@ -149,6 +162,51 @@ describe('CommonLayoutHydrationBoundary', () => {
retry: false,
})
expect(mocks.permissionQueryFn).toHaveBeenCalledTimes(1)
+ expect(mocks.featuresQueryOptions).toHaveBeenCalledWith({
+ retry: false,
+ })
+ expect(mocks.featuresQueryFn).toHaveBeenCalledTimes(1)
+ })
+
+ it.each([true, false])('hydrates the initial skill flag as %s', async (enableSkill) => {
+ mocks.featuresQueryFn.mockResolvedValue({ enable_skill: enableSkill })
+ const { CommonLayoutHydrationBoundary } = await import('../hydration-boundary')
+ const client = new QueryClient()
+ function SkillFlag() {
+ const { data } = useQuery(
+ queryOptions<{ enable_skill: boolean }>({
+ queryKey: ['console', 'features', 'get'],
+ queryFn: mocks.featuresQueryFn,
+ staleTime: Infinity,
+ }),
+ )
+ return <output aria-label="Skills enabled">{String(data?.enable_skill)}</output>
+ }
+ const element = await CommonLayoutHydrationBoundary({ children: <SkillFlag /> })
+ render(<QueryClientProvider client={client}>{element}</QueryClientProvider>)
+ expect(screen.getByLabelText('Skills enabled')).toHaveTextContent(String(enableSkill))
+ expect(mocks.featuresQueryFn).toHaveBeenCalledTimes(1)
+ client.clear()
+ })
+
+ it('does not hydrate another request’s feature flag after a failed prefetch', async () => {
+ const { CommonLayoutHydrationBoundary } = await import('../hydration-boundary')
+ const first = await CommonLayoutHydrationBoundary({ children: null })
+ const firstState = (first as ReactElement<{ state: DehydratedState }>).props.state
+ expect(
+ firstState.queries.find((query) => query.queryKey.includes('features'))?.state.data,
+ ).toEqual({ enable_skill: true })
+
+ mocks.queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
+ mocks.featuresQueryFn.mockRejectedValue(new Error('features unavailable'))
+ const second = await CommonLayoutHydrationBoundary({ children: null })
+ const secondState = (second as ReactElement<{ state: DehydratedState }>).props.state
+ expect(secondState.queries.map((query) => query.queryKey)).not.toContainEqual([
+ 'console',
+ 'features',
+ 'get',
+ ])
+ expect(mocks.redirect).not.toHaveBeenCalled()
})
it('should dehydrate only Common-owned queries', async () => {
@@ -158,21 +216,41 @@ describe('CommonLayoutHydrationBoundary', () => {
const state = (element as ReactElement<{ state: DehydratedState }>).props.state
const queryKeys = state.queries.map((query) => query.queryKey)
- expect(queryKeys).toHaveLength(3)
+ expect(queryKeys).toHaveLength(4)
expect(queryKeys).toEqual(
expect.arrayContaining([
['common', 'user-profile'],
['console', 'workspaces', 'current', 'summary', 'get'],
[['console', 'workspaces', 'current', 'rbac', 'myPermissions', 'get'], { type: 'query' }],
+ ['console', 'features', 'get'],
]),
)
})
- it.each(['workspace', 'permissions'] as const)(
- 'should keep the Common shell recoverable when the %s query fails',
- async (target) => {
- const failedQueryFn =
- target === 'workspace' ? mocks.workspaceQueryFn : mocks.permissionQueryFn
+ it.each([
+ {
+ queryKey: ['console', 'workspaces', 'current', 'summary', 'get'],
+ target: 'workspace' as const,
+ },
+ {
+ queryKey: [
+ ['console', 'workspaces', 'current', 'rbac', 'myPermissions', 'get'],
+ { type: 'query' },
+ ],
+ target: 'permissions' as const,
+ },
+ {
+ queryKey: ['console', 'features', 'get'],
+ target: 'features' as const,
+ },
+ ])(
+ 'should keep the Common shell recoverable when the $target query fails',
+ async ({ queryKey, target }) => {
+ const failedQueryFn = {
+ features: mocks.featuresQueryFn,
+ permissions: mocks.permissionQueryFn,
+ workspace: mocks.workspaceQueryFn,
+ }[target]
failedQueryFn.mockRejectedValue(new Error(`${target} unavailable`))
const { CommonLayoutHydrationBoundary } = await import('../hydration-boundary')
@@ -180,14 +258,7 @@ describe('CommonLayoutHydrationBoundary', () => {
const state = (element as ReactElement<{ state: DehydratedState }>).props.state
expect(mocks.redirect).not.toHaveBeenCalled()
- expect(state.queries.map((query) => query.queryKey)).not.toContainEqual(
- target === 'workspace'
- ? ['console', 'workspaces', 'current', 'summary', 'get']
- : [
- ['console', 'workspaces', 'current', 'rbac', 'myPermissions', 'get'],
- { type: 'query' },
- ],
- )
+ expect(state.queries.map((query) => query.queryKey)).not.toContainEqual(queryKey)
},
)
@@ -248,5 +319,6 @@ describe('CommonLayoutHydrationBoundary', () => {
expect(mocks.profileQueryFn).not.toHaveBeenCalled()
expect(mocks.workspaceQueryFn).not.toHaveBeenCalled()
expect(mocks.permissionQueryFn).not.toHaveBeenCalled()
+ expect(mocks.featuresQueryFn).not.toHaveBeenCalled()
})
})
diff --git a/web/app/(commonLayout)/hydration-boundary.tsx b/web/app/(commonLayout)/hydration-boundary.tsx
index dcd8c05ed0e..1734d6c2243 100644
--- a/web/app/(commonLayout)/hydration-boundary.tsx
+++ b/web/app/(commonLayout)/hydration-boundary.tsx
@@ -73,6 +73,13 @@ export async function CommonLayoutHydrationBoundary({ children }: { children: Re
}),
)
.catch(noop),
+ queryClient
+ .query(
+ consoleQuery.features.get.queryOptions({
+ retry: false,
+ }),
+ )
+ .catch(noop),
])
} catch (error) {
await handleProfileError(error)