-
Notifications
You must be signed in to change notification settings - Fork 8
Performance: Bypass Ember's normalizeResponse
The default DS.JSONAPISerializer class that comes with Ember Data works great out of the box with Sinja and JSONAPI::Serializers. However, it performs a lot of costly, unnecessary work in order to normalize already-mostly-normalized {json:api} responses. If your Ember application primarily talks to Sinja, and your Sinja application primarily talks to Ember, you may be able to bypass the normalizeResponse hook in your application-wide DS.JSONAPISerializer entirely and greatly speed things up.
-
Write your base serializer like this to singularize types and camelCase attribute and relationship names while preserving the default link generation behavior:
require 'jsonapi/ember_serializer' class BaseSerializer include JSONAPI::EmberSerializer # .. end
-
Generate an application-wide serializer (if one doesn't already exist):
$ ember generate serializer application
-
Add this method to the generated serializer:
normalizeResponse(_store, _primaryModelClass, payload, _id, _requestType) { return payload; }
Add
/* jshint unused:vars */
to the top of the file to ignore the JSHint warning, or/* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */
to ignore the ESLint warning.
You can still, of course, customize the JSONAPI::Serializers in your Sinja application and the individual DS.JSONAPISerializer classes in your Ember application if you need to do anything gnarly. This just sets a high-performing default for the "happy path."
Please note that all your {json:api} includes will have to be camelCase going forward to be compatible with this change. If your primary target is Ember, this shouldn't be a problem!
Hat tip to Chris Thoburn for sharing this awesome performance tip on Ember Weekend!