Ember CLI Server Variables
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
-
TypeError: Cannot read properties of undefined (reading 'appToken')
cause The `serverVariables` service was not properly injected into the component or controller attempting to access it, or the variable name was not defined in `config/environment.js`.fixVerify the service is correctly injected using `Ember.inject.service('serverVariables')` (classic) or `@service serverVariables;` (modern Octane). Also, confirm the variable's dasherized name is in `ENV.serverVariables.vars` in `config/environment.js`. -
SyntaxError: Unexpected token 'o' in JSON at position X (or similar JSON parsing error from `JSON.parse`)
cause You are likely attempting to manually `JSON.parse()` a server variable's value after upgrading to version 2.0.0 or later, where the addon automatically handles JSON string parsing.fixRemove any explicit `JSON.parse()` calls for variables fetched from `serverVariablesService` that are expected to be JSON. The service will return JavaScript objects directly for such variables. -
Meta tag for 'my-app-api-key' not found in index.html, or variable not available in service.
cause This error or similar behavior (variable always being an empty string) indicates either the `{{content-for 'server-variables'}}` placeholder is missing, the variable was not included in the `vars` array in `config/environment.js`, or the server serving `index.html` did not populate the meta tag's `content` attribute.fix1. Check `app/index.html` for `{{content-for 'server-variables'}}`. 2. Ensure the variable is listed in `ENV.serverVariables.vars`. 3. Confirm your server-side logic correctly inserts the meta tag with the expected name and content into the generated `index.html`.
Warnings
- breaking Version 4.0.0 introduces breaking changes primarily due to updating Ember to v5.12.0. Your Ember application must meet the updated peer dependency requirements.
- breaking Version 2.0.0 introduced automatic JSON parsing for server variables. If you were previously manually calling `JSON.parse()` on variables retrieved from the service that were expected to be JSON strings, this will now lead to errors or double-parsing.
- gotcha The `{{content-for 'server-variables'}}` tag is crucial and must be present within the `<head>` section of your `app/index.html` file. If this placeholder is missing, the addon cannot inject the necessary `<meta>` tags, and server variables will not be available to your application.
- gotcha The `vars` array within the `serverVariables` block in your `config/environment.js` is a required configuration. Without it, the addon will not know which server variables to look for or expose through its service.
Install
-
npm install ember-cli-server-variables -
yarn add ember-cli-server-variables -
pnpm add ember-cli-server-variables
Imports
- serverVariablesService
import { serverVariablesService } from 'ember-cli-server-variables';import Ember from 'ember'; // In a classic Ember component or controller export default Ember.Component.extend({ serverVariablesService: Ember.inject.service('serverVariables'), // ... }); - Configuration
import config from 'ember-cli-server-variables/environment';
// config/environment.js module.exports = function(environment) { let ENV = { /* ... */ }; ENV.serverVariables = { tagPrefix: 'your-app', vars: ['my-variable', 'json-data'] }; return ENV; };
Quickstart
/* 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}})
}
}