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

Commit

Permalink
supporting multiple stripPrefix & replacePrefix entries via stripPref…
Browse files Browse the repository at this point in the history
…ixMulti (#138)

* treat stripPrefix as an array to support environments that rewrite paths to another directory (i.e. server sends foo/file.txt for requests to bar/file.txt, as well as caching bar/other-file.txt)

* switching from array to map prefix/replace pairs, with documentation

* seems to be working as expected now, removing the conditional check

* satisfying eslint
  • Loading branch information
SignpostMarv authored and jeffposnick committed Jul 12, 2016
1 parent 204b2d0 commit e46423f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,20 @@ URL.

_Default_: `''`

#### stripPrefixMulti [Object]
Maps mutliple strings to be stripped & replaced from the beginning of path URL's 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.
```js
stripPrefixMulti: {
'www-root/public-precached/': 'public/',
'www-root/public/': 'public/'
}
```

_Default_: `{}`

#### templateFilePath [String]

The path to the ([lo-dash](https://lodash.com/docs#template)) template used to
Expand Down
8 changes: 6 additions & 2 deletions lib/sw-precache.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function generate(params, callback) {
navigateFallback: '',
navigateFallbackWhitelist: [],
stripPrefix: '',
stripPrefixMulti: {},
replacePrefix: '',
staticFileGlobs: [],
templateFilePath: path.join(
Expand All @@ -102,6 +103,7 @@ function generate(params, callback) {

var relativeUrlToHash = {};
var cumulativeSize = 0;
params.stripPrefixMulti[params.stripPrefix] = params.replacePrefix;

params.staticFileGlobs.forEach(function(globPattern) {
var filesAndSizesAndHashes = getFilesAndSizesAndHashesForGlobPattern(
Expand All @@ -113,8 +115,10 @@ function generate(params, callback) {
// Strip the prefix to turn this into a relative URL.
var relativeUrl = fileAndSizeAndHash.file
.replace(
new RegExp('^' + escapeRegExp(params.stripPrefix)),
params.replacePrefix)
new RegExp('^(' + Object.keys(params.stripPrefixMulti).map(escapeRegExp).join('|') + ')'),
function(match) {
return params.stripPrefixMulti[match];
})
.replace(path.sep, '/');
relativeUrlToHash[relativeUrl] = fileAndSizeAndHash.hash;

Expand Down

0 comments on commit e46423f

Please sign in to comment.