Skip to main content
Version: v5 (Preview)

Build Conditionals

The Build object from @stencil/core tells your code what kind of build it's running in, so it can include or skip work accordingly:

import { Build, Component } from '@stencil/core';

@Component({ tag: 'my-app' })
export class MyApp {
componentDidLoad() {
if (Build.isDev) {
console.log('my-app loaded');
}
}
}
Flagtrue when
Build.isDevThe build is a development build (stencil build --dev).
Build.isBrowserThe code runs in the browser.
Build.isServerThe code runs in the ssr module, for server-side rendering or prerendering.
Build.isTestingThe components are built under a test runner such as Vitest or Playwright, or with NODE_ENV=test.

Code in a branch that a build can never take is removed from that build. A production build drops Build.isDev code, such as extra logging or diagnostics, and the browser and server builds each drop the other's branches, along with anything only those branches import.

Browser and Server​

Code that needs a real browser, such as reading localStorage or measuring layout, should only run in the browser:

connectedCallback() {
if (Build.isBrowser) {
this.theme = localStorage.getItem('theme') ?? 'light';
}
}

See Writing SSR-Friendly Components for more on code that runs on the server.

Testing​

Use Build.isTesting to skip work that gets in the way of tests, such as animations or analytics:

componentDidLoad() {
if (!Build.isTesting) {
this.startAnimation();
}
}