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

Sw lib caching strategies #108

Merged
merged 10 commits into from
Jan 10, 2017
Merged

Sw lib caching strategies #108

merged 10 commits into from
Jan 10, 2017

Conversation

gauntface
Copy link

R: @jeffposnick @addyosmani

This adds caching strategies to sw-lib as well as adding some docs.

Copy link
Member

@addyosmani addyosmani left a comment

Choose a reason for hiding this comment

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

Left some minor comments.

* importScripts('/<Path to Module>/build/sw-lib.min.js');
*
* goog.swlib.cacheRevisionedAssets([
* '/styles/main.1234.css',
Copy link
Member

Choose a reason for hiding this comment

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

I think what you're trying to show here is that cacheRevisionedAssets accepts both filename paths with revisions in there or an object with the raw path and a separate revision number. Is that the case?

It might be worth splitting these into two examples (one showing caching a few files with one approach (e.g `/styles/main.1234.css', '/js/main.1234.js' etc) and then the object variation. Might make it more easy to read through.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

* '/scripts/main.js',
* new Request('/images/logo.png')
* ]);
*
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

});

describe('Test swlib.cacheOnly', function() {
it('should be accessible goog.swlib.cacheOnly', function() {
Copy link
Member

Choose a reason for hiding this comment

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

We talked about why these functions aren't just using arrow syntax offline. I think your reason about Mocha breaking decorated it bindings etc if arrows are used was fine. I did see https://github.com/skozin/arrow-mocha in case we wanted to explore how to work around this in future.

Copy link
Author

Choose a reason for hiding this comment

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

I'm probably going to stick with the function name for now. Ideally Mocha would change to have a more explicit approach of declaring the timeout and retries of a unit test.

@gauntface
Copy link
Author

@addyosmani happy for this to land?

@jeffposnick
Copy link
Contributor

I'm taking a look right now, too.

* ]);
*
* // If you have assets that aren't revisioned, you can cache them
* // during the installation of you service work using warmRuntimeCache()
Copy link
Contributor

Choose a reason for hiding this comment

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

"service work" ➡️ "service worker"

Copy link
Author

Choose a reason for hiding this comment

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

Fixed.

* }
* ]);
*
* // If you have assets that aren't revisioned, you can cache them
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested rephrasing:

If you have assets that aren't revisioned, but which you plan to reference using a runtime caching strategy, you can add them to your runtime cache

Copy link
Author

Choose a reason for hiding this comment

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

At the moment the precaching module doesn't configure fetch routes, so technically both require "run time caching strategies".

*/
constructor() {
this._router = new RouterWrapper();
this._precacheManager = new PrecacheManager();
this._strategies = {
Copy link
Contributor

Choose a reason for hiding this comment

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

When we talked about this last week, I misunderstood and thought that your plan was to export the class definitions—basically, aliasing them under the sw-lib module namespace. I didn't realize that your suggestion was to create instances of each class and include those in each SWLib instance.

These strategies are most useful when you have some control over their behavior via the RequestWrapper class, but there's no way to configure these instances. The default behavior will work, but in the back of my head I worry that people won't realize that they can do things like change the cache name or configure cache expiration if they're only familiar with using these unconfigured instances.

Do you all share those concerns?

Copy link
Author

Choose a reason for hiding this comment

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

Yes and no.

This API matches that of sw-toolbox, that's all I was aiming for with this as it does cover a simple use case.

There are three 1 options:

  1. This approach and expect developers to use the smaller modules is desired (not ideal).
  2. This approach + expose the classes as well, i.e. allow new goog.swlib.strategies.<Name of Strategy>(). Simple use case plus allows configuration.
  3. Just include the class strategies. Only con is that is requires some boilerplate for basic use.

Copy link
Contributor

Choose a reason for hiding this comment

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

The sw-toolbox-y way of doing this was to pass in the configuration as the third parameter.

I guess we could take inspiration from that and allow developers to pass in a RequestWrapper as an optional third parameter.

For that approach to work, the second parameter should be the an uninstantiated reference to a sw-runtime-caching class, and then SWLib would construct the class at runtime, passing along the RequestWrapper to the constructor if it's provided.

Copy link
Author

Choose a reason for hiding this comment

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

In toolbox I would always go:

toolbox.router.all('/', toolbox.fastest);

The options generally would be global rather than per route.

Would it be worth me removing the strategies from this PR, landing the doc changes and then discussing the strategies in an issue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, yanking them out of this PR and discussing the approach off-thread would make sense.

There are lots of common use cases for per-route configs that we'd need to support, like one route that used a dedicated image cache with a specific cache expiration policy, and a different route that used a dedicated api-response cache with a different policy.

* aren't revisioned, you can cache them here.
* @param {Array<String>} unrevisionedFiles A set of urls to cache when the
* service worker is installed.
* Any assets you wish to cache which can't be revisioned should be
Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

Any assets you wish to cache ahead of time which can't be revisioned should be...

Since in many of the cases for unversioned resources, just populating the caches via a runtime caching strategy is desirable.

Copy link
Author

Choose a reason for hiding this comment

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

done.

@gauntface
Copy link
Author

@jeffposnick the strategies are now removed - any further changes you'd like to comments?

@jeffposnick
Copy link
Contributor

There's a few extra bits to remove to make the linting happy, but once Travis is green, feel free to merge.

/home/travis/build/GoogleChrome/sw-helpers/packages/sw-lib/src/lib/sw-lib.js
  22:3   error  'CacheFirst' is defined but never used            no-unused-vars
  22:15  error  'CacheOnly' is defined but never used             no-unused-vars
  22:26  error  'NetworkFirst' is defined but never used          no-unused-vars
  22:40  error  'NetworkOnly' is defined but never used           no-unused-vars
  22:53  error  'StaleWhileRevalidate' is defined but never used  no-unused-vars

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

Successfully merging this pull request may close these issues.

3 participants