1

I am using ember-data in my app, however, I realize that Ember data will make two separated requests (for example a GET and an OPTIONS) to the server which of course takes a longer time than making a single request.

See screenshot below, the OPTIONS requests take 1 second to complete and total 2 seconds for each user operation.

enter image description here

The code snippet below is what I used to fetch models in one particular route,

model() {
    return this.store.findAll('card');
},

My question is why it makes two requests? and is this necessary to make an OPTIONS request? If not how can I stop let my Ember data making OPTIONS request?

nem035
  • 31,501
  • 5
  • 73
  • 88
X.Creates
  • 11,800
  • 7
  • 58
  • 98

1 Answers1

2

This is known as preflighted request that exists mainly because of security and is required when dealing with CORS.

The browser does this automatically to ensure that the request being done is trusted by the server.

There are two ways to disable it (or at least limit it):

  1. By setting the Access-Control-Max-Age header which will allow results of a preflight request to be cached and reduce the amount of requests.

  2. Convert your request into a simple request which doesn't trigger a preflighted request.

A simple request is one that meets all the following conditions:

  1. The only allowed methods are:

    • GET
    • HEAD
    • POST
  2. The only headers allowed to be manually set are those which the Fetch spec defines as being a CORS-safelisted request-header

If your request uses any other method besides the ones above or sets a non CORS-safelisted header, it will be preflighted automatically.


For reference, the OPTIONS method is defined by the HTTP method definitions RFC like:

The OPTIONS method represents a request for information about the communication options available on the request/response chain identified by the Request-URI. This method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.

Related Question

Community
  • 1
  • 1
nem035
  • 31,501
  • 5
  • 73
  • 88