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

Provide callbacks in serviceWorker (next) #3867

Merged
merged 2 commits into from
Jan 20, 2018

Conversation

piotr-cz
Copy link
Contributor

@piotr-cz piotr-cz commented Jan 19, 2018

This is PR version of #3375 for next branch.

Adding two callbacks in serviceWorker.js file:

  • onUpdate - executed when service worker has been updated in the background
    (console logs New content is available; please refresh.)
  • onSuccess - executed after service worker has been installed for the first time
    (console logs Content is cached for offline use.)

To use it inside application to show notification (toast/ icon etc) register service worker inside App.js file:

// App.js

import { register as registerServiceWorker } from './serviceWorker.js'

export default class App {
  constructor() {
    // Register service worker and add onUpdate callback
    registerServiceWorker({
      onUpdate: this.handleServiceWorkerUpdate
    })
  }

  // Simplest notification implementation example.
  handleServiceWorkerUpdate(registration) {
    if (window.confirm('New update has been installed, click to restart')) {
      window.location.reload()
    }
  }
}

@piotr-cz piotr-cz changed the title Provide callbacks in serviceWorker Provide callbacks in serviceWorker (next) Jan 19, 2018
@gaearon gaearon added this to the 2.0.0 milestone Jan 20, 2018
@gaearon gaearon merged commit 39126a1 into facebook:next Jan 20, 2018
@gaearon
Copy link
Contributor

gaearon commented Jan 20, 2018

This looks good, thank you!

akstuhl pushed a commit to akstuhl/create-react-app that referenced this pull request Mar 15, 2018
* Provide callbacks in serviceWorker

* revert exporting default function
zmitry pushed a commit to zmitry/create-react-app that referenced this pull request Sep 30, 2018
* Provide callbacks in serviceWorker

* revert exporting default function
@piotr-cz piotr-cz deleted the feature/sw-callbacks branch October 9, 2018 09:54
@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants