SSR UtilitiesAPI reference for server-side rendering utility functionsserverAPI Reference
Categories

SSR Utilities

The SSR utilities provide constants for detecting whether the code is running on the server or client side, and defineGlobal for putting a value on the global object without clobbering what is already there.

Constants

isServer

const isServer

A boolean constant indicating whether the current environment is server-side.

Example

import { isServer } from '@semantic-ui/utils';
if (isServer) {
console.log("This code is running on the server");
} else {
console.log("This code is running in the browser");
}

isClient

const isClient

A boolean constant indicating whether the current environment is client-side (browser).

Example

import { isClient } from '@semantic-ui/utils';
if (isClient) {
document.addEventListener('DOMContentLoaded', () => {
console.log("DOM is fully loaded and parsed");
});
}

isDevelopment

const isDevelopment

A boolean constant indicating whether the current environment is development.

Development Mode Detection

Returns true when any of these conditions are met:

Environment Variable Values
Cloud Dev CODESPACE_NAME any value
GITPOD_WORKSPACE_ID any value
Vercel VERCEL_ENV "development", "preview"
Netlify CONTEXT "deploy-preview", "branch-deploy", "dev"
Node.js NODE_ENV "development", "dev", "local", "test"
Vite import.meta.env.DEV true
import.meta.env.MODE any value except "production"
Nuxt process.dev true
React Native __DEV__ true

Example

import { isDevelopment } from '@semantic-ui/utils';
if (isDevelopment) {
console.log('Debug info enabled');
}
// API endpoint selection
const apiUrl = isDevelopment
? 'http://localhost:3000/api'
: 'https://api.production.com';

isCI

const isCI

A boolean constant indicating whether the current environment is a CI/CD pipeline.

CI Platform Detection

Returns true when any of these conditions are met:

Detection Method Variable Values
Generic CI "true", true
GitHub Actions GITHUB_ACTIONS any value
GitLab CI GITLAB_CI any value
Jenkins JENKINS_URL any value
Buildkite BUILDKITE any value
CircleCI CIRCLECI any value
Travis CI TRAVIS any value
AppVeyor APPVEYOR any value
Drone DRONE any value
Semaphore SEMAPHORE any value
TeamCity TEAMCITY_VERSION any value
Azure DevOps TF_BUILD any value
Bamboo BAMBOO_BUILD_KEY any value
AWS CodeBuild CODEBUILD_BUILD_ID any value

Example

import { isCI } from '@semantic-ui/utils';
// Skip interactive prompts in CI
if (isCI) {
console.log('CI detected - using default settings');
} else {
console.log('Local environment - prompting for input');
}
// Different timeouts for CI vs local
const timeout = isCI ? 30000 : 5000;

Functions

defineGlobal

function defineGlobal(name, value, { host = globalThis, onConflict = 'warn', onWarn } = {})

Puts a value on the global object, or any host object, under a name and returns the value the name holds afterward. Defining the same value again is no conflict, so a module re-run under hot reload stays quiet. A name already held by a different value is a conflict, and onConflict decides what happens:

  • 'warn' keeps the existing value and, in development, hands onWarn a message. This is the default, for a value exposed to the console where a collision usually means a second copy of a library
  • 'keep' keeps the existing value quietly. Polyfills, get-or-create singletons that survive hot reloads, and shared queues like dataLayer all want the value that is already there
  • 'replace' overwrites it, including a getter or a read-only configurable property that plain assignment cannot change, as test setup does with fetch or navigator

A name counts as held when name in host is true, so a built-in on the prototype chain (window.status, window.location) is never clobbered by default. The function attaches in every build. When the value is for the console only, fold the call at the callsite with isDevelopment.

onWarn defaults to console.warn, which warns on every conflicting call. For one warning per name, pass the warnOnce of a createLogger, which also prints under your namespace.

Parameters

Name Type Description
name string | symbol The property name
value any The value to define
options object Optional configuration
Options
Name Type Default Description
host object globalThis The object to define on, a library namespace for instance
onConflict string 'warn' 'warn', 'keep' or 'replace'
onWarn function console.warn Receives the conflict message under 'warn', in development only

Returns

The value the name holds after the call: the given value, or the existing one when it was kept.

Example

import { createLogger, defineGlobal, isDevelopment } from '@semantic-ui/utils';
const { warnOnce } = createLogger({ namespace: 'app' });
// reach the store from the devtools console, development builds only, one warning per name
isDevelopment && defineGlobal('store', store, { onWarn: warnOnce });
// get-or-create, a database client that survives dev hot reloads
const db = defineGlobal('db', createClient(), { onConflict: 'keep' });
// append to the page's queue when one exists
defineGlobal('dataLayer', [], { onConflict: 'keep' }).push({ event: 'boot' });
// test setup
defineGlobal('fetch', mockFetch, { onConflict: 'replace' });
// a namespace object instead of the global
defineGlobal('plugins', [], { host: MyLibrary });
Previous
Debug
Next
Equality