web-push icon indicating copy to clipboard operation
web-push copied to clipboard

Calling fetch function for push notifications in service worker causes CORS error

Open MrMelon1232 opened this issue 1 year ago • 1 comments
trafficstars

Hi, I have an app that calls a api fetch call for push notifications in the service worker and it returns the follow error: Access to fetch at 'ressource from server' from origin 'domain' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Operating System: Windows Node Version: latest web-push Version: latest

Please select any browsers that you are experiencing problems with:

  • [x] Chrome
  • [x] Firefox
  • [x] Edge

Problem

As mentioned before, the issue is that we are getting a CORS error, but other fetch functions called by the service worker works fine. I was wondering if the vapid keys might have anything to do with it ? On top of this, I only get this issue on my deployed app using vercel and not locally. I've tried to add the following lines to my server file: app.use((req, res, next) => { res.setHeader("Access-Control-Allow-Origin", "*"); next(); }); but unfortunately I still get the error. Since I am not getting an error with my other fetch calls, I was wondering maybe web-push has specifics to setup or any of the sort. Any help would be appreciated!

Features Used

  • [ x] VAPID Support
  • [x ] GCM API Key
  • [ x] Sending with Payload

MrMelon1232 avatar Jan 11 '24 22:01 MrMelon1232