Specify The Rails API Host for Ember Production Deployment

I’m not going to go through ALL the steps to deploy an Ember and Rails app to production (yet), but wanted to quickly cover some of the Ember steps I needed to take in order to deploy the Beverage app we made in my previous post.

In development using the terminal we would pass in the --proxy flag to ember s which tells Ember where to fetch data from, but in productions we aren’t using ember s so we need to add the location of our Rails API to the config/environment.js file. Find the ‘production’ section and add an environment variable for storing the url to your Rails API.

//config/environment.js
//...

if (environment === 'production') {
  // here you can enable a production-specific feature
  ENV.APP.host = 'https://api.beverage.blake.app'
}

//...

Now we have to tell Ember Data how to access this variable. Because we are using JSON API (the default adapter) for Ember Data we don’t actually have a place for this yet and we need to override the default by creating an adapter.

mkdir app/adapters
touch app/adapters/application.js

In order to use our environment variable we need to import it, then we can set our host variable.

// app/adapters/application.js
import DS from 'ember-data';
import ENV from 'ember-form/config/environment';

const { JSONAPIAdapter } = DS;

export default class ApplicationAdapter extends JSONAPIAdapter {
  host = ENV.APP.host;
}

Now when you run ember build --prod it will contain the host for your Rails API, and in development you can continue to use the --proxy flag with ember s.

Ohh it turns out this will soon not be the default behavior because it has been deprecated in Ember 3.x and you will need to explicitly create your adapters and serializers.

While making some small improvements to the beverage app I noticed some deprecation warnings in the console and thought I should look into them:

DEPRECATION: store.serializerFor(“beverage”) resolved the “-json-api” serializer via the deprecated adapter.defaultSerializer property.

Previously, if no application or type-specific serializer was specified, the store would attempt to lookup a serializer via the defaultSerializer property on the type’s adapter. This behavior is deprecated in favor of explicitly defining a type-specific serializer or application serializer [deprecation id: ember-data:default-serializer] See Ember.js - Deprecations for more details.

At first I thought this was for the new adapter we added, but it turns out that probably fixed one of the deprecations and what I needed to do to get rid of this warning is to add a new serializer like so:

// app/serializers/application.js
export { default } from '@ember-data/serializer/json-api';

Even though json-api is currently the default, it sounds like there won’t be a default anymore and we will have to specify which adapters and serializers we are using.