Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ServiceWorker being replaced after page reload #20

Open
mwidmann opened this issue Oct 27, 2020 · 4 comments
Open

ServiceWorker being replaced after page reload #20

mwidmann opened this issue Oct 27, 2020 · 4 comments

Comments

@mwidmann
Copy link

I am facing an issue with using the OneSignal module. Sometimes after a page reload the service worker in use is the OneSignalServiceWorker.js, sometimes sw.js with an error in the console

installWorker - SW's 'controllerchange' fired but no state change!

In this case running OneSignal.sendSelfNotification() from the console in the DevTools produces an error

Uncaught (in promise) g: This operation can only be performed after the user is subscribed.
    at Function.<anonymous> (https://cdn.onesignal.com/sdks/OneSignalPageSDKES6.js?v=151104:1:250797)
    at Generator.next (<anonymous>)
    at r (https://cdn.onesignal.com/sdks/OneSignalPageSDKES6.js?v=151104:1:716)

To reproduce the effect I performed the following steps:

npx create-nuxt-app nuxt-test-push-notifications

The settings are as follows:

create-nuxt-app v3.4.0
✨  Generating Nuxt.js project in nuxt-test-push-notifications
? Project name: nuxt-test-push-notifications
? Programming language: JavaScript
? Package manager: Npm
? UI framework: None
? Nuxt.js modules: Progressive Web App (PWA)
? Linting tools: (Press <space> to select, <a> to toggle all, <i> to invert selection)
? Testing framework: None
? Rendering mode: Universal (SSR / SSG)
? Deployment target: Server (Node.js hosting)
? Development tools: jsconfig.json (Recommended for VS Code if you're not using typescript)
? What is your GitHub username? martin widmann
? Version control system: Git

After all the setup is done, I changed nuxt.config.js to look like this:

export default {
  server: {
    port: 8080,
    host: '0.0.0.0',
  },
  head: {
    title: 'nuxt-test-push-notifications',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ]
  },

  css: [
  ],

  plugins: [
  ],

  components: true,

  buildModules: [
  ],

  modules: [
    '@nuxtjs/onesignal',
    '@nuxtjs/pwa',
  ],

  oneSignal: {
    init: {
      appId: 'REDACTED',
      allowLocalhostAsSecureOrigin: true,
    }
  },

  pwa: {
    workbox: {
      config: {
        debug: true
      }
    }
  },

  build: {
  }
}

I then run it with npm run build && npm run start to not fall into the self destroying sw mode.

I set up a test project in OneSignal and for local SSL I'm proxying the nodejs server running on localhost:8080 through an Apache with self signed (and registerd) SSL certs running on port 80.

When I reload the page leaving the Application Tab open, I can see the Service Worker being OneSignalServiceWorker.js and directly being replaced by sw.js.

Any idea why I am facing this issue?

@mwidmann
Copy link
Author

Found this issue on the pwa-module issue tracker which seems to be related to my problem. Seems that adding the following

  pwa: {
    workbox: {
      swURL: 'OneSignalSDKWorker.js',
      ...
    }
  },

To the nuxt.config.js fixes this issue for me. At least the sw is not replaced anymore and I can test push notifications with OneSignal.sendSelfNotification() any time I tried.

@i-defranca
Copy link

any updates on this?

@lopermo
Copy link

lopermo commented Jun 7, 2021

Still impossible to use this module...
Can @pi0 take a look, please?

@Oldwo1f
Copy link

Oldwo1f commented Sep 22, 2021

i have the same issue, sometimes i get onesignal service worker (and i am able to recieve push) sometimes i get the normal sw.js (and i am not able to recieve push)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants