Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Conversation

@AsaAyers
Copy link

@AsaAyers AsaAyers commented Jan 4, 2016

This is a continuation of #14, but I'm opening a 2nd PR because:

  1. Some of this is hacked together and I'm looking for some guidance.
  2. I think this belongs in this project and I hope you agree.

I'm trying to use a service worker that needs to cache my application. The first thing it has to cache is all the built assets, the rest is outside the scope of this project. I'm using Introduction to Service Worker as a guide.

This PR introduces a magic __assets__ variable you can use inside a worker.

var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = __assets__

self.addEventListener('install', function(event) {
  // Perform install steps
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
      })
  );
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely has to be changed, but I don't know how to do this right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sokra Do you know how this can be fixed? I'd really like to get this PR production ready and merged, but I'm lost.

It functions, it just uses two hacks that I think should be fixed before merging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BowlingX
Copy link

Just an Idea: I think it would be possible to use the already existing assets-webpack-plugin (https://www.npmjs.com/package/assets-webpack-plugin) and consume the generated file inside the worker with importScripts.

@AsaAyers
Copy link
Author

Using a plugin like that you end up with worker.js and assets.js. worker.js is the one that is registered with the browser. The browser will only update the worker if something in worker.js changes, so when it uses importScripts('assets.js'), your worker never changes as assets change.

The solution to use some form of assets.[hash].js, which will change, but then there's no way for you to predict that hash ahead of time to reference it.

Thanks for the suggestion, but I already tried it.

@AsaAyers
Copy link
Author

@sokra Any updates on this? I'd rather not have to keep depending on my fork.

@ianks
Copy link

ianks commented Oct 22, 2016

@sokra I would love to see this merged

@AsaAyers
Copy link
Author

I'm making https://github.com/pulls more useful to me today by closing my old PRs.

@AsaAyers AsaAyers closed this Aug 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants