Ember CLI Server Variables

4.0.0 · active · verified Tue Apr 21

ember-cli-server-variables is an Ember CLI addon designed to inject server-driven variables into the `<head>` section of an Ember application's `index.html` file during the build process. These variables are typically used for dynamic configurations like API tokens, user location, or other environment-specific data provided by the application server. The current stable version is 4.0.0, which requires Ember.js v4.12+ and Node.js v18+. The addon's release cadence is generally tied to Ember CLI and Ember.js major version updates, ensuring compatibility with the latest ecosystem standards. It differentiates itself by providing a convenient service for accessing these variables within the Ember application, abstracting away the parsing of meta tags. Its core functionality involves configuring a list of expected variables in `config/environment.js`, optionally providing development defaults, and then consuming them via an injected service. Since version 2.0.0, it automatically parses JSON string values in the meta tags, simplifying data retrieval for structured configuration.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure server variables and their development defaults in `config/environment.js`, and then access them within an Ember Octane component using the `@service` decorator, including both string and automatically parsed JSON variables.

/* config/environment.js */
module.exports = function(environment) {
  let ENV = {
    modulePrefix: 'my-ember-app', // Used as default tagPrefix if not explicitly set
    // ... other ENV settings for your Ember application
    serverVariables: {
      tagPrefix: 'my-app',
      vars: ['app-token', 'user-location', 'json-config'] // Define variables expected from server
    }
  };

  if (environment === 'development') {
    // Provide default values for development environment
    ENV.serverVariables.defaults = {
      'app-token': 'dev-secret-token-123',
      'user-location': 'Localhost, Earth',
      'json-config': '{"featureA":true,"level":5}' // JSON strings are auto-parsed
    };
  }

  return ENV;
};

/* app/components/variable-display.js (example modern Ember Octane component) */
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { tracked } from '@glimmer/tracking';

export default class VariableDisplayComponent extends Component {
  @service serverVariables; // Inject the service

  @tracked token = '';
  @tracked location = '';
  @tracked config = {};

  constructor() {
    super(...arguments);
    // Access variables; properties are camelCased from dasherized config
    this.token = this.serverVariables.appToken; 
    this.location = this.serverVariables.userLocation;
    this.config = this.serverVariables.jsonConfig; // Automatically parsed JSON
    
    console.log('Server Variables Loaded:', {
      appToken: this.token,
      userLocation: this.location,
      jsonConfig: this.config
    });
    // Render these variables in your component's template (e.g., this.token in {{this.token}})
  }
}

view raw JSON →