From bee1de36334a2fbc5134df1add84d36a52133590 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Tue, 18 Oct 2016 13:57:56 -0400 Subject: [PATCH 1/3] Promote the use of module.exports for external configuration --- README.md | 45 ++++++++++++++++++++++-------------- demo/sw-precache-config.js | 24 +++++++++++++++++++ demo/sw-precache-config.json | 24 ------------------- 3 files changed, 52 insertions(+), 41 deletions(-) create mode 100644 demo/sw-precache-config.js delete mode 100644 demo/sw-precache-config.json diff --git a/README.md b/README.md index 880d8fd..4434ac0 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,8 @@ the service worker lifecycle event you can listen for to trigger this message. For those who would prefer not to use `sw-precache` as part of a `gulp` or `Grunt` build, there's a [command-line interface](cli.js) which supports the -[options listed](#options-parameter) in the API, provided via flags. +[options listed](#options-parameter) in the API, provided via flags or an +external JavaScript configuration file. **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` @@ -195,32 +196,42 @@ $ sw-precache --root=dist --static-file-globs='dist/**/*.html' to your shell (such as the `*` characters in the sample command line above, for example). -Finally, there's support for storing a complex configuration in an external -JSON file, using `--config `. Any of the options from the file can be -overridden via a command-line flag. For example, +Finally, there's support for storing a complex configuration as the +[`module.exports`](https://nodejs.org/api/modules.html#modules_module_exports) +of an external JavaScript file, using `--config `. Any of the options from +the file can be overridden via a command-line flag. For example, ```sh -$ sw-precache --config=path/to/sw-precache-config.json --verbose --no-handle-fetch +$ sw-precache --config=path/to/sw-precache-config.js --verbose --no-handle-fetch ``` -will generate a service worker file using the options provided in the -`path/to/sw-precache-config.json` file, but with the `verbose` option set to +will generate a service worker file using the configuration exported in the +`path/to/sw-precache-config.js` file, but with the `verbose` option set to `true` and the `handleFetch` option set to `false`. -`sw-precache-config.json` might look like: +`sw-precache-config.js` might look like: -```json -{ - "staticFileGlobs": [ - "app/css/**.css", - "app/**.html", - "app/images/**.*", - "app/js/**.js" +```js +module.exports = { + staticFileGlobs: [ + 'app/css/**.css', + 'app/**.html', + 'app/images/**.*', + 'app/js/**.js' ], - "stripPrefix": "app/" -} + stripPrefix: 'app/', + runtimeCaching: [{ + urlPattern: /this\\.is\\.a\\.regex/, + handler: 'networkFirst' + }] +}; ``` +(Note: It's also possible to pass in a JSON file to `--config`. Using a +JavaScript file that defines `module.exports` allows for more flexibility than +JSON allows, such as providing a regular expression for the +runtimeCaching.urlPattern` option.) + ## API ### Methods diff --git a/demo/sw-precache-config.js b/demo/sw-precache-config.js new file mode 100644 index 0000000..9a97982 --- /dev/null +++ b/demo/sw-precache-config.js @@ -0,0 +1,24 @@ +module.exports = { + dynamicUrlToDependencies: { + 'dynamic/page1': [ + 'app/views/layout.jade', + 'app/views/page1.jade' + ], + 'dynamic/page2': [ + 'app/views/layout.jade', + 'app/views/page2.jade' + ] + }, + staticFileGlobs: [ + 'app/css/**.css', + 'app/**.html', + 'app/images/**.*', + 'app/js/**.js' + ], + stripPrefix: 'app/', + verbose: true, + runtimeCaching: [{ + urlPattern: /this\\.is\\.a\\.regex/, + handler: 'networkFirst' + }] +}; diff --git a/demo/sw-precache-config.json b/demo/sw-precache-config.json deleted file mode 100644 index 41b00ac..0000000 --- a/demo/sw-precache-config.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "dynamicUrlToDependencies": { - "dynamic/page1": [ - "app/views/layout.jade", - "app/views/page1.jade" - ], - "dynamic/page2": [ - "app/views/layout.jade", - "app/views/page2.jade" - ] - }, - "staticFileGlobs": [ - "app/css/**.css", - "app/**.html", - "app/images/**.*", - "app/js/**.js" - ], - "stripPrefix": "app/", - "verbose": true, - "runtimeCaching": [{ - "urlPattern": "/this\\.is\\.a\\.regex/", - "handler": "networkFirst" - }] -} From a03316398523e97a8118c63b91da9d4ae7b78c18 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Tue, 18 Oct 2016 14:03:51 -0400 Subject: [PATCH 2/3] Updated the cli.js --help text --- cli.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli.js b/cli.js index d1dae29..b5175c4 100755 --- a/cli.js +++ b/cli.js @@ -109,7 +109,8 @@ function setDefaults(cli, configFileFlags) { var cli = meow({ help: 'Options from https://github.com/GoogleChrome/sw-precache#options ' + 'are accepted as flags.\nAlternatively, use --config , where ' + - ' is the path to the JSON data representing the same options.\n' + + ' is the path to a JavaScript file that defines the same ' + + 'options via module.exports.\n' + 'When both a config file and command line option is given, the ' + 'command line option takes precedence.' }); From 4ed1e7a6cb326fef4557c3ee165b8bb95d40888e Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Tue, 18 Oct 2016 14:58:41 -0400 Subject: [PATCH 3/3] Review feedback --- README.md | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4434ac0..16373c6 100644 --- a/README.md +++ b/README.md @@ -196,20 +196,11 @@ $ sw-precache --root=dist --static-file-globs='dist/**/*.html' to your shell (such as the `*` characters in the sample command line above, for example). -Finally, there's support for storing a complex configuration as the -[`module.exports`](https://nodejs.org/api/modules.html#modules_module_exports) -of an external JavaScript file, using `--config `. Any of the options from -the file can be overridden via a command-line flag. For example, - -```sh -$ sw-precache --config=path/to/sw-precache-config.js --verbose --no-handle-fetch -``` - -will generate a service worker file using the configuration exported in the -`path/to/sw-precache-config.js` file, but with the `verbose` option set to -`true` and the `handleFetch` option set to `false`. - -`sw-precache-config.js` might look like: +Finally, there's support for passing complex configurations using `--config `. +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`](https://nodejs.org/api/modules.html#modules_module_exports). +For example, assume there's a `path/to/sw-precache-config.js` file that contains: ```js module.exports = { @@ -227,10 +218,34 @@ module.exports = { }; ``` -(Note: It's also possible to pass in a JSON file to `--config`. Using a -JavaScript file that defines `module.exports` allows for more flexibility than -JSON allows, such as providing a regular expression for the -runtimeCaching.urlPattern` option.) +That file could be passed to the command-line interface, while also setting the +`verbose` option, via + +```sh +$ 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: + +```json +{ + "staticFileGlobs": [ + "app/css/**.css", + "app/**.html", + "app/images/**.*", + "app/js/**.js" + ], + "stripPrefix": "app/", + "runtimeCaching": [{ + "urlPattern": "/express/style/path/(.*)", + "handler": "networkFirst" + }] +} +``` ## API