Skip to content

refactor(web): extract isServer/isClient utility & upgrade Node.js to 22.12.0#30803

Merged
lyzno1 merged 10 commits intomainfrom
refactor/web-isserver-utility
Jan 12, 2026
Merged

refactor(web): extract isServer/isClient utility & upgrade Node.js to 22.12.0#30803
lyzno1 merged 10 commits intomainfrom
refactor/web-isserver-utility

Conversation

@lyzno1
Copy link
Member

@lyzno1 lyzno1 commented Jan 10, 2026

Summary

Why not use TanStack Query's isServer?

TanStack Query exports:

export const isServer = typeof window === 'undefined' || 'Deno' in globalThis

We don't need Deno detection because:

  • Dify is a Next.js application running on Node.js
  • Deno is a different JavaScript runtime that Dify doesn't target
  • TanStack Query includes Deno check because it's a universal library supporting multiple runtimes
  • Our application code only needs to handle our own runtime environments

Changes

New File

  • web/utils/client.ts - Exports isServer and isClient boolean constants

Migrated Files

File Occurrences
featured-tools.tsx 3
featured-triggers.tsx 3
rag-tool-recommendations/index.tsx 3
embedded-chatbot/header/index.tsx 1
query-client.tsx 1
use-trigger-events-limit-modal.ts 1
list.tsx 1
gtag.ts 1
use-query-params.ts 1

Node.js Version Update

File Change
.nvmrc 22.11.022.20.0
web/package.json engines >=v22.11.0>=22.12.0

This fixes jsdom 27.3.0 + parse5 8.0.0 ESM compatibility. Node.js 22.12.0+ has require(esm) enabled by default.

Benefits

  • ✂️ DRY principle: 16 repeated checks → 1 utility function
  • 🧠 Better readability: if (isServer) is clearer than if (typeof window === 'undefined')
  • 🔄 Improved consistency: unified environment detection pattern
  • 🛠️ Easier maintenance: change logic in one place if needed
  • 🐛 Fewer typos: no risk of misspelling 'undefined'

Test plan

  • pnpm lint:fix passes
  • pnpm type-check:tsgo passes
  • pnpm test passes (27/27 tests)
  • Manual testing of affected components (localStorage persistence, SSR behavior)

Closes #30802
Closes #30807

…onment detection Centralize server/client environment detection by introducing a dedicated utility file instead of repeating `typeof window === 'undefined'` checks across the codebase. This improves code maintainability and consistency across 8 files with 15 occurrences. Closes #30802
Copilot AI review requested due to automatic review settings January 10, 2026 04:52
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. 💪 enhancement New feature or request labels Jan 10, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lyzno1, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the web application's environment detection mechanism by consolidating server-side and client-side checks into a dedicated utility. The primary goal is to standardize how the application determines its runtime context, moving away from repetitive and verbose typeof window checks. This change aims to make the codebase more robust, easier to understand, and simpler to maintain by centralizing a common pattern.

Highlights

  • Centralized Environment Detection: Introduced a new utility file (web/utils/client.ts) that exports isServer and isClient boolean constants for consistent runtime environment detection.
  • Code Modernization: Replaced 15 instances of the typeof window === 'undefined' pattern with the newly created isServer utility across 8 different files, streamlining environment checks.
  • Improved Maintainability: This refactoring enhances code maintainability, reduces duplication, and lowers cognitive load by providing a single, clear source for determining if the code is running on the server or client.

🧠 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.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

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 effectively refactors environment detection by introducing isServer and isClient utilities, which significantly improves code consistency and maintainability. The implementation is solid, and the changes have been applied correctly across multiple files. My review includes a few suggestions to further refine the code by removing some redundant logic that became apparent through this refactoring and a small improvement to the new utility file.

Missed migration from the initial refactoring - removes local isClient variable definition and imports from the centralized utility.
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

This PR introduces a centralized environment detection utility to replace scattered typeof window === 'undefined' checks across the codebase. The refactor consolidates server/client detection logic into a single source of truth, improving maintainability and code consistency.

Changes:

  • Created new utility file web/utils/client.ts exporting isServer and isClient constants
  • Replaced 15 inline environment checks with imports from the new utility across 8 files
  • Updated localStorage persistence logic and SSR guards to use the centralized utility

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
web/utils/client.ts New utility file defining isServer and isClient constants with documentation
web/utils/gtag.ts Replaced inline window check with isServer for Google Analytics guard
web/hooks/use-query-params.ts Updated query parameter clearing function to use isServer
web/context/query-client.tsx Updated query client instantiation guard to use isServer
web/context/hooks/use-trigger-events-limit-modal.ts Updated modal hook's SSR guard to use isServer
web/app/components/workflow/block-selector/rag-tool-recommendations/index.tsx Replaced localStorage access guards with isServer in collapse state management
web/app/components/workflow/block-selector/featured-triggers.tsx Updated collapse state persistence guards to use isServer
web/app/components/workflow/block-selector/featured-tools.tsx Updated collapse state persistence guards to use isServer
web/app/components/base/chat/embedded-chatbot/header/index.tsx Replaced inline isClient definition with import from utility
web/app/components/apps/list.tsx Updated query parameter normalization guard to use isServer

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

- Update .nvmrc from 22.11.0 to 22.20.0 (lts-latest) - Update web/package.json engines from >=v22.11.0 to >=22.12.0 This fixes jsdom 27.3.0 + parse5 8.0.0 ESM compatibility issues. Node.js 22.12.0+ has require(esm) enabled by default, which allows CommonJS modules to require ES modules. Closes #30807
@lyzno1 lyzno1 changed the title refactor(web): extract isServer/isClient utility for consistent environment detection refactor(web): extract isServer/isClient utility & upgrade Node.js to 22.12.0 Jan 10, 2026
@lyzno1
Copy link
Member Author

lyzno1 commented Jan 10, 2026

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

lyzno1 and others added 3 commits January 10, 2026 15:14
Removed documentation comments for server and client detection utilities.
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jan 12, 2026
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:M This PR changes 30-99 lines, ignoring generated files.

3 participants