Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Promote the use of module.exports for external configuration #198

Merged
merged 3 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configuration via module.exports LGTM. I'm glad we maintain a note about the --config JSON configuration option (I personally use this in a few projects).

**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`
Expand All @@ -195,19 +196,40 @@ $ 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 <file>`. Any of the options from the file can be
overridden via a command-line flag. 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`](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 = {
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

```sh
$ sw-precache --config=path/to/sw-precache-config.json --verbose --no-handle-fetch
$ sw-precache --config=path/to/sw-precache-config.js --verbose
```

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
`true` and the `handleFetch` option set to `false`.
This provides the most flexibility, such as providing a regular expression for
the `runtimeCaching.urlPattern` option.

`sw-precache-config.json` might look like:
We also support passing in a JSON file for `--config`, though this provides
less flexibility:

```json
{
Expand All @@ -217,7 +239,11 @@ will generate a service worker file using the options provided in the
"app/images/**.*",
"app/js/**.js"
],
"stripPrefix": "app/"
"stripPrefix": "app/",
"runtimeCaching": [{
"urlPattern": "/express/style/path/(.*)",
"handler": "networkFirst"
}]
}
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you end up going for the earlier suggestions this text may end up getting moved around a little, but that's totally your call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per earlier, I'd suggest keeping in an example of the JSON config that is supported. As long as a feature is supported, it's good to show how it can be used imo (if this gets dropped at a later point then discouraging by not including examples makes more sense).

Expand Down
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file>, where ' +
'<file> is the path to the JSON data representing the same options.\n' +
'<file> 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.'
});
Expand Down
24 changes: 24 additions & 0 deletions demo/sw-precache-config.js
Original file line number Diff line number Diff line change
@@ -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'
}]
};
24 changes: 0 additions & 24 deletions demo/sw-precache-config.json

This file was deleted.