Code Monkey home page Code Monkey logo

googlechromelabs / sw-precache Goto Github PK

View Code? Open in Web Editor NEW
5.2K 103.0 392.0 630 KB

[Deprecated] A node module to generate service worker code that will precache specific resources so they work offline.

Home Page: https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw

License: Apache License 2.0

JavaScript 89.26% CSS 4.12% HTML 2.33% Dockerfile 4.29%
javascript service-workers progressive-web-app offline offline-first service-worker

sw-precache's Introduction

⚠️ sw-precache ⚠️

sw-toolbox and sw-precache are deprecated in favor of Workbox. Please read this migration guide for information on upgrading.

About

Service Worker Precache is a module for generating a service worker that precaches resources. It integrates with your build process. Once configured, it detects all your static resources (HTML, JavaScript, CSS, images, etc.) and generates a hash of each file's contents. Information about each file's URL and versioned hash are stored in the generated service worker file, along with logic to serve those files cache-first, and automatically keep those files up to date when changes are detected in subsequent builds.

Serving your local static resources cache-first means that you can get all the crucial scaffolding for your web app—your App Shell—on the screen without having to wait for any network responses.

The module can be used in JavaScript-based build scripts, like those written with gulp, and it also provides a command-line interface. You can use the module directly, or if you'd prefer, use one of the wrappers around sw-precache for specific build environments, like webpack.

It can be used alongside the sw-toolbox library, which works well when following the App Shell + dynamic content model.

The full documentation is in this README, and the getting started guide provides a quicker jumping off point.

To learn more about the internals of the generated service worker, you can read this deep-dive by Huang Xuan.

Table of Contents

Install

Local build integration:

$ npm install --save-dev sw-precache

Global command-line interface:

$ npm install --global sw-precache

Usage

Overview

  1. Make sure your site is served using HTTPS! Service worker functionality is only available on pages that are accessed via HTTPS. (http://localhost will also work, to facilitate testing.) The rationale for this restriction is outlined in the "Prefer Secure Origins For Powerful New Features" document.

  2. Incorporate sw-precache into your node-based build script. It should work well with either gulp or Grunt, or other build scripts that run on node. In fact, we've provided examples of both in the demo/ directory. Each build script in demo has a function called writeServiceWorkerFile() that shows how to use the API. Both scripts generate fully-functional JavaScript code that takes care of precaching and fetching all the resources your site needs to function offline. There is also a command-line interface available, for those using alternate build setups.

  3. Register the service worker JavaScript. The JavaScript that's generated needs to be registered as the controlling service worker for your pages. This technically only needs to be done from within a top-level "entry" page for your site, since the registration includes a scope which will apply to all pages underneath your top-level page. service-worker-registration.js is a sample script that illustrates the best practices for registering the generated service worker and handling the various lifecycle events.

Example

The project's sample gulpfile.js illustrates the full use of sw-precache in context. (Note that the sample gulpfile.js is the one in the demo folder, not the one in the root of the project.) You can run the sample by cloning this repo, using npm install to pull in the dependencies, changing to the demo/ directory, running `npm bin`/gulp serve-dist, and then visiting http://localhost:3000.

There's also a sample Gruntfile.js that shows service worker generation in Grunt. Though, it doesn't run a server on localhost.

Here's a simpler gulp example for a basic use case. It assumes your site's resources are located under app and that you'd like to cache all your JavaScript, HTML, CSS, and image files.

gulp.task('generate-service-worker', function(callback) {
  var swPrecache = require('sw-precache');
  var rootDir = 'app';

  swPrecache.write(`${rootDir}/service-worker.js`, {
    staticFileGlobs: [rootDir + '/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}'],
    stripPrefix: rootDir
  }, callback);
});

This task will create app/service-worker.js, which your client pages need to register before it can take control of your site's pages. service-worker-registration.js is a ready-to- use script to handle registration.

Considerations

  • Service worker caching should be considered a progressive enhancement. If you follow the model of conditionally registering a service worker only if it's supported (determined by if('serviceWorker' in navigator)), you'll get offline support on browsers with service workers and on browsers that don't support service workers, the offline-specific code will never be called. There's no overhead/breakage for older browsers if you add sw-precache to your build.

  • All resources that are precached will be fetched by a service worker running in a separate thread as soon as the service worker is installed. You should be judicious in what you list in the dynamicUrlToDependencies and staticFileGlobs options, since listing files that are non-essential (large images that are not shown on every page, for instance) will result in browsers downloading more data than is strictly necessary.

  • Precaching doesn't make sense for all types of resources (see the previous point). Other caching strategies, like those outlined in the Offline Cookbook, can be used in conjunction with sw-precache to provide the best experience for your users. If you do implement additional caching logic, put the code in a separate JavaScript file and include it using the importScripts() method.

  • sw-precache uses a cache-first strategy, which results in a copy of any cached content being returned without consulting the network. A useful pattern to adopt with this strategy is to display a toast/alert to your users when there's new content available, and give them an opportunity to reload the page to pick up that new content (which the service worker will have added to the cache, and will be available at the next page load). The sample service-worker-registration.js file illustrates the service worker lifecycle event you can listen for to trigger this message.

Command-line interface

For those who would prefer not to use sw-precache as part of a gulp or Grunt build, there's a command-line interface which supports the options listed in the API, provided via flags or an external JavaScript configuration file.

Hypenated flags are converted to camelCase options.
Options starting with --no prefix negate the boolean value. For example, --no-clients-claim sets the value of clientsClaim to false.

Warning: When using sw-precache "by hand", outside of an automated build process, it's your responsibility to re-run the command each time there's a change to any local resources! If sw-precache is not run again, the previously cached local resources will be reused indefinitely.

Sensible defaults are assumed for options that are not provided. For example, if you are inside the top-level directory that contains your site's contents, and you'd like to generate a service-worker.js file that will automatically precache all of the local files, you can simply run

$ sw-precache

Alternatively, if you'd like to only precache .html files that live within dist/, which is a subdirectory of the current directory, you could run

$ sw-precache --root=dist --static-file-globs='dist/**/*.html'

Note: Be sure to use quotes around parameter values that have special meanings to your shell (such as the * characters in the sample command line above, for example).

Finally, there's support for passing complex configurations using --config <file>. Any of the options from the file can be overridden via a command-line flag. We strongly recommend passing it an external JavaScript file defining config via module.exports. For example, assume there's a path/to/sw-precache-config.js file that contains:

module.exports = {
  staticFileGlobs: [
    'app/css/**.css',
    'app/**.html',
    'app/images/**.*',
    'app/js/**.js'
  ],
  stripPrefix: 'app/',
  runtimeCaching: [{
    urlPattern: /this\\.is\\.a\\.regex/,
    handler: 'networkFirst'
  }]
};

That file could be passed to the command-line interface, while also setting the verbose option, via

$ sw-precache --config=path/to/sw-precache-config.js --verbose

This provides the most flexibility, such as providing a regular expression for the runtimeCaching.urlPattern option.

We also support passing in a JSON file for --config, though this provides less flexibility:

{
  "staticFileGlobs": [
    "app/css/**.css",
    "app/**.html",
    "app/images/**.*",
    "app/js/**.js"
  ],
  "stripPrefix": "app/",
  "runtimeCaching": [{
    "urlPattern": "/express/style/path/(.*)",
    "handler": "networkFirst"
  }]
}

Runtime Caching

It's often desireable, even necessary to use precaching and runtime caching together. You may have seen our sw-toolbox tool, which handles runtime caching, and wondered how to use them together. Fortunately, sw-precache handles this for you.

The sw-precache module has the ability to include the sw-toolbox code and configuration alongside its own configuration. Using the runtimeCaching configuration option in sw-precache (see below) is a shortcut that accomplishes what you could do manually by importing sw-toolbox in your service worker and writing your own routing rules.

API

Methods

The sw-precache module exposes two methods: generate and write.

generate(options, callback)

generate takes in options, generates a service worker from them and passes the result to a callback function, which must have the following interface:

callback(error, serviceWorkerString)

In the 1.x releases of sw-precache, this was the default and only method exposed by the module.

Since 2.2.0, generate() also returns a Promise.

write(filePath, options, callback)

write takes in options, generates a service worker from them, and writes the service worker to a specified file. This method always invokes callback(error). If no error was found, the error parameter will be null

Since 2.2.0, write() also returns a Promise.

Options Parameter

Both the generate() and write() methods take the same options.

cacheId [String]

A string used to distinguish the caches created by different web applications that are served off of the same origin and path. While serving completely different sites from the same URL is not likely to be an issue in a production environment, it avoids cache-conflicts when testing various projects all served off of http://localhost. You may want to set it to, e.g., the name property from your package.json.

Default: ''

clientsClaim [Boolean]

Controls whether or not the generated service worker will call clients.claim() inside the activate handler.

Calling clients.claim() allows a newly registered service worker to take control of a page immediately, instead of having to wait until the next page navigation.

Default: true

directoryIndex [String]

Sets a default filename to return for URL's formatted like directory paths (in other words, those ending in '/'). sw-precache will take that translation into account and serve the contents a relative directoryIndex file when there's no other match for a URL ending in '/'. To turn off this behavior, set directoryIndex to false or null. To override this behavior for one or more URLs, use the dynamicUrlToDependencies option to explicitly set up mappings between a directory URL and a corresponding file.

Default: 'index.html'

dontCacheBustUrlsMatching [Regex]

It's very important that the requests sw-precache makes to populate your cache result in the most up-to-date version of a resource at a given URL. Requests that are fulfilled with out-of-date responses (like those found in your browser's HTTP cache) can end up being read from the service worker's cache indefinitely. Jake Archibald's blog post provides more context about this problem.

