Skip to content

refactor(web): drop swr and migrate share/chat hooks to tanstack query#30232

Merged
iamjoel merged 7 commits intomainfrom
refactor/drop-swr
Dec 29, 2025
Merged

refactor(web): drop swr and migrate share/chat hooks to tanstack query#30232
iamjoel merged 7 commits intomainfrom
refactor/drop-swr

Conversation

@lyzno1
Copy link
Member

@lyzno1 lyzno1 commented Dec 27, 2025

Summary

Fixes #30230

Screenshots

Before After
... ...

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
Copilot AI review requested due to automatic review settings December 27, 2025 04:50
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Dec 27, 2025
@lyzno1 lyzno1 marked this pull request as draft December 27, 2025 04:51
gemini-code-assist[bot]

This comment was marked as outdated.

This comment was marked as outdated.

@langgenius langgenius deleted a comment from gemini-code-assist bot Dec 27, 2025
@lyzno1 lyzno1 marked this pull request as ready for review December 27, 2025 05:12
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Dec 27, 2025
@lyzno1
Copy link
Member Author

lyzno1 commented Dec 27, 2025

/gemini summary

@dosubot dosubot bot added the 💪 enhancement New feature or request label Dec 27, 2025
@lyzno1
Copy link
Member Author

lyzno1 commented Dec 27, 2025

/gemini review

@lyzno1 lyzno1 requested a review from Copilot December 27, 2025 05:12
@gemini-code-assist
Copy link
Contributor

Summary of Changes

This pull request undertakes a significant refactoring effort to standardize the data fetching strategy across the web frontend. It systematically replaces all instances and dependencies on the SWR library with Tanstack Query, particularly within the share and chat functionalities. This migration aims to improve consistency, maintainability, and potentially performance by leveraging Tanstack Query's robust caching and state management capabilities.

