sw-toolbox
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -174,2 +174,29 @@ /* | ||
function isResponseFresh(response, maxAgeSeconds, now) { | ||
// If we don't have a response, then it's not fresh. | ||
if (!response) { | ||
return false; | ||
} | ||
// Only bother checking the age of the response if maxAgeSeconds is set. | ||
if (maxAgeSeconds) { | ||
var dateHeader = response.headers.get('date'); | ||
// If there's no Date: header, then fall through and return true. | ||
if (dateHeader) { | ||
var parsedDate = new Date(dateHeader); | ||
// If the Date: header was invalid for some reason, parsedDate.getTime() | ||
// will return NaN, and the comparison will always be false. That means | ||
// that an invalid date will be treated as if the response is fresh. | ||
if ((parsedDate.getTime() + (maxAgeSeconds * 1000)) < now) { | ||
// Only return false if all the other conditions are met. | ||
return false; | ||
} | ||
} | ||
} | ||
// Fall back on returning true by default, to match the previous behavior in | ||
// which we never bothered checking to see whether the response was fresh. | ||
return true; | ||
} | ||
module.exports = { | ||
@@ -183,3 +210,4 @@ debug: debug, | ||
precache: precache, | ||
validatePrecacheInput: validatePrecacheInput | ||
validatePrecacheInput: validatePrecacheInput, | ||
isResponseFresh: isResponseFresh | ||
}; |
@@ -17,9 +17,13 @@ /* | ||
'use strict'; | ||
var globalOptions = require('../options'); | ||
var helpers = require('../helpers'); | ||
function cacheFirst(request, values, options) { | ||
options = options || {}; | ||
helpers.debug('Strategy: cache first [' + request.url + ']', options); | ||
return helpers.openCache(options).then(function(cache) { | ||
return cache.match(request).then(function(response) { | ||
if (response) { | ||
var cacheOptions = options.cache || globalOptions.cache; | ||
var now = Date.now(); | ||
if (helpers.isResponseFresh(response, cacheOptions.maxAgeSeconds, now)) { | ||
return response; | ||
@@ -26,0 +30,0 @@ } |
@@ -17,8 +17,18 @@ /* | ||
'use strict'; | ||
var globalOptions = require('../options'); | ||
var helpers = require('../helpers'); | ||
function cacheOnly(request, values, options) { | ||
options = options || {}; | ||
helpers.debug('Strategy: cache only [' + request.url + ']', options); | ||
return helpers.openCache(options).then(function(cache) { | ||
return cache.match(request); | ||
return cache.match(request).then(function(response) { | ||
var cacheOptions = options.cache || globalOptions.cache; | ||
var now = Date.now(); | ||
if (helpers.isResponseFresh(response, cacheOptions.maxAgeSeconds, now)) { | ||
return response; | ||
} | ||
return undefined; | ||
}); | ||
}); | ||
@@ -25,0 +35,0 @@ } |
@@ -39,7 +39,10 @@ /* | ||
cache.match(request).then(function(response) { | ||
if (response) { | ||
// Only resolve this promise if there's a valid response in the | ||
// cache. This ensures that we won't time out a network request | ||
// unless there's a cached entry to fallback on, which is arguably | ||
// the preferable behavior. | ||
var cacheOptions = options.cache || globalOptions.cache; | ||
// Only resolve this promise if there's a valid response in the | ||
// cache. This ensures that we won't time out a network request | ||
// unless there's a cached entry to fallback on, which is arguably | ||
// the preferable behavior. | ||
var now = Date.now(); | ||
var maxAgeSeconds = cacheOptions.maxAgeSeconds; | ||
if (helpers.isResponseFresh(response, maxAgeSeconds, now)) { | ||
resolve(response); | ||
@@ -46,0 +49,0 @@ } |
{ | ||
"name": "sw-toolbox", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "Service Worker Toolbox provides some simple helpers for use in creating your own service workers.", | ||
@@ -23,23 +23,20 @@ "license": "Apache-2.0", | ||
"chai": "^3.4.1", | ||
"chromedriver": "^2.24.1", | ||
"chromedriver": "^2.27.2", | ||
"cookie-parser": "^1.4.1", | ||
"eslint": "^1.10.3", | ||
"eslint-config-google": "^0.3.0", | ||
"eslint": "^3.13.1", | ||
"eslint-config-google": "^0.7.1", | ||
"express": "^4.13.3", | ||
"geckodriver": "^1.1.2", | ||
"geckodriver": "^1.3.0", | ||
"gulp": "^3.9.0", | ||
"gulp-eslint": "^1.1.1", | ||
"gulp-eslint": "^3.0.1", | ||
"gulp-gh-pages": "^0.5.4", | ||
"gulp-header": "^1.8.8", | ||
"gulp-sourcemaps": "^1.6.0", | ||
"gulp-sourcemaps": "^2.3.1", | ||
"gulp-uglify": "^2.0.0", | ||
"jsdoc": "^3.4.0", | ||
"jshint-stylish": "^2.1.0", | ||
"mocha": "^2.3.4", | ||
"mocha": "^3.2.0", | ||
"npm-publish-scripts": "^2.0.7", | ||
"operadriver": "^0.2.2", | ||
"qunitjs": "^1.20.0", | ||
"selenium-assistant": "^1.0.0", | ||
"selenium-webdriver": "^3.0.0-beta-2", | ||
"sw-testing-helpers": "0.1.4", | ||
"operadriver": "^1.0.0", | ||
"selenium-assistant": "^5.0.2", | ||
"sw-testing-helpers": "1.0.1", | ||
"temp": "^0.8.3", | ||
@@ -46,0 +43,0 @@ "vinyl-buffer": "^1.0.0", |
# Service Worker Toolbox | ||
[![Build Status](https://travis-ci.org/GoogleChrome/sw-toolbox.svg?branch=master)](https://travis-ci.org/GoogleChrome/sw-toolbox) [![Dependency Status](https://david-dm.org/googlechrome/sw-toolbox.svg)](https://david-dm.org/googlechrome/sw-toolbox) [![devDependency Status](https://david-dm.org/googlechrome/sw-toolbox/dev-status.svg)](https://david-dm.org/googlechrome/sw-toolbox#info=devDependencies) | ||
[![Build Status](https://travis-ci.org/GoogleChrome/sw-toolbox.svg?branch=master)](https://travis-ci.org/GoogleChrome/sw-toolbox) [![Dependency Status](https://david-dm.org/googlechrome/sw-toolbox.svg)](https://david-dm.org/googlechrome/sw-toolbox) [![devDependencies Status](https://david-dm.org/googlechrome/sw-toolbox/dev-status.svg)](https://david-dm.org/googlechrome/sw-toolbox?type=dev) | ||
> A collection of tools for [service workers](https://w3c.github.io/ServiceWorker/) | ||
Service Worker Toolbox provides some simple helpers for use in creating your own service workers. Specifically, it provides common caching patterns and an [expressive approach](https://googlechrome.github.io/sw-toolbox/docs/master/tutorial-api.html#expressive-approach) to using those strategies for runtime requests. If you're not sure what service workers are or what they are for, start with [the explainer doc](https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md). | ||
Service Worker Toolbox provides some simple helpers for use in creating your own service workers. Specifically, it provides common caching strategies for dynamic content, such as API calls, third-party resources, and large or infrequently used local resources that you don't want precached. | ||
Service Worker Toolbox provides an [expressive approach](https://googlechrome.github.io/sw-toolbox/docs/master/tutorial-api.html#expressive-approach) to using those strategies for runtime requests. If you're not sure what service workers are or what they are for, start with [the explainer doc](https://github.com/slightlyoff/ServiceWorker/blob/master/explainer.md). | ||
## What if I need precaching as well? | ||
Then you should go check out [`sw-precache`](https://github.com/GoogleChrome/sw-precache) before doing anything else. In addition to precaching static resources, `sw-precache` supports optional [runtime caching](https://github.com/GoogleChrome/sw-precache#runtime-caching) through a simple, declarative configuration that incorporates Service Worker Toolbox under the hood. | ||
## Install | ||
@@ -10,0 +16,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
24
1
74
1
2
49011
17
847