In the interest of avoiding that scenario, sw-precache will, by default, append a cache-busting parameter to the end of each URL it requests when populating or updating its cache. Developers who are explicitly doing "the right thing" when it comes to setting HTTP caching headers on their responses might want to opt out of this cache-busting. For example, if all of your static resources already include versioning information in their URLs (via a tool like gulp-rev), and are served with long-lived HTTP caching headers, then the extra cache-busting URL parameter is not needed, and can be safely excluded.

dontCacheBustUrlsMatching gives you a way of opting-in to skipping the cache busting behavior for a subset of your URLs (or all of them, if a catch-all value like /./ is used). If set, then the pathname of each URL that's prefetched will be matched against this value. If there's a match, then the URL will be prefetched as-is, without an additional cache-busting URL parameter appended.

Note: Prior to sw-precache v5.0.0, dontCacheBustUrlsMatching matched against the entire request URL. As of v5.0.0, it only matches against the URL's pathname.

Default: not set

dynamicUrlToDependencies [Object⟨String,Buffer,Array⟨String⟩⟩]

Maps a dynamic URL string to an array of all the files that URL's contents depend on. E.g., if the contents of /pages/home are generated server-side via the templates layout.jade and home.jade, then specify '/pages/home': ['layout.jade', 'home.jade']. The MD5 hash is used to determine whether /pages/home has changed will depend on the hashes of both layout.jade and home.jade.

An alternative value for the mapping is supported as well. You can specify a string or a Buffer instance rather than an array of file names. If you use this option, then the hash of the string/Buffer will be used to determine whether the URL used as a key has changed. For example, '/pages/dynamic': dynamicStringValue could be used if the contents of /pages/dynamic changes whenever the string stored in dynamicStringValue changes.

Default: {}

handleFetch [boolean]

Determines whether the fetch event handler is included in the generated service worker code. It is useful to set this to false in development builds, to ensure that features like live reload still work. Otherwise, the content would always be served from the service worker cache.

Default: true

ignoreUrlParametersMatching [Array⟨Regex⟩]

sw-precache finds matching cache entries by doing a comparison with the full request URL. It's common for sites to support URL query parameters that don't affect the site's content and should be effectively ignored for the purposes of cache matching. One example is the utm_-prefixed parameters used for tracking campaign performance. By default, sw-precache will ignore key=value when key matches any of the regular expressions provided in this option. To ignore all parameters, use [/./]. To take all parameters into account when matching, use [].

Default: [/^utm_/]

importScripts [Array⟨String⟩]

Writes calls to importScripts() to the resulting service worker to import the specified scripts.

Default: []

logger [function]

Specifies a callback function for logging which resources are being precached and a precache size. Use function() {} if you'd prefer that nothing is logged. Within a gulp script, it's recommended that you use gulp-util and pass in gutil.log.

Default: console.log

maximumFileSizeToCacheInBytes [Number]

Sets the maximum allowed size for a file in the precache list.

Default: 2097152 (2 megabytes)

navigateFallback [String]

Sets an HTML document to use as a fallback for URLs not found in the sw-precache cache. This fallback URL needs to be cached via staticFileGlobs or dynamicUrlToDependencies otherwise it won't work.

// via staticFileGlobs
staticFileGlobs: ['/shell.html']
navigateFallback: '/shell.html'

// via dynamicUrlToDependencies
dynamicUrlToDependencies: {
  '/shell': ['/shell.hbs']
},
navigateFallback: '/shell'

This comes in handy when used with a web application that performs client-side URL routing using the History API. It allows any arbitrary URL that the client generates to map to a fallback cached HTML entry. This fallback entry ideally should serve as an "application shell" that is able to load the appropriate resources client-side, based on the request URL.

Note: This is not intended to be used to route failed navigations to a generic "offline fallback" page. The navigateFallback page is used whether the browser is online or offline. If you want to implement an "offline fallback", then using an approach similar to this example is more appropriate.

