Giter Club home page Giter Club logo

Comments (6)

flybayer avatar flybayer commented on May 22, 2024 12

I converted the service worker code from this template over to basically use what create-react-app uses where the service worker registration code is compiled with webpack (it's not in this template).

CRA progressive web app information

  1. Delete build/service-worker-prod.js and build/service-worker-dev.js and related loading code from index.html and from build/webpack.dev.js and build/webpack.prod.js
  2. Port over src/registerServiceWorker.js from CRA
  3. Add proper import from src/index.js

Then you can import your Vuex store into src/registerServiceWorker.js and dispatch a notification like this:

const notifyUserAboutUpdate = worker =>
  store.dispatch(NEW_NOTIFICATION, {
    text: 'App update available!',
    secondary: true,
    showReloadButton: true,
    timeout: 0,
    onClick: () => worker.postMessage({action: 'skipWaiting'})
  })

function registerValidSW (swUrl) {
  navigator.serviceWorker
    .register(swUrl)
    .then(registration => {
      if (registration.waiting) {
        console.log('New content has been available from last session; please refresh.')
        notifyUserAboutUpdate(registration.waiting)
        return
      }

      registration.onupdatefound = () => {
        const installingWorker = registration.installing
        installingWorker.onstatechange = () => {
          if (installingWorker.state === 'installed') {
            if (navigator.serviceWorker.controller) {
              // At this point, the fresh content will have been added to the cache.
              // It's the perfect time to display a "New content is
              // available; please refresh." message in your web app.
              console.log('New content is available; please refresh.')
              notifyUserAboutUpdate(installingWorker)
            } else {
              // At this point, everything has been precached.
              // It's the perfect time to display a
              // "Content is cached for offline use." message.
              console.log('Content is cached for offline use.')
            }
          }
        }
      }
    })
    .catch(error => {
      console.error('Error during service worker registration:', error)
    })
}

from pwa.

rof20004 avatar rof20004 commented on May 22, 2024

Me too, in fact, I did this and worked on desktop, but not in mobile.

from pwa.

niutech avatar niutech commented on May 22, 2024

There is a simpler way to access your Vue app from the outside. In your main.js prepend before new Vue():

window.App = new Vue({ ... })

Then your App instance is available in the global scope, you can access App.$data, App.$store and so on.

from pwa.

rof20004 avatar rof20004 commented on May 22, 2024

Many thanks @flybayer

from pwa.

LuisHCK avatar LuisHCK commented on May 22, 2024

There is a simpler way to access your Vue app from the outside. In your main.js prepend before new Vue():

window.App = new Vue({ ... })

Then your App instance is available in the global scope, you can access App.$data, App.$store and so on.

It can be much cleaner to use EventBus

from pwa.

gotexis avatar gotexis commented on May 22, 2024

There is a simpler way to access your Vue app from the outside. In your main.js prepend before new Vue():

window.App = new Vue({ ... })

Then your App instance is available in the global scope, you can access App.$data, App.$store and so on.

It can be much cleaner to use EventBus

how to do that, please

from pwa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.