broccoli-serviceworker
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -41,3 +41,4 @@ var path = require('path'); | ||
excludePaths: ['test.*','robots.txt'], | ||
precacheURLs: [] | ||
precacheURLs: [], | ||
rootURL: appOptions.rootURL || appOptions.baseURL | ||
}; | ||
@@ -44,0 +45,0 @@ |
function getFallbackFromCache(request, values, options) { | ||
logDebug('Fetching from fallback url: '+ options.fallbackURL +'for url: '+request.url); | ||
var req = new Request(options.fallbackURL, request); | ||
return toolbox.cacheFirst(req, values, options).then(function(response) { | ||
if (response) { | ||
logDebug('Got fallback response from cache',response); | ||
return response; | ||
} | ||
}); | ||
return request | ||
.text() | ||
.then(function(text) { | ||
// cannot set mode to navigate in Request constructor init object | ||
// https://fetch.spec.whatwg.org/#dom-request | ||
var mode = request.mode === 'navigate' ? 'same-origin' : request.mode; | ||
var body = text === '' ? undefined : text; | ||
return new Request(options.fallbackURL, { | ||
method: request.method, | ||
headers: request.headers, | ||
body: body, | ||
mode: mode, | ||
credentials: request.credentials, | ||
redirect: request.redirect, | ||
cache: request.cache, | ||
}); | ||
}) | ||
.then(function(req) { | ||
return toolbox.cacheFirst(req, values, options); | ||
}) | ||
.then(function(response) { | ||
if (response) { | ||
logDebug('Got fallback response from cache',response); | ||
return response; | ||
} | ||
}); | ||
} | ||
@@ -11,0 +30,0 @@ |
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.register('./service-worker.js', {scope: './'}) | ||
navigator.serviceWorker.register('/service-worker.js', {scope: '/'}) | ||
.catch(function(error) { | ||
@@ -4,0 +4,0 @@ console.error('Error registering service worker:'+error); |
@@ -28,2 +28,3 @@ var fs = require("fs"); | ||
this.fallback = options.fallback || []; | ||
this.rootURL = (options.rootURL === false ? false : options.rootURL || '/'); | ||
this.precacheURLs = options.precacheURLs || []; | ||
@@ -54,2 +55,3 @@ if (options.includeRegistration === false) { | ||
var fallback = this.fallback; | ||
var rootURL = this.rootURL; | ||
var fastestURLs = this.fastestURLs; | ||
@@ -75,3 +77,5 @@ var precacheURLs = this.precacheURLs; | ||
lines.push("var urlsToPrefetch = ["); | ||
lines.push(" '/',"); | ||
if (rootURL) { | ||
lines.push(" '"+rootURL+"',"); | ||
} | ||
getFilesRecursively(srcDir, [ "**/*" ]).forEach(function (file, idx, array) { | ||
@@ -78,0 +82,0 @@ lines.push(createArrayLine(' '+JSON.stringify(file), idx, array.length)); |
{ | ||
"name": "broccoli-serviceworker", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A broccoli plugin automating ServiceWorker file creation for Broccoli and Ember.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/service-worker.js", |
@@ -24,3 +24,3 @@ broccoli-serviceworker | ||
debug: true, | ||
precacheURLs: ['/mystaticresouce'], | ||
precacheURLs: ['/mystaticresource'], | ||
excludePaths: ['test.*', 'robots.txt',], | ||
@@ -54,8 +54,12 @@ fallback: [ | ||
* **fallback** - Array of URLs with fallbacks when the resource isn't available via network or cache. | ||
* **Routing Options** - The following options allow you to specify routes that use [sw-toolbox's built-in handlers](https://github.com/GoogleChrome/sw-toolbox#built-in-handlers). Each of these options accepts an array of URLs that can be strings or [regular expressions](https://github.com/GoogleChrome/sw-toolbox#regular-expression-routes). | ||
* **cacheFirstURLs** -- List of URLS that should pull from cache first and then fallback to network if it isn't in cache. For more details, see the details on [sw-toolbox's cacheFirst strategy](https://github.com/GoogleChrome/sw-toolbox#toolboxcachefirst). | ||
* **cacheOnlyURLs** - List of URLs that should resolve the request from the cache, or fail. For more details, see the details on [sw-toolbox's cacheOnly strategy](https://github.com/GoogleChrome/sw-toolbox#toolboxcacheonly). | ||
* **fastestURLs** -- List of URLS that should pull from network and cache and deliver the fastest response. For more details, see the details on [sw-toolbox's fastest strategy](https://github.com/GoogleChrome/sw-toolbox#toolboxfastest). | ||
* **networkFirstURLs** - List of URLs that should use a network first strategy that falls back to a cached version of the response if the network is unavailable. For more details, see the details on [sw-toolbox's networkFirst strategy](https://github.com/GoogleChrome/sw-toolbox#user-content-toolboxnetworkfirst). | ||
* If additional configuration is desired for the above routing options, instead of using URLs, you can pass a configuration object: | ||
* **skipWaiting** - Allows a simple page refresh to update the app. Defaults to true. | ||
* **swIncludeFiles** - Array of files to include in the generated service worker. This is intended to allow inclusion of vendor files in your service worker. For example, if you wanted to run [PouchDB](http://pouchdb.com/) replication in a service worker, you need to include PouchDB in your service worker. | ||
####Routing Options | ||
The following options allow you to specify routes that use [sw-toolbox's built-in handlers](https://github.com/GoogleChrome/sw-toolbox#built-in-handlers). Each of these options accepts an array of URLs that can be strings or [regular expressions](https://github.com/GoogleChrome/sw-toolbox#regular-expression-routes). | ||
* **cacheFirstURLs** -- List of URLS that should pull from cache first and then fallback to network if it isn't in cache. For more details, see the details on [sw-toolbox's cacheFirst strategy](https://github.com/GoogleChrome/sw-toolbox#toolboxcachefirst). | ||
* **cacheOnlyURLs** - List of URLs that should resolve the request from the cache, or fail. For more details, see the details on [sw-toolbox's cacheOnly strategy](https://github.com/GoogleChrome/sw-toolbox#toolboxcacheonly). | ||
* **fastestURLs** -- List of URLS that should pull from network and cache and deliver the fastest response. For more details, see the details on [sw-toolbox's fastest strategy](https://github.com/GoogleChrome/sw-toolbox#toolboxfastest). | ||
* **networkFirstURLs** - List of URLs that should use a network first strategy that falls back to a cached version of the response if the network is unavailable. For more details, see the details on [sw-toolbox's networkFirst strategy](https://github.com/GoogleChrome/sw-toolbox#user-content-toolboxnetworkfirst). | ||
* If additional configuration is desired for the above routing options, instead of using URLs, you can pass a configuration object: | ||
Optionally they can be an array of objects in the format: | ||
@@ -71,9 +75,6 @@ ```javascript | ||
``` | ||
* **route** - the url or regular expression for the route. | ||
* **method** - the HTTP method for the route. Defaults to **any** which matches all HTTP methods. | ||
* **options** - passed to the [route handler](https://github.com/GoogleChrome/sw-toolbox#methods) and are available for example to specify a different origin domain. | ||
* **skipWaiting** - Allows a simple page refresh to update the app. Defaults to true. | ||
* **swIncludeFiles** - Array of files to include in the generated service worker. This is intended to allow inclusion of vendor files in your service worker. For example, if you wanted to run [PouchDB](http://pouchdb.com/) replication in a service worker, you need to include PouchDB in your service worker. | ||
* **route** - the url or regular expression for the route. | ||
* **method** - the HTTP method for the route. Defaults to **any** which matches all HTTP methods. | ||
* **options** - passed to the [route handler](https://github.com/GoogleChrome/sw-toolbox#methods) and are available for example to specify a different origin domain. | ||
Usage for Broccoli.js | ||
@@ -80,0 +81,0 @@ --------------------- |
27338
12
355
139