mlegenhausen / fetch-intercept

Interceptor library for the native fetch command inspired by angular http intercepts.
MIT License
406 stars 52 forks source link

triger cors when work with swr #72

Open yizhilee opened 1 year ago

yizhilee commented 1 year ago

I have registered an interceptor in my app.js file in react as follows:

fetchIntercept.register({
  response: function (response) {
    if (response.status >= 500 && response.status < 600) {
      bottomToast("server error")
    }
    return response;
  }
});

However, when I use SWR to fetch data, I encounter a CORS error. Here's the code I'm using for the fetch:

const fetcher = (...args) => fetch(...args, { headers:{ accessToken: state.accessToken } }).then((res) => res.json())
const {data: profile} = useSWR(state.isLogin ? `${portalAddress}/user/${state.userId}` : null, fetcher);

It seems that the OPTIONS request is being sent before the fetch request, which is causing the CORS error. It's important to note that this issue only occurs in production mode and not in the development environment. The error message I receive is shown in the attached screenshot.

image