Repository: GoogleChrome/sw-precache
Pull request #35 (2015-09-24 17:49:00)

Make the stripPrefix option only strip the prefix in a filename
Currently sw-precache is stripping the first occurrence of `stripPrefix`, but the first occurrence could be in the middle of the name.


Patch:
diff --git a/lib/sw-precache.js b/lib/sw-precache.js
index 83f84e0..a65d7dc 100644
--- a/lib/sw-precache.js
+++ b/lib/sw-precache.js
@@ -62,6 +62,11 @@ function getHash(data) {
   return md5.digest('hex');
 }
 
+// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
+function escapeRegExp(string) {
+  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
+}
+
 function generate(params, callback) {
   return new Promise(function(resolve, reject) {
     params = defaults(params || {}, {
@@ -97,7 +102,7 @@ function generate(params, callback) {
         if (fileAndSizeAndHash.size <= params.maximumFileSizeToCacheInBytes) {
           // Strip the prefix to turn this into a relative URL.
           var relativeUrl = fileAndSizeAndHash.file
-            .replace(params.stripPrefix, params.replacePrefix)
+            .replace(new RegExp('^' + escapeRegExp(params.stripPrefix)), params.replacePrefix)
             .replace(path.sep, '/');
           relativeUrlToHash[relativeUrl] = fileAndSizeAndHash.hash;
 


Test patch:
diff --git a/test/test.js b/test/test.js
index 12d049c..da0f768 100644
--- a/test/test.js
+++ b/test/test.js
@@ -139,6 +139,66 @@ describe('sw-precache core functionality', function() {
     });
   });
 
+  it('should produce the same output when stripPrefix doesn\'t match the file prefixes', function(done) {
+    var config = {
+      logger: NOOP,
+      staticFileGlobs: [
+        'test/data/one/a.txt',
+        'test/data/one/c.txt',
+        'test/data/two/b.txt'
+      ],
+      stripPrefix: '.'
+    };
+
+    var configPrime = {
+      logger: NOOP,
+      staticFileGlobs: [
+        'test/data/one/c.txt',
+        'test/data/two/b.txt',
+        'test/data/one/a.txt'
+      ]
+    };
+
+    generate(config, function(error, reponseString) {
+      assert.ifError(error);
+      generate(configPrime, function(error, responseStringPrime) {
+        assert.ifError(error);
+        assert.strictEqual(reponseString, responseStringPrime);
+        done();
+      });
+    });
+  });
+
+  it('should produce different output when stripPrefix matches the file prefixes', function(done) {
+    var config = {
+      logger: NOOP,
+      staticFileGlobs: [
+        'test/data/one/a.txt',
+        'test/data/one/c.txt',
+        'test/data/two/b.txt'
+      ],
+      stripPrefix: 'test'
+    };
+
+    var configPrime = {
+      logger: NOOP,
+      staticFileGlobs: [
+        'test/data/one/c.txt',
+        'test/data/two/b.txt',
+        'test/data/one/a.txt'
+      ]
+    };
+
+    generate(config, function(error, reponseString) {
+      assert.ifError(error);
+      generate(configPrime, function(error, responseStringPrime) {
+        assert.ifError(error);
+        assert.notStrictEqual(reponseString, responseStringPrime);
+        done();
+      });
+    });
+  });
+
   after(function() {
     fs.unlinkSync(TEMP_FILE);
   });