Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

WorkBox does not perform update method #1186

Closed
leonardovilarinho opened this issue Apr 27, 2018 · 2 comments
Closed

WorkBox does not perform update method #1186

leonardovilarinho opened this issue Apr 27, 2018 · 2 comments

Comments

@leonardovilarinho
Copy link

Version

3.0.0-beta.6

Steps to reproduce

Phases:

  1. Run vue create app and select PWA.

  2. Run yarn build and serve ./dist to test the site in production.

  3. After accessing the site and waiting for the cache to be done, disable the internet and check for offline access.

  4. Edit the App.vue by putting some more content.

  5. Run yarn build and serve ./dist to test the production site again.

What is expected?

The workbox message is expected: New content is available; please refresh.

What is actually happening?

The message is not displayed, so the workbox is not detecting that the content of the application has been updated.

I've already checked that the service worker is in the cache and is not.

@leonardovilarinho
Copy link
Author

I have seen that although the updated register-service-worker method is not called, the service-worker collects the update and waits to install. As in the image below:

sw

So maybe this can help.

@leonardovilarinho
Copy link
Author

I do not know if it was the right solution, but I managed to circumvent it. I put the workbox to skip the wait, in vue.config.js:

module.exports = {
  lintOnSave: false,
  pwa: {
    workboxPluginMode: 'GenerateSW',
    workboxOptions: {
      skipWaiting: true
    }
  }
}

And in registerServiceWorker.js I changed all content, registering my SW and whenever it is registered I check the temporary cache of the workbox, if it has any content is because the workbox has captured an update and is ready to replace the cache:

/* eslint-disable no-console */

const updated = () => {
  console.log('Has new content!')
  // use window.location.reload(true) to get new cache
}

const register = async (path) => {
  const registration = await navigator.serviceWorker.register(path)
  console.log('SW registred!')

  const cacheList = await caches.keys()
  cacheList.forEach(async (cacheName) => {
    if (!cacheName.includes('-temp')) return

    const tempCache = await caches.open(cacheName)
    const tempCachesKeys = await tempCache.keys()
    if (tempCachesKeys.length > 0) {
      updated()
    }
  })

  registration.onupdatefound = () => updated()
}

const { NODE_ENV, BASE_URL } = process.env

if (NODE_ENV === 'production') {
  if ('serviceWorker' in navigator) {
    register(`${BASE_URL}service-worker.js`)
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants