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.

perf(web): prefetch app links on intent (#40133)

yyhf00d0329d4f3
diff --git a/web/app/components/explore/installed-app-navigation/__tests__/app-nav-item.spec.tsx b/web/app/components/explore/installed-app-navigation/__tests__/app-nav-item.spec.tsx
index 1ecb8828935..d9b54695c25 100644
--- a/web/app/components/explore/installed-app-navigation/__tests__/app-nav-item.spec.tsx
+++ b/web/app/components/explore/installed-app-navigation/__tests__/app-nav-item.spec.tsx
@@ -1,8 +1,26 @@
import type { InstalledAppResponse } from '@dify/contracts/api/console/installed-apps/types.gen'
+import type { AnchorHTMLAttributes, ReactNode } from 'react'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import AppNavItem from '../app-nav-item'
+vi.mock('@/next/link', () => ({
+ default: ({
+ children,
+ href,
+ prefetch,
+ ...props
+ }: AnchorHTMLAttributes<HTMLAnchorElement> & {
+ children?: ReactNode
+ href: string
+ prefetch?: boolean | null
+ }) => (
+ <a href={href} data-prefetch={prefetch === null ? 'auto' : prefetch} {...props}>
+ {children}
+ </a>
+ ),
+}))
+
const baseProps = {
app: {
id: 'app-123',
@@ -51,6 +69,7 @@ describe('AppNavItem', () => {
expect(link).toHaveAttribute('href', '/installed/app-123')
expect(link).toHaveAttribute('aria-label', 'My App')
expect(link).not.toHaveAttribute('aria-current')
+ expect(link).not.toHaveAttribute('data-prefetch')
})
it('should use a contextual accessible name when ariaLabel is provided', () => {
@@ -63,6 +82,33 @@ describe('AppNavItem', () => {
expect(screen.getByText('My App')).toBeInTheDocument()
})
+ it('should enable prefetch after pointer intent when requested', async () => {
+ const user = userEvent.setup()
+ render(<AppNavItem {...baseProps} prefetchOnIntent />)
+
+ const link = screen.getByRole('link', { name: 'My App' })
+
+ expect(link).toHaveAttribute('data-prefetch', 'false')
+
+ await user.hover(link)
+
+ expect(link).toHaveAttribute('data-prefetch', 'auto')
+ })
+
+ it('should enable prefetch after keyboard focus when requested', async () => {
+ const user = userEvent.setup()
+ render(<AppNavItem {...baseProps} prefetchOnIntent />)
+
+ const link = screen.getByRole('link', { name: 'My App' })
+
+ expect(link).toHaveAttribute('data-prefetch', 'false')
+
+ await user.tab()
+
+ expect(link).toHaveFocus()
+ expect(link).toHaveAttribute('data-prefetch', 'auto')
+ })
+
it('should expose selected state through the current link', () => {
render(<AppNavItem {...baseProps} isSelected />)
diff --git a/web/app/components/explore/installed-app-navigation/app-nav-item.tsx b/web/app/components/explore/installed-app-navigation/app-nav-item.tsx
index f042bd612f8..393c5a9a867 100644
--- a/web/app/components/explore/installed-app-navigation/app-nav-item.tsx
+++ b/web/app/components/explore/installed-app-navigation/app-nav-item.tsx
@@ -1,7 +1,7 @@
'use client'
import type { InstalledAppResponse } from '@dify/contracts/api/console/installed-apps/types.gen'
import { cn } from '@langgenius/dify-ui/cn'
-import * as React from 'react'
+import { useState } from 'react'
import AppIcon from '@/app/components/base/app-icon'
import { buildInstalledAppPath } from '@/app/components/explore/installed-app/routes'
import ItemOperation from '@/app/components/explore/item-operation'
@@ -10,6 +10,7 @@ import Link from '@/next/link'
type IAppNavItemProps = {
variant?: 'default' | 'mainNav'
ariaLabel?: string
+ prefetchOnIntent?: boolean
app: InstalledAppResponse
isSelected: boolean
onTogglePin: (id: string, isPinned: boolean) => void
@@ -19,11 +20,13 @@ type IAppNavItemProps = {
export default function AppNavItem({
variant = 'default',
ariaLabel,
+ prefetchOnIntent = false,
app: installedApp,
isSelected,
onTogglePin,
onDelete,
}: IAppNavItemProps) {
+ const [isPrefetchEnabled, setIsPrefetchEnabled] = useState(false)
const {
id,
is_pinned: isPinned,
@@ -47,6 +50,9 @@ export default function AppNavItem({
>
<Link
href={url}
+ prefetch={prefetchOnIntent ? (isPrefetchEnabled ? null : false) : undefined}
+ onMouseEnter={prefetchOnIntent ? () => setIsPrefetchEnabled(true) : undefined}
+ onFocus={prefetchOnIntent ? () => setIsPrefetchEnabled(true) : undefined}
aria-current={isSelected ? 'page' : undefined}
aria-label={ariaLabel ?? name}
title={name}
diff --git a/web/app/components/main-nav/components/web-apps-section.tsx b/web/app/components/main-nav/components/web-apps-section.tsx
index 77666c07343..2b9421928d6 100644
--- a/web/app/components/main-nav/components/web-apps-section.tsx
+++ b/web/app/components/main-nav/components/web-apps-section.tsx
@@ -124,6 +124,7 @@ const WebAppsSectionContent = () => {
<AppNavItem
key={installedApp.id}
variant="mainNav"
+ prefetchOnIntent
app={installedApp}
ariaLabel={t(($) => $['mainNav.webApps.openApp'], {
ns: 'common',
diff --git a/web/app/education-apply/__tests__/expire-notice.spec.tsx b/web/app/education-apply/__tests__/expire-notice.spec.tsx
index 269ad682850..419a5e8525e 100644
--- a/web/app/education-apply/__tests__/expire-notice.spec.tsx
+++ b/web/app/education-apply/__tests__/expire-notice.spec.tsx
@@ -8,7 +8,6 @@ import { resolveEducationExpireNotice } from '../use-expire-notice'
const mockEducationStatus = vi.hoisted(() => ({
allowRefresh: true,
expireAt: Date.UTC(2099, 0, 1) / 1000,
- isLoading: false,
}))
const mockPricingModal = vi.hoisted(() => ({ isOpen: false }))
@@ -38,12 +37,10 @@ vi.mock('@/next/dynamic', () => ({
const renderNotice = (accountId = 'user-1') => {
const { wrapper } = createConsoleQueryWrapper({
accountProfile: { id: accountId, timezone: 'UTC' },
- educationStatus: mockEducationStatus.isLoading
- ? undefined
- : {
- allow_refresh: mockEducationStatus.allowRefresh,
- expire_at: mockEducationStatus.expireAt,
- },
+ educationStatus: {
+ allow_refresh: mockEducationStatus.allowRefresh,
+ expire_at: mockEducationStatus.expireAt,
+ },
})
return render(<EducationExpireNotice />, { wrapper })
@@ -54,7 +51,6 @@ describe('EducationExpireNotice', () => {
localStorage.clear()
mockEducationStatus.allowRefresh = true
mockEducationStatus.expireAt = Date.UTC(2099, 0, 1) / 1000
- mockEducationStatus.isLoading = false
mockPricingModal.isOpen = false
})
@@ -84,14 +80,6 @@ describe('EducationExpireNotice', () => {
expect(await screen.findByRole('dialog')).toHaveTextContent('Expired')
})
- it('does not show while education status is unavailable', async () => {
- mockEducationStatus.isLoading = true
-
- renderNotice()
-
- await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
- })
-
it('defers the notice while the URL-driven pricing modal is open', async () => {
mockPricingModal.isOpen = true
diff --git a/web/features/home/continue-work/__tests__/item.spec.tsx b/web/features/home/continue-work/__tests__/item.spec.tsx
index 0669f7f3362..75759198e0c 100644
--- a/web/features/home/continue-work/__tests__/item.spec.tsx
+++ b/web/features/home/continue-work/__tests__/item.spec.tsx
@@ -43,9 +43,19 @@ vi.mock('@/next/link', () => ({
children,
href,
className,
+ prefetch,
...props
- }: AnchorHTMLAttributes<HTMLAnchorElement> & { children?: ReactNode; href: string }) => (
- <a href={href} className={className} {...props}>
+ }: AnchorHTMLAttributes<HTMLAnchorElement> & {
+ children?: ReactNode
+ href: string
+ prefetch?: boolean | null
+ }) => (
+ <a
+ href={href}
+ className={className}
+ data-prefetch={prefetch === null ? 'auto' : prefetch}
+ {...props}
+ >
{children}
</a>
),
@@ -95,6 +105,33 @@ describe('ContinueWorkItem', () => {
expect(mockFormatTimeFromNow).toHaveBeenCalledWith(200000)
})
+ it('should enable prefetch after pointer intent', async () => {
+ const user = userEvent.setup()
+ renderItem(createApp())
+
+ const link = screen.getByRole('link', { name: /Continue App/ })
+
+ expect(link).toHaveAttribute('data-prefetch', 'false')
+
+ await user.hover(link)
+
+ expect(link).toHaveAttribute('data-prefetch', 'auto')
+ })
+
+ it('should enable prefetch after keyboard focus', async () => {
+ const user = userEvent.setup()
+ renderItem(createApp())
+
+ const link = screen.getByRole('link', { name: /Continue App/ })
+
+ expect(link).toHaveAttribute('data-prefetch', 'false')
+
+ await user.tab()
+
+ expect(link).toHaveFocus()
+ expect(link).toHaveAttribute('data-prefetch', 'auto')
+ })
+
it.each([
['chat', 'app.types.chatbot'],
['advanced-chat', 'app.types.advanced'],
diff --git a/web/features/home/continue-work/item.tsx b/web/features/home/continue-work/item.tsx
index 93dc847c1b0..b2a3c8445ba 100644
--- a/web/features/home/continue-work/item.tsx
+++ b/web/features/home/continue-work/item.tsx
@@ -5,7 +5,7 @@ import { cn } from '@langgenius/dify-ui/cn'
import { toast } from '@langgenius/dify-ui/toast'
import { useSuspenseQuery } from '@tanstack/react-query'
import { useAtomValue } from 'jotai'
-import * as React from 'react'
+import { useId, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { AppTypeIcon } from '@/app/components/app/type-selector'
import AppIcon from '@/app/components/base/app-icon'
@@ -35,9 +35,10 @@ export function ContinueWorkItem({ app }: ContinueWorkItemProps) {
const currentUserId = useAtomValue(userProfileIdAtom)
const workspacePermissionKeys = useAtomValue(workspacePermissionKeysAtom)
const { data: systemFeatures } = useSuspenseQuery(systemFeaturesQueryOptions())
- const appNameId = React.useId()
- const appModeId = React.useId()
- const appMetadataId = React.useId()
+ const appNameId = useId()
+ const appModeId = useId()
+ const appMetadataId = useId()
+ const [isPrefetchEnabled, setIsPrefetchEnabled] = useState(false)
const isRbacEnabled = systemFeatures.rbac_enabled
const updatedAt = app.updated_at * 1000
const appModeLabel = t(($) => $[appModeLabelKeys[app.mode]], { ns: 'app' })
@@ -121,6 +122,9 @@ export function ContinueWorkItem({ app }: ContinueWorkItemProps) {
return (
<Link
href={href}
+ prefetch={isPrefetchEnabled ? null : false}
+ onMouseEnter={() => setIsPrefetchEnabled(true)}
+ onFocus={() => setIsPrefetchEnabled(true)}
aria-labelledby={`${appNameId} ${appModeId}`}
aria-describedby={appMetadataId}
className={cn(