@spoolcms/nextjs
Version:
The beautiful headless CMS for Next.js developers
67 lines (66 loc) • 2 kB
JavaScript
;
/**
* Environment detection utilities for Spool CMS
* Provides reliable detection of server vs client context and environment settings
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectEnvironment = detectEnvironment;
exports.isServerContext = isServerContext;
exports.isClientContext = isClientContext;
exports.isDevelopmentMode = isDevelopmentMode;
exports.isProductionMode = isProductionMode;
exports.getEnvironmentCacheKey = getEnvironmentCacheKey;
/**
* Detect the current execution environment
* This function works reliably in both server and client contexts
*/
function detectEnvironment() {
// Server vs Client detection
const isServer = typeof window === 'undefined';
const isClient = !isServer;
// Environment detection
const nodeEnv = process.env.NODE_ENV;
const isDevelopment = nodeEnv === 'development';
const isProduction = nodeEnv === 'production';
// React Strict Mode detection (helps with double rendering issues)
const isReactStrictMode = isDevelopment && isClient;
return {
isServer,
isClient,
isDevelopment,
isProduction,
isReactStrictMode,
};
}
/**
* Check if we're running in a server context
*/
function isServerContext() {
return typeof window === 'undefined';
}
/**
* Check if we're running in a client context
*/
function isClientContext() {
return typeof window !== 'undefined';
}
/**
* Check if we're in development mode
*/
function isDevelopmentMode() {
return process.env.NODE_ENV === 'development';
}
/**
* Check if we're in production mode
*/
function isProductionMode() {
return process.env.NODE_ENV === 'production';
}
/**
* Get a cache key suffix based on environment
* This helps prevent cache collisions between different environments
*/
function getEnvironmentCacheKey() {
const env = detectEnvironment();
return `${env.isServer ? 'server' : 'client'}-${env.isDevelopment ? 'dev' : 'prod'}`;
}