This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 388
Promote the use of module.exports for external configuration #198
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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 | ||
{ | ||
|
@@ -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" | ||
}] | ||
} | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}] | ||
}; |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).