Home » Web Dev » CORS Headers, Preflight & Performance (ie. How to get rid of the OPTIONS calls)

About Brian ONeill

Brian ONeill

CORS Headers, Preflight & Performance (ie. How to get rid of the OPTIONS calls)

Monetate personalizes hundreds of millions of page views each day. To do that, we take hundreds of data points into account (e.g. weather, geolocation, inventory information, population density, past behavior, etc.), make a decision, and then personalize the page (e.g. content, product recommendations, etc).

But this means that our client’s web sites don’t render until after we’ve made our decisions, which means we need to be fast.

How fast you ask? 12 milliseconds per decision fast.
And in that kind of environment, every millisecond counts.

To personalize each page, the browser reaches out to Monetate servers for decisions/actions prior to render. The web page contacts Monetate servers via a cross-origin request, which means
Cross-Origin Resource Sharing (CORS) comes into play. The CORS interaction comes with a “preflight” request that basically amounts to the client asking the server if it can handle a cross-origin request. If the server replies in the affirmative, then the client sends the actual request. Browsers issue preflight requests for potentially “dangerous” requests.

For more information on the motivation behind CORs, see his stack overflow.

Note specifically:

“New servers that are written with an awareness of CORS. According to standard security practices, the server has to protect its resources in the face of any incoming request — servers can’t trust clients to not do malicious things. This scenario doesn’t benefit from the preflight mechanism: the preflight mechanism brings no additional security to a server that has properly protected its resources.”

This is our scenario. Our servers are not only CORs aware, but they are purpose-built to handle cross-origin mutating requests. Thus, in our case the pre-flight request is pure overhead without benefit. (costing tens if not hundreds of milliseconds!)

To eliminate that pesky preflight request, we need to convince the browser that this is not a “dangerous” request. To do that..

Some people change their API, see: Two Strategies for Crossing Origins with Performance in Mind.
Some people use proxies, see: Avoiding pre-flight OPTIONS calls on CORS requests
Some people try lots of things, see: Killing CORS Preflight Requests on a React SPA

Of those options, I don’t like changing the API, because our API is consumed from lots of different channels. (Mobile apps, etc.) Having two different APIs to maintain, develop, etc. just to accommodate CORs seems like really bad decision. Meanwhile, proxies would just introduce latency without any added value.

Enter the content-type header…

In our scenario, the browser flags our request as “dangerous” because it contains a JSON object. The browser knows it contains a JSON object because the content-type header is set to application/json. Changing the value of this header to text/plain allows the browser to send the request with no preflight!

So, we did that.
Boom… instant performance improvement.
We shaved almost a hundred milliseconds off of our request times!

Now, I would have preferred a standard way of communicating to the browser that it can “trust this server”, but if there was such a method, then a nefarious individual that was trying to issue the errant request against an unsuspecting server could use that same method to bypass the safety check. Oh well. Oh well.

At this point, it seems to be best practice to eliminate the preflight request in performance sensitive scenarios where there is no benefit to the added check, and changing the content-type seems the least intrusive way of doing that. For you purists out there, you’ll just need to squint a bit, and keep telling yourself that JSON is a form of text/plain. ;)

Happy hacking.

Published on Web Code Geeks with permission by Brian ONeill, partner at our WCG program. See the original article here: CORS Headers, Preflight & Performance (ie. How to get rid of the OPTIONS calls)

Opinions expressed by Web Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of