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

Closed
opened 2026-02-21 20:52:24 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/30803

State: closed
Merged: Yes


Summary

  • Created centralized isServer/isClient utility at /web/utils/client.ts
  • Migrated 16 occurrences of typeof window === 'undefined' across 9 files to use the new utility
  • Improves code maintainability, consistency, and reduces cognitive load
  • Upgraded Node.js minimum version to 22.12.0 for jsdom 27 compatibility (see #30807)

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/30803 **State:** closed **Merged:** Yes --- ## Summary - Created centralized `isServer`/`isClient` utility at `/web/utils/client.ts` - Migrated 16 occurrences of `typeof window === 'undefined'` across 9 files to use the new utility - Improves code maintainability, consistency, and reduces cognitive load - **Upgraded Node.js minimum version to 22.12.0** for jsdom 27 compatibility (see #30807) ## Why not use TanStack Query's `isServer`? TanStack Query exports: ```typescript 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.0` → `22.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 - [x] `pnpm lint:fix` passes - [x] `pnpm type-check:tsgo` passes - [x] `pnpm test` passes (27/27 tests) - [ ] Manual testing of affected components (localStorage persistence, SSR behavior) Closes #30802 Closes #30807
yindo added the pull-request label 2026-02-21 20:52:24 -05:00
yindo closed this issue 2026-02-21 20:52:24 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32962