Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Langgenius Dify TanStack Query Integration

From Leeroopedia
Knowledge Sources Dify
Domains Frontend, Data Fetching
Last Updated 2026-02-12 07:00 GMT

Overview

Server state management using TanStack React Query for caching, deduplication, and background refresh, providing a robust data fetching layer for the Dify frontend.

Description

The TanStack Query Integration principle defines how Dify manages server-side data in the frontend application. Rather than manually tracking loading states, caching responses, and handling refetch logic, the application delegates these concerns to TanStack React Query (formerly React Query). This library provides automatic caching with configurable stale times, request deduplication when multiple components request the same data, background refresh to keep data current, and built-in error and loading state management.

In the Dify codebase, TanStack Query is integrated at the context level, where application-critical data such as workspace information, user profiles, model provider configurations, and system settings are fetched and cached. The App Context, Global Public Context, and Provider Context all leverage query hooks to manage their respective data domains. Query keys are structured hierarchically to enable targeted cache invalidation when mutations occur. The integration provides a consistent pattern where any component can access server data through query hooks without implementing its own fetch logic.

This principle matters because Dify's frontend makes numerous API calls to display workspace data, application configurations, model provider status, and real-time conversation data. Without a dedicated server state management layer, each component would implement its own caching and refresh logic, leading to inconsistent data, redundant network requests, and complex loading state management. TanStack Query eliminates these problems while providing advanced features like optimistic updates, infinite scrolling support, and prefetching that enhance the user experience.

Usage

Use this principle when:

  • Fetching data from the backend API that multiple components need to access
  • Implementing mutations that should invalidate or update cached data
  • Adding features that require background data refresh or optimistic updates

Theoretical Basis

TanStack Query implements the stale-while-revalidate caching strategy, originally defined in HTTP RFC 5861, where cached data is served immediately (even if stale) while a background refresh fetches the latest version. The library distinguishes between client state (UI state local to the browser) and server state (data owned by the server that the client caches), following the principle that these two categories of state require fundamentally different management strategies. The query key system implements cache key normalization, ensuring that identical requests always resolve to the same cache entry regardless of component location.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment