becem-gharbi / nuxt-fcm

Firebase Cloud Messaging for Nuxt
MIT License
20 stars 2 forks source link

Cannot register service worker if not on root #15

Closed japereira94 closed 4 months ago

japereira94 commented 4 months ago

I have a nuxt app that is deployed to a subfolder "example". When opening the app I get the following message.

FirebaseError: Messaging: We are unable to register the default service worker. Failed to register a ServiceWorker for scope ('[HOSTNAME]/firebase-cloud-messaging-push-scope') with script ('[HOSTNAME]/firebase-messaging-sw.js'): A bad HTTP response code (404) was received when fetching the script. (messaging/failed-service-worker-registration).

Is there a way to configure this to use a subfolder as the baseURL is configured like this?

app: {
  baseURL: "/example/"
}
becem-gharbi commented 4 months ago

You want to add a subpath to the service worker script like [host]/[subpath]/firebase-messaging-sw.js. Can you confirm?

We can add a new config option (e.g. serviceWorkerRoute ) with default to /firebase-messaging-sw.js.

https://github.com/becem-gharbi/nuxt-fcm/blob/1be4a9533712f75c10c62224464ddf948ac18d68/src/module.ts#L72