medusa icon indicating copy to clipboard operation
medusa copied to clipboard

Can't use Medusa in service workers or Cloudflare Workers due to Axios limitations

Open drewdecarme opened this issue 3 years ago • 0 comments

Issue

I was interested in deploying my application in a serverless environment, more specifically Cloudflare Workers. At the moment, Axios is the HTTP library used for interacting with the Medusa API via the client that's explored from this package.

Service Workers can only operate using fetch and don't support XMLHttpRequest. In addition, the stable method for deploying Cloudflare Workers is via service-worker. So, unfortunately, Axios cannot be used in in PWA service workers or Cloudflare Workers.

Here's a the main issue: https://github.com/axios/axios/issues/1219

The primary maintainer of Axios just said in October that they're going to be moving to a V1 which would support a `fetch based adapter in order to use it in different deployment contexts such as Cloudflare Workers. However, if you scroll down to his comment here,

Version 1 will very easily be a year long project, we can still add features, make fixes etc as long as we don't break anything in terms of BC.

So converting Axios is a long way out in order to use Medusa in its current state in environments that only support the fetch API. This is kind of a super blocker for using one of the more popular methods of deploying serverless applications.

Potential Solutions

  1. Update the instance of Axios you're using to the fork of Axios that Contentful uses, which has a patch already added to it to use fetch. Read more on the fork here: https://github.com/contentful/contentful.js/pull/396
  2. Update the Medusa App to use plain old fetch instead of using Axios. This would probably require some breaking changes as that the types and a few of the interfaces would have to change surrounding errors, replies, and responses.
  3. Update the client options so that fetch can be used instead of Axios without re-writing the entire module

Thoughts?

drewdecarme avatar Dec 01 '21 16:12 drewdecarme