Default: ''

navigateFallbackWhitelist [Array⟨RegExp⟩]

Works to limit the effect of navigateFallback, so that the fallback only applies to requests for URLs with paths that match at least one RegExp.

This option is useful if you want to fallback to the cached App Shell for certain specific subsections of your site, but not have that behavior apply to all of your site's URLs.

For example, if you would like to have navigateFallback only apply to navigation requests to URLs whose path begins with /guide/ (e.g. https://example.com/guide/1234), the following configuration could be used:

navigateFallback: '/shell',
navigateFallbackWhitelist: [/^\/guide\//]

If set to [] (the default), the whitelist will be effectively bypassed, and navigateFallback will apply to all navigation requests, regardless of URL.

Default: []

replacePrefix [String]

Replaces a specified string at the beginning of path URL's at runtime. Use this option when you are serving static files from a different directory at runtime than you are at build time. For example, if your local files are under dist/app/ but your static asset root is at /public/, you'd strip 'dist/app/' and replace it with '/public/'.

Default: ''

runtimeCaching [Array⟨Object⟩]

Configures runtime caching for dynamic content. If you use this option, the sw-toolbox library configured with the caching strategies you specify will automatically be included in your generated service worker file.

Each Object in the Array needs a urlPattern, which is either a RegExp or a string, following the conventions of the sw-toolbox library's routing configuration. Also required is a handler, which should be either a string corresponding to one of the built-in handlers under the toolbox. namespace, or a function corresponding to your custom request handler. Optionally, method can be added to specify one of the supported HTTP methods (default: 'get'). There is also support for options, which corresponds to the same options supported by a sw-toolbox handler.

For example, the following defines runtime caching behavior for two different URL patterns. It uses a different handler for each, and specifies a dedicated cache with maximum size for requests that match /articles/:

runtimeCaching: [{
  urlPattern: /^https:\/\/example\.com\/api/,
  handler: 'networkFirst'
}, {
  urlPattern: /\/articles\//,
  handler: 'fastest',
  options: {
    cache: {
      maxEntries: 10,
      name: 'articles-cache'
    }
  }
}]

The sw-precache + sw-toolbox explainer has more information about how and why you'd use both libraries together.

Default: []

skipWaiting [Boolean]

Controls whether or not the generated service worker will call skipWaiting() inside the install handler.

By default, when there's an update to a previously installed service worker, then the new service worker delays activation and stays in a waiting state until all pages controlled by the old service worker are unloaded. Calling skipWaiting() allows a newly registered service worker to bypass the waiting state.

When skipWaiting is true, the new service worker's activate handler will be called immediately, and any out of date cache entries from the previous service worker will be deleted. Please keep this in mind if you rely on older cached resources to be available throughout the page's lifetime, because, for example, you defer the loading of some resources until they're needed at runtime.

Default: true

staticFileGlobs [Array⟨String⟩]

An array of one or more string patterns that will be passed in to glob. All files matching these globs will be automatically precached by the generated service worker. You'll almost always want to specify something for this.

Default: []

stripPrefix [String]

Removes a specified string from the beginning of path URL's at runtime. Use this option when there's a discrepancy between a relative path at build time and the same path at run time. For example, if all your local files are under dist/app/ and your web root is also at dist/app/, you'd strip that prefix from the start of each local file's path in order to get the correct relative URL.

Default: ''

stripPrefixMulti [Object]

Maps multiple strings to be stripped and replaced from the beginning of URL paths at runtime. Use this option when you have multiple discrepancies between relative paths at build time and the same path at run time. If stripPrefix and replacePrefix are not equal to '', they are automatically added to this option.

stripPrefixMulti: {
  'www-root/public-precached/': 'public/',
  'www-root/public/': 'public/'
}

Default: {}

templateFilePath [String]

The path to the (lo-dash) template used to generate service-worker.js. If you need to add additional functionality to the generated service worker code, it's recommended that you use the importScripts option to include extra JavaScript rather than using a different template. But if you do need to change the basic generated service worker code, please make a copy of the original template, modify it locally, and use this option to point to your template file.

Default: service-worker.tmpl (in the directory that this module lives in)

verbose [boolean]

Determines whether there's log output for each individual static/dynamic resource that's precached. Even if this is set to false, there will be a final log entry indicating the total size of all precached resources.

Default: false

Wrappers and Starter Kits

While it's possible to use the sw-precache module's API directly within any JavaScript environment, several wrappers have been developed by members of the community tailored to specific build environments. They include:

There are also several starter kits or scaffolding projects that incorporate sw-precache into their build process, giving you a full service worker out of the box. The include:

CLIs

Starter Kits

Recipes for writing a custom wrapper

While there are not always ready-to-use wrappers for specific environments, this list contains some recipes to integrate sw-precache in your workflow:

Acknowledgements

Thanks to Sindre Sorhus and Addy Osmani for their advice and code reviews. Jake Archibald was kind enough to review the service worker logic.

License

Copyright © 2017 Google, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

sw-precache's People

Contributors

addyosmani avatar alexendoo avatar asolove avatar denar90 avatar eauc avatar goldob avatar guria avatar halfninja avatar ithinkihaveacat avatar jeffposnick avatar jpmedley avatar karolklp avatar localnerve avatar mahwy avatar maxweldsouza avatar mbj36 avatar mikestead avatar mlcdf avatar natecox avatar paradox41 avatar rafalborczuch avatar rlmcneary2 avatar rmacklin avatar signpostmarv avatar talater avatar taneltm avatar timvdlippe avatar vernondegoede avatar yangchenyun avatar zenorocha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sw-precache's Issues

Inconsistent state after change of cacheId

Changing the cacheId while keeping the same service worker registration seems to leave the cache in a not-great state. It's probably not a common practice, but it would be good to get to the bottom of what's going on.

dynamicUrlToDependencies Tightly coupled

I've just faced a non-obvious issue with build process for app-shell.

I was hitting an issue where my CSS wasn't getting updated in the app shell. What I realised is that the endpoint '/app-shell' inlines a CSS file, which means that as far as sw-precache is concerned the css file is a template - this results in a oddly tight coupling of what my server is doing and what sw-precache is doing.

I'm not sure of a nice way of getting around this. In an ideal world sw-precache would just look at the endpoint and do a diff based on latest content - but this ends up requiring a server running to work.

Can't run with Grunt

Sorry for the stupid question, but how to use this plugin in the project?
I have tried to run $ grunt from demo folder, but it gives me following error:

grunt-cli: The grunt command line interface. (v0.1.13)

Fatal error: Unable to find local grunt.

If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:

http://gruntjs.com/getting-started

Don't include the cache-busting URL parameter in the cache key

Right now, we include the cache-busting URL parameter in the key name for the cache entries. This works fine with the logic sw-precache has in place for doing cache lookups, but if we excluded the cache-busting URL parameter, we would play nicely with service worker code outside of sw-precache that uses caches.match().

Add a new `verbose` option

Probably safe to default to off. When it's on, the individual log statements about which resources are being precached are logged, in addition to the total precache size. When it's off, just the total precache size is logged.

Simplify options

Moving my comment from #27:

And since we specify a rootDir option, we might be able to get rid of the stripPrefix option usage and the rootDir thing in staticFileGlobs.

The options feels a bit verbose. Would be nice to be able to simplify it.

Need an option to exclude files from precaching

If I'm building a new app, I can structure my site such that I can easily separate files to be cached from those that shouldn't. If I'm adding precache to an existing site, the separation may not be that simple. I might have paths in build scripts for example that aren't easily changed.

I can imagine two possible implementations for this. It could be a member of the options object or it could be a list in a file called .precache-ignore (or something like that).

General Feedback

  • Error cases just throw errors, there is no helpful error messages before the problem is reached.
    • Examples
      • dynamicUrlToDependencies, I first just gave it an array assuming it would take an array of urls, this causes an error when a sort method it called
      • The tool was struggling to find some file somewhere and I get: Error: ENOENT: no such file or directory, stat 'server/app-shell.handlebars'
  • dynamicUrlToDependencies just feels a bit bluergh to me compared to vanilla SW JS where you say here's the url, cache it. This is much more involved and not sure of the benefit.
  • I have no affirmation of what has been cached. I was partly hoping that the generated sw.js file would be fairly clean to parse and see what files are cached, but the following threw me off:
    • it's heavy on eslint comments (Not sure why)
    • the array of files isn't formatted particularly friendly for quick parsing (i.e. no new lines and the file name plus hash is a little weird).
    • functions are first in the doc rather than the sw event listeners which is a shame, kind of hides SW from first glance of the file
  • Would be nice of the options had examples of the input. When I started using them I felt like I had to guess what was expected (which is both before and after reading the docs - I'm a bad person I know).
  • Caching mechanisms are basically non-existent in this scenario, it seems like it's always cache first and network if it's not cached.
  • Last bit of feedback which is a bit more open ended. I feel this is all a bit too divorced from SW for me to be able to "use" or "change" swprecache. In an ideal world I'd just have the gulp plugin that would find my SW file, inject the files I globbed for, and then inside the SW file I'd have a thirdparty script that would basically give me lots of helper methods to manage a fetch request. For example, navigateFallback didn't work at first and I thought it was set up correctly, but in terms of debugging, I had to guess what was wrong and try and fix it. Flip side of this is that when it does work its awesome.

Notify which assets have updated

Is there someway to be notified of which assets have been updated? For example, if a new stylesheet came in, using postMessage we can get its path, and update the src of the current stylesheet - avoiding a reload. If api data was refreshed we can pass this to our data layer via postMessage without ever notifying the user or requiring a refresh. Obviously there are some cases where the hassel isnt warranted, but likewise, there are plenty of cases where a full refresh is an overkill and a bad ux.

onmessage handler to clear all caches

Forcibly delete all caches for the given origin, invoked via a postMessage() with a specific payload from a controlled page. Useful for sites that need to implement a "kill switch".

Missing sw-toolbox source map when inlining sw-toolbox

Looks like the latest Chrome Version 49.0.2623.75 has caused

GET http://localhost:8009/service-worker.js net::ERR_FILE_EXISTS, service worker failed to load every time.

And also caused Failed to parse SourceMap: http://localhost:8009/sw-toolbox.map.json
To get rid of the parse error, I had to change sourceMappingURL in the generated service-worker.js
from
//# sourceMappingURL=sw-toolbox.map.json
to
//# sourceMappingURL=node_modules/sw-toolbox/sw-toolbox.map.json

This was all working up until this morning.with version 48

Thanks.

sw-precache and implicit index.html rewrites

Many servers are configured to handle a request for a bare directory URL as being equivalent to the request for index.html within that directory.

It's possible, but not intuitive, to handle this using the dynamicUrlToDependencies parameter. This approach also forces you to list entries for every directory that has an implicit index.html rewrite.

At the very least, the docs should clarify this behavior. Better, sw-precache should add a new option to configure what bare directories get rewritten to (e.g. index.html,index.php,index) and should automatically handle serving the appropriate file without the need for [dynamicUrlToDependencies`] mappings.

Better explanation of how importScripts can extend default behavior

One of the takeaways from @gauntface's feedback at #43 is that it's not clear how an experienced SW developer can extend the default sw-precache behavior to, e.g., implement a specific runtime caching strategy for a specific URL pattern. Or write their own SW push event handlers, which is something that sw-precache doesn't attempt to do at all.

@jpmedley, this is a topic I tried to touch on in Service Workers in Production, but we could use a specific set of guidance in the docs that covers this use case. Something like "sw-precache handles your static resources. To extend your SW to handle other things, use importScripts(...)..." and then some inline examples and a link to the App Shell demo I'm working on, or the Web Starter Kit integration.

Advanced App Shell routing

The current release of sw-precache has a very rudimentary "routing" system in place, in which navigations to either all URLs or just URLs that match a set of RegExps will result in the App Shell HTML, via the navigateFallback and navigateFallbackWhitelist options.

This works well enough for sites that only have a single App Shell within the scope of their service worker, but it would break down when there are multiple App Shells that might be used for different URL routes.

Something more flexible allowing arbitrary route-to-App Shell mappings would allow for more complicated web app setups.

And then it may or may not make sense to combine this routing with the routing rules used to define sw-toolbox runtime caching via the runtimeCaching option.

CC: @addyosmani @gauntface @wibblymat for thoughts.

CLI access

Most major tools in this area have a CLI counterpart. It'd be great to have a CLI for sw-precache as well.

Support gulp vinyl streams

Not sure if this is possible currently...but I was thinking that it might be useful if the module supported vinyl streams.

This would allow further processing on the generated service worker script (eg. minify, create source maps etc.) before writing it out, using standard stream functions, e.g. .pipe(), .dest().

Not that service worker scripts are large; but...minify all the things!

Something along the lines of:

const sourceMaps = require("gulp-sourcemaps"),
      swPrecache = require("sw-precache"),
      uglify = require("gulp-uglify"),
      util = require("gulp-util"),

      // Options for generating service worker
      swOptions = {
        scriptName: "my-service-worker.js",  // new option (default: 'service-worker.js')
        cacheId: "my-app",
        staticFileGlobs: ["dist/**/*.{html,js,css}"],
        stripPrefix: "dist",
        logger: util.log
      };

// Generate minified service worker & source map
gulp.task("build:serviceworker", () => swPrecache.generate(swOptions)
  .pipe(sourceMaps.init())
    .pipe(uglify())
  .pipe(sourceMaps.write("."))
  .pipe(gulp.dest("dist"))
  .on("error", util.log));

The above would create a minified /dist/my-service-worker.js, along with a corresponding /dist/my-service-worker.js.map source map.

Add ability to name service-worker.js something different

[Low priority feature request]

I'd like to import the generated sw-precache SW into my existing SW, which is already named service-worker.js so it would be nice to have an option to control the name.

I know I could do the reverse and import my actual SW into the sw-precache one but for various reasons I'd rather not.

When is app shell updated?

This is probably a silly question, but I've used sw-precache for my site which worked great initially, but the only way I'm seeing the cached files updating is if I unregister the service worker.

When using this in production, when are the cached files supposed to update themselves? Am I doing something wrong perhaps?

"Couldn't serve response for..." and "net::ERR_FILE_EXISTS" errors encountered when reloading the page after clicking "Clear Cache"

Start the demo app using "gulp serve-dist".
Load the URL (http://localhost:3000/).
Use "chrome://serviceworker-internals/" to unregister the ServiceWorker.
Reload the page.
Click "Clear Cache".
Reload the page.
You'll get a bunch of errors:

Couldn't serve response for "http://localhost:3000/" from cache: Error: The cache sw-precache-v1-sw-precache-http://localhost:3000/-http://localhost:3000/index.html-d378b5b669cd3e69fcf8397eba85b67d is empty

GET http://localhost:3000/service-worker.js net::ERR_FILE_EXISTS

See the attached screen shot.

Include a "version" as part of the CacheNamePrefix

Useful once #2 is implemented to ensure that we don't read from any incompatible caches.

I'm assuming it would just hardcode v1 for now, and only increment if something fundamental changes in the way we generate service-worker.js that would make reading from the old caches a no-go.

Defining a directory that doesn't exist

If you attempt to write to a path that doesn't exist at the time its run, swPrecache.write throws:

Error: ENOENT: no such file or directory, open '/service-worker.js'

Project Structure

During the switch to google eslint config, the structure of the project meant is wasn't as straight forward as it could have been due to the different eslint configs in different directories.

There are a few things that could be different to help this:

  1. Get rid of all demos, keep this repo as lean and on point as humanly possible and move demos to a different repo (perhaps submodule in the sw-precache code for testing and use)
  2. Structure to just /lib, /test/ and /demos/, then have /app-shell/ and /basic/ inside demos
  3. Move the linting of the built demo over to a test for linting the test output rather than have it as part of the linting task in in Gulp.

No Cookies during precache/cache request

Hey,

The sw-precache does not seem to allow the fetch feature, including cookies / credentials.
Actually, Grunt generates a default fetch, which does not send cookies/credentials.
For instance, it is quite problematic when you want to precache dynamic resources from an app server.

Fetch API allows it, with mode and credentials attributes.

I may be wrong, but I did not find it in the precache API.

C.

Error on dynamicUrlToDependencies

I had the following config:

swPrecache.write(path.join(GLOBAL.config.dest, 'sw.js'), {
    staticFileGlobs: [
      GLOBAL.config.dest + '/**/*.{js,html,css,png,jpg,jpeg,gif,svg}',
      GLOBAL.config.dest + '/manifest.json'
    ],
    dynamicUrlToDependencies: {
      '/app-shell': ['server/views/layouts/app-shell.handlebars'],
      '/api/': [
        'server/views/layouts/partial.handlebars',
        'server/views/index.handlebars'
      ],
      '/api/url-1': [
        'server/views/layouts/partial.handlebars',
        'server/views/url-1.handlebars'
      ],
      '/api/url-2': [
        'server/views/layouts/partial.handlebars',
        'server/views/url-2.handlebars'
      ]
    },
    stripPrefix: GLOBAL.config.dest,
    navigateFallback: '/app-shell',
    cacheId: packageName
  }).then(cb);

I deleted the partial.handlebars which caused the task to not finish (Gulp logs below):

[17:09:07] Finished 'scripts:es6' after 4.09 s
[17:09:07] Finished 'scripts' after 5.17 s
[17:09:07] Starting 'service-worker'...
$

Probably suggests two things:

1.) Have a catch handler on the promise in the docs
2.) Throw an error from swPrecache on this edge case

Possible to use a single cache?

Chatting with one of the engineers and the topic of using multiple caches (i.e. one per asset) could be poor in terms of the number of instances of caches in memory at any one time.

Is there a specific reason precache couldn't use a single cache?

`.write()` convenience function

The use of this module in WSK is currently a bit verbose. Would be nice to be able to simplify it a bit. First I think this module should have a convenience method to write the file to disk.

From:

var rootDir = 'dist';

swPrecache({
  cacheId: packageJson.name || 'web-starter-kit',
  dynamicUrlToDependencies: {
    './': [path.join(rootDir, 'index.html')]
  },
  staticFileGlobs: [
    rootDir + '/fonts/**/*.woff',
    rootDir + '/images/**/*',
    rootDir + '/scripts/**/*.js',
    rootDir + '/styles/**/*.css',
    rootDir + '/*.{html,json}'
  ],
  stripPrefix: path.join(rootDir, path.sep)
}, function (error, serviceWorkerFileContents) {
  if (error) {
    return callback(error);
  }
  fs.writeFile(path.join(rootDir, 'service-worker.js'),
    serviceWorkerFileContents, function (error) {
    if (error) {
      return callback(error);
    }
    callback();
  });
});

To:

var rootDir = 'dist';

swPrecache.write('service-worker.js', {
  rootDir: 'dist',
  cacheId: packageJson.name || 'web-starter-kit',
  dynamicUrlToDependencies: {
    './': [path.join(rootDir, 'index.html')]
  },
  staticFileGlobs: [
    rootDir + '/fonts/**/*.woff',
    rootDir + '/images/**/*',
    rootDir + '/scripts/**/*.js',
    rootDir + '/styles/**/*.css',
    rootDir + '/*.{html,json}'
  ],
  stripPrefix: path.join(rootDir, path.sep)
}, function () {});

And since we specify a rootDir option, we might be able to get rid of the stripPrefix option usage and the rootDir thing in staticFileGlobs.

Grunt sw-precache runs but caches nothing

Firstly, thanks in advance for those who are helping. And thanks for making such a wonderful tool.

The Gruntfile was generated by yeoman generator-angular 0.14.0.

I edited it to incorporate sw-precache.

Mac OS X El Capitan, 10.11.2(15C50)

I'm using

node v5.1.0
npm v3.3.12
grunt-cli v0.1.13
grunt v0.4.5
yeoman v1.5.1
sw-precache v2.3.0

Grunt "fails"

On running

grunt swPrecache:build

I received,

Running "swPrecache:build" (swPrecache) task
Total precache size is about 0 B for 0 resources.

Done, without errors.


Execution Time (2015-12-30 00:44:56 UTC)
loading tasks     164ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 89%
swPrecache:build   20ms  ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 11%
Total 184ms

CLI works

But on running sw-precache cli

sw-precache --verbose=true --root=dist --static-file-globs='dist/**/*.{html,css,js,jpg,png}'

I received,

Caching static resource "dist/404.html" (3.3 kB)
Caching static resource "dist/images/39da2e97bdf244d89c5a00f8f424984f.cd131126.jpg" (115.45 kB)
Caching static resource "dist/images/42e16924568c29040d602959f797761d.416c74df.jpg" (29.74 kB)
Caching static resource "dist/images/4cb9845bd0975b36917e3063934132d2.9cde9e8c.jpg" (56.57 kB)
Caching static resource "dist/images/AT_SHIRT_BACK_7_white.06e4f06d.jpg" (49.7 kB)
Caching static resource "dist/images/AT_SHIRT_BACK_7.d87cf4ae.jpg" (55.01 kB)
Caching static resource "dist/images/ATFUNDAY1.1da5069d.jpg" (594.38 kB)
Caching static resource "dist/images/ATFUNDAY2.a51caf82.jpg" (528.8 kB)
Caching static resource "dist/images/ATFUNDAY3.afc404b6.jpg" (625.13 kB)
Caching static resource "dist/images/ATFUNDAY4.d1d8f876.jpg" (709.45 kB)
Caching static resource "dist/images/ATFUNDAY5.d22cdbf1.jpg" (777.39 kB)
Caching static resource "dist/images/ATFUNDAY6.710bf95b.jpg" (651.05 kB)
Caching static resource "dist/images/cover-mobile.bd3d8b5f.jpg" (237.4 kB)
Caching static resource "dist/images/dragonball.6da5595c.jpg" (67.66 kB)
Caching static resource "dist/images/humanfusball.29824bf6.jpg" (713.82 kB)
Caching static resource "dist/images/last.1a4fc262.jpg" (42.91 kB)
Caching static resource "dist/images/last2.77816a8d.png" (201.74 kB)
Caching static resource "dist/images/last2.b937f765.jpg" (142.43 kB)
Caching static resource "dist/images/maxresdefault.8a31c896.jpg" (211.54 kB)
Caching static resource "dist/images/npat.acfd2dc4.jpg" (35.48 kB)
Caching static resource "dist/images/pattern.1818068c.png" (183.25 kB)
Caching static resource "dist/images/Small-Magellanic-Cloud.92ea29c6.jpg" (1.85 MB)
Caching static resource "dist/images/yeoman.8cb970fb.png" (8.49 kB)
Caching static resource "dist/index.html" (1.47 kB)
Caching static resource "dist/scripts/scripts.48790507.js" (17.47 kB)
Caching static resource "dist/scripts/vendor.b10599dc.js" (483.24 kB)
Caching static resource "dist/service-worker.js" (8.97 kB)
Caching static resource "dist/styles/main.f70680f4.css" (2.29 kB)
Caching static resource "dist/styles/vendor.03a02723.css" (201.49 kB)
Caching static resource "dist/swreg.js" (2.57 kB)
Total precache size is about 8.6 MB for 30 resources.
dist/service-worker.js has been generated with the service worker contents.

Whatever that was cached by the sw-precache cli is what i wanted. Please please help me I couldn't figure out what was wrong.

Gruntfile attached

The Gruntfile.js is attached here. I attached the whole file because i was afraid i missed out something.

// Generated on 2015-12-03 using generator-angular 0.14.0
'use strict';

// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'

module.exports = function (grunt) {
  var path = require('path'),
  swPrecache = require('sw-precache'),
  packageJson = require('./package.json');

  // Time how long tasks take. Can help when optimizing build times
  require('time-grunt')(grunt);

  // Automatically load required Grunt tasks
  require('jit-grunt')(grunt, {
    useminPrepare: 'grunt-usemin',
    ngtemplates: 'grunt-angular-templates',
    cdnify: 'grunt-google-cdn',
    karm: 'grunt-karma'
  });

  // Configurable paths for the application
  var appConfig = {
    app: require('./bower.json').appPath || 'app',
    dist: 'dist'
  };

  // Define the configuration for all the tasks
  grunt.initConfig({

    // Project settings
    yeoman: appConfig,

    // Watches files for changes and runs tasks based on the changed files
    watch: {
      bower: {
        files: ['bower.json'],
        tasks: ['wiredep']
      },
      js: {
        files: ['<%= yeoman.app %>/scripts/{,*/}*.js'],
        tasks: ['newer:jshint:all', 'newer:jscs:all'],
        options: {
          livereload: '<%= connect.options.livereload %>'
        }
      },
      jsTest: {
        files: ['test/spec/{,*/}*.js'],
        tasks: ['newer:jshint:test', 'newer:jscs:test', 'karma']
      },
      styles: {
        files: ['<%= yeoman.app %>/styles/{,*/}*.css'],
        tasks: ['newer:copy:styles', 'postcss']
      },
      gruntfile: {
        files: ['Gruntfile.js']
      },
      livereload: {
        options: {
          livereload: '<%= connect.options.livereload %>'
        },
        files: [
          '<%= yeoman.app %>/{,*/}*.html',
          '.tmp/styles/{,*/}*.css',
          '<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
        ]
      }
    },

    // The actual grunt server settings
    connect: {
      options: {
        port: 9000,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: 'localhost',
        livereload: 35729
      },
      livereload: {
        options: {
          open: true,
          middleware: function (connect) {
            return [
              connect.static('.tmp'),
              connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ),
              connect().use(
                '/app/styles',
                connect.static('./app/styles')
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      test: {
        options: {
          port: 9001,
          middleware: function (connect) {
            return [
              connect.static('.tmp'),
              connect.static('test'),
              connect().use(
                '/bower_components',
                connect.static('./bower_components')
              ),
              connect.static(appConfig.app)
            ];
          }
        }
      },
      dist: {
        options: {
          open: true,
          base: '<%= yeoman.dist %>'
        }
      }
    },

    // Make sure there are no obvious mistakes
    jshint: {
      options: {
        jshintrc: '.jshintrc',
        reporter: require('jshint-stylish')
      },
      all: {
        src: [
          'Gruntfile.js',
          '<%= yeoman.app %>/scripts/{,*/}*.js'
        ]
      },
      test: {
        options: {
          jshintrc: 'test/.jshintrc'
        },
        src: ['test/spec/{,*/}*.js']
      }
    },

    // Make sure code styles are up to par
    jscs: {
      options: {
        config: '.jscsrc',
        verbose: true
      },
      all: {
        src: [
          'Gruntfile.js',
          '<%= yeoman.app %>/scripts/{,*/}*.js'
        ]
      },
      test: {
        src: ['test/spec/{,*/}*.js']
      }
    },

    // Empties folders to start fresh
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            '.tmp',
            '<%= yeoman.dist %>/{,*/}*',
            '!<%= yeoman.dist %>/.git{,*/}*'
          ]
        }]
      },
      server: '.tmp'
    },

    // Add vendor prefixed styles
    postcss: {
      options: {
        processors: [
          require('autoprefixer-core')({browsers: ['last 1 version']})
        ]
      },
      server: {
        options: {
          map: true
        },
        files: [{
          expand: true,
          cwd: '.tmp/styles/',
          src: '{,*/}*.css',
          dest: '.tmp/styles/'
        }]
      },
      dist: {
        files: [{
          expand: true,
          cwd: '.tmp/styles/',
          src: '{,*/}*.css',
          dest: '.tmp/styles/'
        }]
      }
    },

    // Automatically inject Bower components into the app
    wiredep: {
      app: {
        src: ['<%= yeoman.app %>/index.html'],
        ignorePath:  /\.\.\//
      },
      test: {
        devDependencies: true,
        src: '<%= karma.unit.configFile %>',
        ignorePath:  /\.\.\//,
        fileTypes:{
          js: {
            block: /(([\s\t]*)\/{2}\s*?bower:\s*?(\S*))(\n|\r|.)*?(\/{2}\s*endbower)/gi,
              detect: {
                js: /'(.*\.js)'/gi
              },
              replace: {
                js: '\'{{filePath}}\','
              }
            }
          }
      }
    }, 

    // Renames files for browser caching purposes
    filerev: {
      dist: {
        src: [
          '<%= yeoman.dist %>/scripts/{,*/}*.js',
          '<%= yeoman.dist %>/styles/{,*/}*.css',
          '<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
          '<%= yeoman.dist %>/styles/fonts/*'
        ]
      }
    },

    // Reads HTML for usemin blocks to enable smart builds that automatically
    // concat, minify and revision files. Creates configurations in memory so
    // additional tasks can operate on them
    useminPrepare: {
      html: '<%= yeoman.app %>/index.html',
      options: {
        dest: '<%= yeoman.dist %>',
        flow: {
          html: {
            steps: {
              js: ['concat', 'uglifyjs'],
              css: ['cssmin']
            },
            post: {}
          }
        }
      }
    },

    // Performs rewrites based on filerev and the useminPrepare configuration
    usemin: {
      html: ['<%= yeoman.dist %>/{,*/}*.html'],
      css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
      js: ['<%= yeoman.dist %>/scripts/{,*/}*.js'],
      options: {
        assetsDirs: [
          '<%= yeoman.dist %>',
          '<%= yeoman.dist %>/images',
          '<%= yeoman.dist %>/styles'
        ],
        patterns: {
          js: [[/(images\/[^''""]*\.(png|jpg|jpeg|gif|webp|svg))/g, 'Replacing references to images']]
        }
      }
    },

    // The following *-min tasks will produce minified files in the dist folder
    // By default, your `index.html`'s <!-- Usemin block --> will take care of
    // minification. These next options are pre-configured if you do not wish
    // to use the Usemin blocks.
    // cssmin: {
    //   dist: {
    //     files: {
    //       '<%= yeoman.dist %>/styles/main.css': [
    //         '.tmp/styles/{,*/}*.css'
    //       ]
    //     }
    //   }
    // },
    // uglify: {
    //   dist: {
    //     files: {
    //       '<%= yeoman.dist %>/scripts/scripts.js': [
    //         '<%= yeoman.dist %>/scripts/scripts.js'
    //       ]
    //     }
    //   }
    // },
    // concat: {
    //   dist: {}
    // },

    imagemin: {
      dist: {
        files: [{
          expand: true,
          cwd: '<%= yeoman.app %>/images',
          src: '{,*/}*.{png,jpg,jpeg,gif}',
          dest: '<%= yeoman.dist %>/images'
        }]
      }
    },

    svgmin: {
      dist: {
        files: [{
          expand: true,
          cwd: '<%= yeoman.app %>/images',
          src: '{,*/}*.svg',
          dest: '<%= yeoman.dist %>/images'
        }]
      }
    },

    htmlmin: {
      dist: {
        options: {
          collapseWhitespace: true,
          conservativeCollapse: true,
          collapseBooleanAttributes: true,
          removeCommentsFromCDATA: true
        },
        files: [{
          expand: true,
          cwd: '<%= yeoman.dist %>',
          src: ['*.html'],
          dest: '<%= yeoman.dist %>'
        }]
      }
    },

    ngtemplates: {
      dist: {
        options: {
          module: 'npatApp',
          htmlmin: '<%= htmlmin.dist.options %>',
          usemin: 'scripts/scripts.js'
        },
        cwd: '<%= yeoman.app %>',
        src: 'views/{,*/}*.html',
        dest: '.tmp/templateCache.js'
      }
    },

    // ng-annotate tries to make the code safe for minification automatically
    // by using the Angular long form for dependency injection.
    ngAnnotate: {
      dist: {
        files: [{
          expand: true,
          cwd: '.tmp/concat/scripts',
          src: '*.js',
          dest: '.tmp/concat/scripts'
        }]
      }
    },

    // Replace Google CDN references
    cdnify: {
      dist: {
        html: ['<%= yeoman.dist %>/*.html']
      }
    },

    // Copies remaining files to places other tasks can use
    copy: {
      dist: {
        files: [{
          expand: true,
          dot: true,
          cwd: '<%= yeoman.app %>',
          dest: '<%= yeoman.dist %>',
          src: [
            '*.{ico,png,txt}',
            '*.html',
            'images/{,*/}*.{webp}',
            'styles/fonts/{,*/}*.*',
            'videos/**/*'
          ]
        }, {
          expand: true,
          cwd: '.tmp/images',
          dest: '<%= yeoman.dist %>/images',
          src: ['generated/*']
        }]
      },
      styles: {
        expand: true,
        cwd: '<%= yeoman.app %>/styles',
        dest: '.tmp/styles/',
        src: '{,*/}*.css'
      }
    },

    // Run some tasks in parallel to speed up the build process
    concurrent: {
      server: [
        'copy:styles'
      ],
      test: [
        'copy:styles'
      ],
      dist: [
        'copy:styles',
        'imagemin',
        'svgmin'
      ]
    },

    // Test settings
    karma: {
      unit: {
        configFile: 'test/karma.conf.js',
        singleRun: true
      }
    },

    //swPrecache
    swPrecache:{
      build: {
        handleFetch: true,
        rootDir: 'dist'
      }
    }
  });

  function writeServiceWorkerFile(rootDir, handleFetch, callback) {
    var config = {
      cacheId: packageJson.name,
      // If handleFetch is false (i.e. because this is called from swPrecache:dev), then
      // the service worker will precache resources but won't actually serve them.
      // This allows you to test precaching behavior without worry about the cache preventing your
      // local changes from being picked up during the development cycle.
      handleFetch: handleFetch,
      staticFileGlobs: [
        rootDir + '/**/*.{html,css,js,jpg,png}'
      ],
      stripPrefix: rootDir
    };

    swPrecache.write(path.join(rootDir, 'service-worker.js'), config, callback);
  }


  grunt.registerMultiTask('swPrecache', function() {
    var done = this.async();
    var rootDir = this.data.rootDir;
    var handleFetch = this.data.handleFetch;

    //console.log(this);

    writeServiceWorkerFile(rootDir, handleFetch, function(error) {
      if (error) {
        grunt.fail.warn(error);
      }
      done();
    });
  });


  grunt.registerTask('serve', 'Compile then start a connect web server', function (target) {
    if (target === 'dist') {
      return grunt.task.run(['build', 'connect:dist:keepalive']);
    }

    grunt.task.run([
      'clean:server',
      'wiredep',
      'concurrent:server',
      'postcss:server',
      'connect:livereload',
      'watch'
    ]);
  });

  grunt.registerTask('server', 'DEPRECATED TASK. Use the "serve" task instead', function (target) {
    grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
    grunt.task.run(['serve:' + target]);
  });

  grunt.registerTask('test', [
    'clean:server',
    'wiredep',
    'concurrent:test',
    'postcss',
    'connect:test',
    'karma'
  ]);

  grunt.registerTask('build', [
    'clean:dist',
    'wiredep',
    'useminPrepare',
    'concurrent:dist',
    'postcss',
    'ngtemplates',
    'concat',
    'ngAnnotate',
    'copy:dist',
    'cdnify',
    'cssmin',
    'uglify',
    'filerev',
    'usemin',
    'htmlmin',
    'swPrecache'
  ]);

  grunt.registerTask('default', [
    'newer:jshint',
    'newer:jscs',
    'test',
    'build'
  ]);
};

Notify the page about which resources were updated

Use postMessage() to notify controlled pages about which resources were updated when an install event kicks off. This would give controlled pages the option of taking specific actions based on what's updated, e.g. display a "Please reload" toast if a core HTML resource was updated, or just ignore it if a non-essential image resource was updated.

Don't cache-bust versioned resources while prefetching

We currently append a cache-busting URL query parameter to requests made during the install handler, to ensure that we always get a fresh response which bypasses the HTTP cache.

This works even if the resource is explicitly versioned with, e.g., a hash in its filename, but it results in unnecessary network traffic—a hash in the filename should be considered a sign that the resource is explicitly versioned, and the copy in the HTTP cache with the same name should be up to date.

The default behavior should stay as-is, but we should add in a new RegExp configuration option, which developers could set to something like, e.g., ^\w+\.\w{14}\.\w+$, depending on what their versioned filenames look like. Filenames which match that RegExp would be fetched without the cache-busting URL query parameter during the install handler.

Take cache name prefix into account when handling fetch()

Right now the code uses CacheStorage.match() (via the polyfill) when determining whether there's a matching cache entry inside the fetch handler. This has the potential to match entries that live in caches that don't start with CacheNamePrefix. There's an edge case where if CacheNamePrefix changes (maybe because the registration scope schanges), old cache entries won't be auto-deleted and the CacheStorage.match() call might find those stale entries.

To work around this, the code should only look at caches whose name starts with the current CacheNamePrefix when attempting to match.

Since CacheStorage.match() is currently polyfilled on all browsers, this shouldn't incur a performance hit (it might be slightly more efficient). Versus a hypothetical native CacheStorage.match() implementation, though, it's going to be less performant.

Handle non-static js files

Thanks for the awesome lib. Very useful.

I'm having trouble adding dynamic files that are not yet available during build time. E.g. http://localhost:8889/socket.io/socket.io.js is a js file that gets served by the socket.io server at runtime:

dynamicUrlToDependencies: {
    'socket.io/socket.io.js': ['socket.io/socket.io.js'],
}

Unfortunately this throws the following error when running the grunt script:

Warning: ENOENT, no such file or directory 'socket.io/socket.io.js' Use --force to continue.

Am I missing something or can you only add static files to the precache?

Thanks

webpack

Is there any way to get this working using webpack, not gulp ?

I already have a stack I've using for my projects and its using webpack, would love to use sw-precache with the same stack.

Any tips / advice ?

intercept all non-extension requests and send index.html for an html5-routed web app

Hello everyone, thanks for your work on this project. I'm using it in creating my side project, financier, an Angular 1, PouchDB offline webapp.

I'm curious how you guys would approach html5 routing in a best practice with sw-precache. My google-fu is not showing anyone else that has encountered this before.

For example, I cache index.html, and then this allows navigating to example.com/ to work. However, example.com/an-html5-route doesn't load index.html, like it does in a traditional, properly configured server.

What is the best way to intercept all requests without an extension and send them index.html?

The equivalent functionality in express:

// define other request handlers above this
app.all('/*', function(req, res) {
  res.sendfile('index.html');
});

If this isn't really an issue for sw-precache, feel free to close this and point me in the right direction. :)

Thanks!

Unminified sw-toolbox code for debugging

The version of sw-toolbox we inline into the generated service worker file is minified. We can't count on developers making the sourcemap available at a particular URL, so it would aid in debugging if we either took in a specific URL to use for the sourcemap as a parameter, or provided an option in which the unminified sw-toolbox code was included.

Allow passing in a custom filesystem in options

Would you consider a PR that adds the ability to pass in a custom fs option?

sw-precache would still use require('fs') by default, but I could override this with a custom fs object that implements the same methods, such as memory-fs.

This would allow me to use sw-precache as part of an in-memory pipeline of build steps – i.e. in situations where the files it's reading don't actually exist on disk; they only exist in memory, having been generated by prior build steps such as Babel and Browserify.

Handling non-local resources

Not sure where the roadmap is headed, but has there been any consideration for a use case where non-local resources might want/need to be cached? An example might be that in defining staticFileGlobs we might point to a resource on a CDN (for the sake of argument, let's say an image).

At the moment, sw-precache will ignore such a resource (getFileAndSizeAndHashForFile() will return null in the process of verifying the file on the local filesystem, which is obviously not there) and the SW isn't setup to handle mapping outside the existing domain (populateCurrentCacheNames() sets a baseUrl via self.location for mappings and handling requests would need no-cors mode).

I suppose my question boils down to:

  1. Is this a feature that is needed/wanted/planned?
  2. If yes, is there a preferred approach to that implementation?

I can think of a couple of different ways to go about this and I'm willing to put in some time to dev that feature out if it would be useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.