Reducing the impact of preflight requests #223
Labels
A-Client-Server
Issues affecting the CS API
feature
Suggestion for a significant extension which needs considerable consideration
For quick reference, a preflight request is required to be performed by a user agent implementing CORS when any of the following conditions are met:
GET
,POST
orHEAD
Accept
,Accept-Language
,Content-Language
orContent-Type
is set manuallyContent-Type
has a value other thanapplication/x-www-form-urlencoded
,multipart/form-data
, ortext/plain
Since matrix uses
Content-Type: application/json
, all non-GET
requests require a preflight, this means for example that sending any event to the server requires 2 requests, adding additional latency and a bit of overheadUnfortunately there's no perfect solution to the problem, but here are some options:
Proxy requests through the same origin
This one shifts the work onto the client meaning nothing has to change spec wise, but has quite a few issues:
Allow
Content-Type: text/plain
Feels like a hack, but parsing plain text
POST
messages as if they were JSON would mean no preflight request. However this wouldn't help for the common path of sending messages to rooms which usesPUT
Improve preflight cacheability
Preflight requests can be optionally cached by the user agent indicated using the header
Access-Control-Max-Age
. However the cache is per URL not just the origin, meaning that any change in the path (which includes query parameters) warrants another preflight requestThe large issue with this one is that it would require a breaking change to the API, with frequently changing parameters being placed in the JSON body or as HTTP headers
Websockets
Websockets have no such overhead once the connection is established, however whilst they do not require breaking API compatibility it's still a large spec addition
The text was updated successfully, but these errors were encountered: