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

supporting multiple stripPrefix & replacePrefix entries via stripPrefixMulti #138

Merged
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
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