Highlights

  • SWR Removal & Tanstack Query Adoption: The swr library has been completely removed from the project's dependencies and replaced with @tanstack/react-query for data fetching.
  • Centralized Share Hooks: New Tanstack Query hooks for share-related functionalities (conversations, chat lists, conversation naming) have been introduced in web/service/use-share.ts, centralizing data fetching logic.
  • Chat Component Migration: The useChatWithHistory and useEmbeddedChatbot hooks have been refactored to utilize the new Tanstack Query hooks, standardizing their data management.
  • Initializer Renaming & Cleanup: The SwrInitializer component has been renamed to AppInitializer and its SWR-specific configuration and imports have been removed, streamlining the application's initialization process.
  • Updated Development Scripts & Documentation: Internal development scripts and documentation related to component analysis and refactoring guidelines have been updated to reflect the shift from SWR to Tanstack Query.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • .claude/skills/component-refactoring/SKILL.md
    • Removed the specific mention of "Project is migrating from SWR to React Query" from the Dify Convention for data hooks.
  • web/app/(commonLayout)/layout.tsx
    • Replaced SwrInitializer import with AppInitializer.
    • Replaced the SwrInitializer component with AppInitializer in the JSX.
  • web/app/account/(commonLayout)/layout.tsx
    • Replaced SwrInitor import with AppInitializer.
    • Replaced the SwrInitor component with AppInitializer in the JSX.
  • web/app/components/base/chat/chat-with-history/hooks.spec.tsx
    • Added a new test file for useChatWithHistory hooks, including tests for share queries, new conversation completion, chat list gating, and conversation ID persistence, all using @tanstack/react-query.
  • web/app/components/base/chat/chat-with-history/hooks.tsx
    • Removed useSWR import.
    • Replaced fetchChatList, fetchConversations, and generationConversationName imports with new Tanstack Query hooks (useShareConversations, useShareChatList, useShareConversationName, useInvalidateShareConversations) from web/service/use-share.
    • Migrated data fetching logic for pinned conversations, unpinned conversations, and chat lists from useSWR to their respective useShare* Tanstack Query hooks.
    • Replaced mutateAppConversationData and mutateAppPinnedConversationData calls with invalidateShareConversations().
  • web/app/components/base/chat/embedded-chatbot/hooks.spec.tsx
    • Added a new test file for useEmbeddedChatbot hooks, including tests for share queries, new conversation completion, chat list gating, and conversation ID persistence, all using @tanstack/react-query.
  • web/app/components/base/chat/embedded-chatbot/hooks.tsx
    • Removed useSWR import.
    • Replaced fetchChatList, fetchConversations, and generationConversationName imports with new Tanstack Query hooks (useShareConversations, useShareChatList, useShareConversationName, useInvalidateShareConversations) from web/service/use-share.
    • Migrated data fetching logic for pinned conversations, unpinned conversations, and chat lists from useSWR to their respective useShare* Tanstack Query hooks.
    • Replaced mutateAppConversationData calls with invalidateShareConversations().
  • web/app/components/swr-initializer.tsx
    • File renamed to web/app/components/app-initializer.tsx.
    • Removed SWRConfig import and usage.
    • Removed export default SwrInitializer.
    • Renamed SwrInitializerProps to AppInitializerProps and SwrInitializer to AppInitializer.
  • web/knip.config.ts
    • Updated the entry for the initializer component from swr-initializer.tsx to app-initializer.tsx.
  • web/package.json
    • Removed the swr dependency.
  • web/pnpm-lock.yaml
    • Removed all entries related to the swr package.
  • web/scripts/analyze-component.js
    • Removed the hasSWR feature detection.
    • Updated API call mocking suggestions to remove useSWR and include useMutation.
  • web/scripts/component-analyzer.js
    • Removed hasSWR property from the analysis result.
    • Updated hasAPI detection to include hasReactQuery instead of useSWR.
    • Refined hasReactQuery detection to include various useQuery variants.
  • web/scripts/refactor-component.js
    • Removed the hasSWR check and the corresponding migration suggestion.
    • Updated refactoring guidelines to simply state "Use React Query for data fetching" instead of "not SWR".
    • Removed the specific action item "MIGRATE SWR TO REACT QUERY".
  • web/service/annotation.ts
    • Removed Fetcher type import from swr.
    • Updated annotationBatchImport and checkAnnotationBatchImportProgress function signatures to remove Fetcher type annotations.
  • web/service/apps.ts
    • Updated comment for fetchAppDetailDirect from "non-SWR usage" to "one-off usage".
  • web/service/plugins.ts
    • Removed Fetcher type import from swr.
    • Updated fetchMarketplaceCollections and fetchMarketplaceCollectionPlugins function signatures to remove Fetcher type annotations.
  • web/service/use-share.ts
    • Introduced shareQueryKeys for consistent query key management.
    • Added useShareConversations hook for fetching conversation lists (pinned and unpinned).
    • Added useShareChatList hook for fetching chat messages for a specific conversation.
    • Added useShareConversationName hook for generating conversation names.
    • Added useInvalidateShareConversations hook to invalidate conversation-related queries.
    • Updated useGetWebAppAccessModeByCode, useGetWebAppInfo, useGetWebAppParams, and useGetWebAppMeta to use shareQueryKeys.
Activity
  • lyzno1 requested a summary of the pull request.
  • lyzno1 requested a review of the pull request.
  • gemini-code-assist[bot] provided a review comment suggesting to memoize the useInvalidateShareConversations hook using useCallback and useQueryClient to prevent unnecessary re-renders, highlighting a potential performance issue with the current implementation.
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant and well-executed refactoring that removes the SWR dependency and migrates the share/chat data fetching logic to TanStack Query. The changes are clean, consistent, and follow best practices for using TanStack Query, such as creating custom hooks and centralized query keys. The addition of comprehensive test suites for the new hooks is commendable and greatly improves the robustness of the codebase.

My review focuses on ensuring the new data fetching logic is sound. I've identified a potential issue in the enabled logic within the newly created use-share.ts hooks, which could lead to race conditions and failed API requests. The proposed fixes will make these hooks more robust. Overall, this is an excellent contribution that modernizes the data fetching layer.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 18 out of 19 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • web/pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lyzno1 lyzno1 marked this pull request as draft December 27, 2025 05:24
@lyzno1 lyzno1 marked this pull request as ready for review December 29, 2025 02:45
@iamjoel iamjoel marked this pull request as draft December 29, 2025 03:18
@iamjoel iamjoel marked this pull request as ready for review December 29, 2025 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:XXL This PR changes 1000+ lines, ignoring generated files.

3 participants