TransWikia.com

Use Cloudflare Worker to avoid preflight requests

Server Fault Asked by cabz on February 27, 2021

I have a statically hosted SPA at app.example.com, communicating with a Rest API at api.example.com. I use Cloudflare as reverse proxy in front of both.

I would like to avoid preflight requests to be sent to the API when using the app in the browser, and have therefore created the following Cloudflare Worker acting on route app.example.com/

addEventListener("fetch", event => {  
  var url = new URL(event.request.url);
  if (url.pathname.startsWith('/api/')) {
    url.host = 'api.example.com';
    var newRequest = new Request(url, event.request);
    event.respondWith(fetch(newRequest))
  } else {
    event.respondWith(fetch(event.request))
  }
})

This makes it possible to connect to app.example.com/api instead hence avoiding any preflights to be sent. My question is if this in any way is bad practice, and if there are any concerns I should be aware of?

Thanks

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP