OPTIONS request makes application 2x slower?

api, cors, single-page-application

Solution

It probably won't make your entire application 2x slower. It will sometimes issue 2 http requests when you might expect one. But your app is probably more than just HTTP requests, so you'd have to measure the performance of your app as a whole.

The conditions for the browser issuing a preflight are:

- The HTTP method is not a simple method (`GET`, `HEAD`, `POST`), or

- There are HTTP headers other than `Accept`, `Accept-Language`, `Content-Language` or `Content-Type` (but only if the `Content-Type` value is not `application/x-www-form-urlencoded`, `multipart/form-data`, or `text/plain`)

If your HTTP request doesn't meet those criteria, it will not issue a preflight. The preflight is a small OPTIONS request without a body, so it should be fast (depending on your connection speed). And once you issue a preflight, its results are cached for a period of time (the cache time varies by browser. Chrome/Safari do 5 minutes, FF does 24 hours).

If you are interested in tips for reducing preflights, see this answer: How to apply CORS preflight cache to an entire domain

Problem

I have very intensive single-page application that is using API. Let's say application is located at `application.com`. Now if I place API in `api.application.com` it will enable CORS thus all browsers will do `OPTIONS` request before the actual request. Does this makes my application 2x slower?

Original source

Related problems