-
Notifications
You must be signed in to change notification settings - Fork 831
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
Conversation
There was a problem hiding this 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', |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') | ||
* ]); | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@addyosmani happy for this to land? |
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"service work" ➡️ "service worker"
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- This approach and expect developers to use the smaller modules is desired (not ideal).
- This approach + expose the classes as well, i.e. allow
new goog.swlib.strategies.<Name of Strategy>()
. Simple use case plus allows configuration. - Just include the class strategies. Only con is that is requires some boilerplate for basic use.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
@jeffposnick the strategies are now removed - any further changes you'd like to comments? |
There's a few extra bits to remove to make the linting happy, but once Travis is green, feel free to merge.
|
R: @jeffposnick @addyosmani
This adds caching strategies to sw-lib as well as adding some docs.