From 259599ddcc08abafb1d291cb26fa90f66cd937d1 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Fri, 3 Oct 2025 18:48:46 +0200 Subject: [PATCH] v0.0.15: Fix SSR warning in client components MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes misleading warning that appeared during Next.js SSR: - Client components are initially rendered on server where window is undefined - This is expected behavior and doesn't require a warning - Now silently falls back to relative URLs during SSR - Properly uses window.location.origin once hydrated on client The hooks now work seamlessly in: - Pure client-side apps - Next.js with SSR/SSG - Server components (with explicit serverURL) - Client components (auto-detects after hydration) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- package.json | 2 +- src/hooks/client.ts | 36 ++++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 778c1ad..6cb8c3f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xtr-dev/payload-feature-flags", - "version": "0.0.14", + "version": "0.0.15", "description": "Feature flags plugin for Payload CMS - manage feature toggles, A/B tests, and gradual rollouts", "license": "MIT", "type": "module", diff --git a/src/hooks/client.ts b/src/hooks/client.ts index 1ea345c..b7c7167 100644 --- a/src/hooks/client.ts +++ b/src/hooks/client.ts @@ -21,23 +21,31 @@ export interface FeatureFlagOptions { // Helper to get config from options or defaults function getConfig(options?: FeatureFlagOptions) { - // In server-side environments, serverURL must be explicitly provided - const serverURL = options?.serverURL || - (typeof window !== 'undefined' ? window.location.origin : undefined) - - if (!serverURL) { - console.warn( - 'FeatureFlags: serverURL must be provided when using hooks in server-side environment. ' + - 'Falling back to relative URL which may not work correctly.' - ) - // Use relative URL as fallback - will work if API is on same domain - return { serverURL: '', apiPath: options?.apiPath || '/api', collectionSlug: options?.collectionSlug || 'feature-flags' } + // Check if serverURL is explicitly provided + if (options?.serverURL) { + return { + serverURL: options.serverURL, + apiPath: options.apiPath || '/api', + collectionSlug: options.collectionSlug || 'feature-flags' + } } - const apiPath = options?.apiPath || '/api' - const collectionSlug = options?.collectionSlug || 'feature-flags' + // In browser environment, use window.location.origin + if (typeof window !== 'undefined') { + return { + serverURL: window.location.origin, + apiPath: options?.apiPath || '/api', + collectionSlug: options?.collectionSlug || 'feature-flags' + } + } - return { serverURL, apiPath, collectionSlug } + // During SSR or in non-browser environments, use relative URL + // This will work for same-origin requests + return { + serverURL: '', + apiPath: options?.apiPath || '/api', + collectionSlug: options?.collectionSlug || 'feature-flags' + } } /**