Skip to content

Commit 2e3d59e

Browse files
danielgrovesXhmikosR
authored andcommitted
Fix handling of a URL which has a non-hash component in the place of a hash (#37)
* Fix handling of a URL which has a non-hash component in the place of a hash * Refactor to reuse the position of the hash in the file name
1 parent f7d4cad commit 2e3d59e

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ const staticify = (root, options) => {
9292

9393
const fileName = path.basename(p);
9494
const fileNameParts = fileName.split('.');
95+
const fileNameHashPosition = fileNameParts.length - 2;
96+
const fileNameHash = fileNameParts[fileNameHashPosition];
9597
const re = new RegExp(`^[0-9a-f]{${HASH_LEN}}$`, 'i');
98+
const reResult = re.exec(fileNameHash);
9699

97-
if (fileNameParts.length >= 3 &&
98-
fileNameParts[fileNameParts.length - 2].length === HASH_LEN &&
99-
re.exec(fileNameParts[fileNameParts.length - 2])[0] === fileNameParts[fileNameParts.length - 2]
100+
if (fileNameParts.length >= 3 && fileNameHash.length === HASH_LEN &&
101+
(reResult && reResult[0] === fileNameHash)
100102
) {
101-
const stripped = fileNameParts.slice(0, fileNameParts.length - 2);
103+
const stripped = fileNameParts.slice(0, fileNameHashPosition);
102104

103105
stripped.push(fileNameParts[fileNameParts.length - 1]);
104106

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ describe('.stripVersion', () => {
3232
staticify(ROOT).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js'));
3333
});
3434

35+
it('should not fail when the path contains a 7 character string that is not a hash', () => {
36+
staticify(ROOT).stripVersion(path.normalize('/script.abcdefg.html')).should.equal(path.normalize('/script.abcdefg.html'));
37+
});
38+
3539
it('should strip the (long) hash from a path when necessary', () => {
3640
staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.4e2502b01a4c92b0a51b1a5a3271eab6.js')).should.equal(path.normalize('/script.js'));
3741
staticify(ROOT, {shortHash: false}).stripVersion(path.normalize('/script.js')).should.equal(path.normalize('/script.js'));
3842
});
43+
44+
it('should not fail when the path contains a 32 character string that is not a hash', () => {
45+
staticify(ROOT).stripVersion(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html')).should.equal(path.normalize('/script.abcdefgabcdefgabcdefgabcdefgabcd.html'));
46+
});
3947
});
4048

4149
describe('.getVersionedPath', () => {

0 commit comments

Comments
 (0)