Skip to content

Commit

Permalink
Fix breaking of query strings when moving assets around
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Oct 20, 2019
1 parent 3ce16e3 commit e5cf247
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/hashfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ module.exports = async function hashfiles(assetGraph, options = {}) {
}
});
}
return baseUrl + asset.fileName;

return `${baseUrl}${asset.fileName}${asset.url.replace(
/^[^#?]*(?:)/,
''
)}`;
}
);

Expand Down
34 changes: 30 additions & 4 deletions test/hashfiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('hashfiles', () => {
type: { $in: ['Html', 'Atom', 'Ico'] }
});

const hasshedAssets = graph
const hashedAssets = graph
.findAssets({
isInline: false,
isLoaded: true,
Expand All @@ -159,7 +159,7 @@ describe('hashfiles', () => {
{ path: '/', fileName: 'feed.xml' }
]);

expect(hasshedAssets, 'to satisfy', [
expect(hashedAssets, 'to satisfy', [
{ path: '/static/', fileName: 'main.d35efae1d2.css' },
{ path: '/static/', fileName: 'syntax.8a0ad6441f.css' },
{ path: '/static/', fileName: '152.6f37a55225.png' },
Expand All @@ -181,6 +181,32 @@ describe('hashfiles', () => {
{ path: '/static/', fileName: 'social-email.487543e65c.svg' },
{ path: '/static/', fileName: 'social-feed.ad4bea7819.svg' }
]);

expect(unhashedAssets[0], 'to satisfy', {
text: expect
.it('to contain', 'main.d35efae1d2.css')
.and('to contain', 'syntax.8a0ad6441f.css')
});
});

it('should retain query strings on hashed assets', async () => {
const graph = await getPopulatedGraph('querystring', ['index.html']);

await hashFiles(graph);

expect(graph.findAssets()[0], 'to satisfy', {
fileName: 'index.html',
outgoingRelations: [
{
href: 'static/main.6bc5650065.css?foo=bar',
to: {
path: '/static/',
fileName: 'main.6bc5650065.css',
query: { foo: 'bar' }
}
}
]
});
});
});

Expand Down Expand Up @@ -208,7 +234,7 @@ describe('hashfiles', () => {
type: { $in: ['Html', 'Atom', 'Ico'] }
});

const hasshedAssets = graph
const hashedAssets = graph
.findAssets({
isInline: false,
isLoaded: true,
Expand All @@ -222,7 +248,7 @@ describe('hashfiles', () => {
{ path: '/', fileName: 'feed.xml' }
]);

expect(hasshedAssets, 'to satisfy', [
expect(hashedAssets, 'to satisfy', [
{ path: '/__mydir/', fileName: 'main.d35efae1d2.css' },
{ path: '/__mydir/', fileName: 'syntax.8a0ad6441f.css' },
{ path: '/__mydir/', fileName: '152.6f37a55225.png' },
Expand Down
16 changes: 16 additions & 0 deletions testdata/querystring/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="main.css?foo=bar">
</head>

<body>

</body>

</html>
1 change: 1 addition & 0 deletions testdata/querystring/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* empty */

0 comments on commit e5cf247

Please # to comment.