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): defer tldts load for MCP favicon URL blur (#42486)

Sourav Rajvi200da319f2f7
diff --git a/web/app/components/tools/mcp/hooks/__tests__/use-mcp-modal-form.spec.ts b/web/app/components/tools/mcp/hooks/__tests__/use-mcp-modal-form.spec.ts
index ed53d2ca951..d7b04c7517e 100644
--- a/web/app/components/tools/mcp/hooks/__tests__/use-mcp-modal-form.spec.ts
+++ b/web/app/components/tools/mcp/hooks/__tests__/use-mcp-modal-form.spec.ts
@@ -13,6 +13,16 @@ vi.mock('@/service/common', () => ({
uploadRemoteFileInfo: vi.fn(),
}))
+vi.mock('tldts', () => ({
+ getDomain: vi.fn((url: string) => {
+ try {
+ return new URL(url).hostname
+ } catch {
+ return null
+ }
+ }),
+}))
+
describe('useMCPModalForm', () => {
describe('Utility Functions', () => {
describe('isValidUrl', () => {
@@ -425,6 +435,64 @@ describe('useMCPModalForm', () => {
consoleErrorSpy.mockRestore()
})
+ it('should ignore stale blur when a newer blur starts before fetch completes', async () => {
+ const { uploadRemoteFileInfo } = await import('@/service/common')
+ let resolveFirstUpload: (value: {
+ id: string
+ name: string
+ size: number
+ mime_type: string
+ url: string
+ }) => void
+ const firstUpload = new Promise<{
+ id: string
+ name: string
+ size: number
+ mime_type: string
+ url: string
+ }>((resolve) => {
+ resolveFirstUpload = resolve
+ })
+
+ vi.mocked(uploadRemoteFileInfo).mockImplementation((remoteIcon) => {
+ if (String(remoteIcon).includes('first.example.com')) return firstUpload
+ return Promise.resolve({
+ id: 'file456',
+ name: 'icon2.png',
+ size: 1024,
+ mime_type: 'image/png',
+ url: 'https://example.com/files/file456/file-preview/icon2.png',
+ } as unknown as { id: string; name: string; size: number; mime_type: string; url: string })
+ })
+
+ const { result } = renderHook(() => useMCPModalForm())
+
+ const firstBlur = result.current.actions.handleUrlBlur('https://first.example.com/mcp')
+
+ await act(async () => {
+ await result.current.actions.handleUrlBlur('https://second.example.com/mcp')
+ })
+
+ resolveFirstUpload!({
+ id: 'file123',
+ name: 'icon.png',
+ size: 1024,
+ mime_type: 'image/png',
+ url: 'https://example.com/files/file123/file-preview/icon.png',
+ })
+ await act(async () => {
+ await firstBlur
+ })
+
+ expect(result.current.state.appIcon.type).toBe('image')
+ expect((result.current.state.appIcon as AppIconImageSelection).url).toBe(
+ 'https://example.com/files/file456/file-preview/icon2.png',
+ )
+ expect(result.current.state.isFetchingIcon).toBe(false)
+
+ vi.mocked(uploadRemoteFileInfo).mockReset()
+ })
+
it('should fetch icon successfully for valid URL in create mode', async () => {
vi.mocked(
await import('@/service/common').then((m) => m.uploadRemoteFileInfo),
diff --git a/web/app/components/tools/mcp/hooks/use-mcp-modal-form.ts b/web/app/components/tools/mcp/hooks/use-mcp-modal-form.ts
index a14f8e04236..d7db9a1bcff 100644
--- a/web/app/components/tools/mcp/hooks/use-mcp-modal-form.ts
+++ b/web/app/components/tools/mcp/hooks/use-mcp-modal-form.ts
@@ -3,7 +3,6 @@ import type { HeaderItem } from '../headers-input'
import type { AppIconSelection } from '@/app/components/base/app-icon-picker'
import type { ToolWithProvider } from '@/app/components/workflow/types'
import { useCallback, useMemo, useRef, useState } from 'react'
-import { getDomain } from 'tldts'
import { v4 as uuid } from 'uuid'
import { MCPAuthMethod } from '@/app/components/tools/types'
import { toast } from '@/app/notifications'
@@ -101,6 +100,7 @@ export const useMCPModalForm = (data?: ToolWithProvider) => {
const [headers, setHeaders] = useState<HeaderItem[]>(() => getInitialHeaders(data))
const [isFetchingIcon, setIsFetchingIcon] = useState(false)
const appIconRef = useRef<HTMLDivElement>(null)
+ const urlBlurGenerationRef = useRef(0)
// Auth state
const [authMethod, setAuthMethod] = useState<MCPAuthMethod>(MCPAuthMethod.authentication)
const [isDynamicRegistration, setIsDynamicRegistration] = useState(() =>
@@ -117,13 +117,26 @@ export const useMCPModalForm = (data?: ToolWithProvider) => {
async (urlValue: string) => {
if (data) return
if (!isValidUrl(urlValue)) return
- const domain = getDomain(urlValue)
- const remoteIcon = `https://www.google.com/s2/favicons?domain=${domain}&sz=128`
+
+ const blurGeneration = ++urlBlurGenerationRef.current
+ const isStale = () => blurGeneration !== urlBlurGenerationRef.current
+
setIsFetchingIcon(true)
try {
+ const { getDomain } = await import('tldts')
+ if (isStale()) return
+
+ const domain = getDomain(urlValue)
+ if (!domain) return
+
+ const remoteIcon = `https://www.google.com/s2/favicons?domain=${domain}&sz=128`
const res = await uploadRemoteFileInfo(remoteIcon, undefined, true)
+ if (isStale()) return
+
setAppIcon({ type: 'image', url: res.url, fileId: extractFileId(res.url) || '' })
} catch (e) {
+ if (isStale()) return
+
let errorMessage = 'Failed to fetch remote icon'
if (e instanceof Response) {
try {
@@ -138,7 +151,7 @@ export const useMCPModalForm = (data?: ToolWithProvider) => {
console.error('Failed to fetch remote icon:', e)
toast.warning(errorMessage)
} finally {
- setIsFetchingIcon(false)
+ if (!isStale()) setIsFetchingIcon(false)
}
},
[data],