-
Notifications
You must be signed in to change notification settings - Fork 832
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
Changes from 6 commits
7ad1b69
a245226
0f490e1
6b7ea24
9e11ac2
fe2613b
1a75be4
efbf3e8
73bd04f
f1396cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
layout: index | ||
title: Home | ||
navigation_weight: 0 | ||
--- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,54 @@ if (!assert.isSWEnv()) { | |
throw ErrorFactory.createError('not-in-sw'); | ||
} | ||
|
||
/** | ||
* The sw-lib module is a high-level library that makes it easier to | ||
* configure routes with caching strategies as well as manage precaching | ||
* of assets during the install step of a service worker. | ||
* | ||
* @example | ||
* importScripts('/<Path to Module>/build/sw-lib.min.js'); | ||
* | ||
* // cacheRevisionedAssets() can take an array of strings with | ||
* // the revision details in the url. | ||
* goog.swlib.cacheRevisionedAssets([ | ||
* '/styles/main.1234.css', | ||
* '/images/logo.abcd.jpg' | ||
* ]); | ||
* | ||
* // Or it can accept objects with the URL and revision data | ||
* // kept seperate. | ||
* goog.swlib.cacheRevisionedAssets([ | ||
* { | ||
* url: '/index.html', | ||
* revision: '1234' | ||
* }, | ||
* { | ||
* url: '/about.html', | ||
* revision: 'abcd' | ||
* } | ||
* ]); | ||
* | ||
* // 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 commentThe reason will be displayed to describe this comment to others. Learn more. Suggested rephrasing:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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". |
||
* // during the installation of you service worker using warmRuntimeCache() | ||
* goog.swlib.warmRuntimeCache([ | ||
* '/scripts/main.js', | ||
* '/images/default-avater.png' | ||
* ]); | ||
* | ||
* // warmRuntimeCache can also accept Requests, in case you need greater | ||
* // control over the request. | ||
* goog.swlib.warmRuntimeCache([ | ||
* new Request('/images/logo.png'), | ||
* new Request('/api/data.json') | ||
* ]); | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
* goog.swlib.router.registerRoute('/', goog.swlib.cacheFirst); | ||
* goog.swlib.router.registerRoute('/example/', goog.swlib.networkFirst); | ||
* | ||
* @module sw-lib | ||
*/ | ||
|
||
const swLibInstance = new SWLib(); | ||
swLibInstance.Route = Route; | ||
export default swLibInstance; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
importScripts('/node_modules/mocha/mocha.js'); | ||
importScripts('/node_modules/chai/chai.js'); | ||
importScripts('/node_modules/sw-testing-helpers/build/browser/mocha-utils.js'); | ||
|
||
importScripts('/packages/sw-lib/build/sw-lib.min.js'); | ||
|
||
/* global goog */ | ||
|
||
const expect = self.chai.expect; | ||
self.chai.should(); | ||
mocha.setup({ | ||
ui: 'bdd', | ||
reporter: null, | ||
}); | ||
|
||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
expect(goog.swlib.cacheOnly).to.exist; | ||
}); | ||
}); | ||
|
||
describe('Test swlib.cacheFirst', function() { | ||
it('should be accessible goog.swlib.cacheFirst', function() { | ||
expect(goog.swlib.cacheFirst).to.exist; | ||
}); | ||
}); | ||
|
||
describe('Test swlib.networkOnly', function() { | ||
it('should be accessible goog.swlib.networkOnly', function() { | ||
expect(goog.swlib.networkOnly).to.exist; | ||
}); | ||
}); | ||
|
||
describe('Test swlib.networkFirst', function() { | ||
it('should be accessible goog.swlib.networkFirst', function() { | ||
expect(goog.swlib.networkFirst).to.exist; | ||
}); | ||
}); | ||
|
||
describe('Test swlib.fastest', function() { | ||
it('should be accessible goog.swlib.fastest', function() { | ||
expect(goog.swlib.fastest).to.exist; | ||
}); | ||
}); |
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.