Skip to content

Commit

Permalink
Don't move files to the canonical root.
Browse files Browse the repository at this point in the history
Fixes bug introduced in 879b4a1

mochajs/mocha#3412 (comment)
  • Loading branch information
papandreou committed Jun 10, 2018
1 parent 048c29b commit 1f49f38
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 40 deletions.
3 changes: 1 addition & 2 deletions lib/transforms/buildProduction.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ module.exports = function(options) {
await assetGraph.moveAssetsInOrder(
moveAssetsInOrderQuery,
(asset, assetGraph) => {
let baseUrl =
(assetGraph.canonicalRoot || assetGraph.root) + 'static/';
let baseUrl = assetGraph.root + 'static/';
// Conservatively assume that all JavaScriptStaticUrl relations pointing at non-images are intended to be fetched via XHR
// and thus cannot be put on a CDN because of same origin restrictions:
const hasIncomingJavaScriptStaticUrlOrServiceWorkerRelations =
Expand Down
100 changes: 62 additions & 38 deletions test/transforms/buildProduction.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,49 +1014,73 @@ describe('buildProduction', function() {
);
});

it('should handle a test case with an RSS feed (#118)', async function() {
const assetGraph = new AssetGraph({
root: __dirname + '/../../testdata/transforms/buildProduction/rss/',
canonicalRoot: 'http://www.someexamplerssdomain.com/'
});
await assetGraph.loadAssets('index.html');
await assetGraph.populate();
describe('with the canonicalRoot option', function() {
it('should handle a test case with an RSS feed (#118)', async function() {
const assetGraph = new AssetGraph({
root: __dirname + '/../../testdata/transforms/buildProduction/rss/',
canonicalRoot: 'http://www.someexamplerssdomain.com/'
});
await assetGraph.loadAssets('index.html');
await assetGraph.populate();

expect(assetGraph, 'to contain asset', {
contentType: 'application/rss+xml',
type: 'Rss'
});
expect(assetGraph, 'to contain asset', {
contentType: 'application/rss+xml',
type: 'Rss'
});

await assetGraph.buildProduction({
version: false
await assetGraph.buildProduction({
version: false
});

expect(assetGraph.findRelations(), 'to satisfy', [
{
type: 'HtmlAlternateLink',
href: 'rssFeed.xml',
canonical: false
},
{
type: 'RssChannelLink',
href: 'index.html',
canonical: false
},
{
type: 'XmlHtmlInlineFragment'
},
{
type: 'HtmlImage',
canonical: true,
href: 'http://www.someexamplerssdomain.com/static/foo.d65dd5318f.png'
}
]);

expect(
assetGraph.findAssets({ type: 'Rss' })[0].text,
'to equal',
'<?xml version="1.0" encoding="UTF-8"?>\n<rss version="2.0">\n<channel>\n <title>RSS Title</title>\n <description>This is an example of an RSS feed</description>\n <link>index.html</link>\n <lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>\n <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>\n <ttl>1800</ttl>\n <item>\n <title>Example entry</title>\n <description>Here is some text containing an interesting description and an image: &lt;img src=http://www.someexamplerssdomain.com/static/foo.d65dd5318f.png>.</description>\n <link>http://www.wikipedia.org/</link>\n <guid>unique string per item</guid>\n <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>\n </item>\n</channel>\n</rss>'
);
});

expect(assetGraph.findRelations(), 'to satisfy', [
{
type: 'HtmlAlternateLink',
href: 'rssFeed.xml',
canonical: false
},
{
type: 'RssChannelLink',
href: 'index.html',
canonical: false
},
{
type: 'XmlHtmlInlineFragment'
},
{
type: 'HtmlImage',
canonical: true,
href: 'http://www.someexamplerssdomain.com/static/foo.d65dd5318f.png'
}
]);
// https://github.com/mochajs/mocha/pull/3412#issuecomment-396038075
it('should keep static files within assetGraph.root', async function() {
const assetGraph = new AssetGraph({
root:
__dirname +
'/../../testdata/transforms/buildProduction/canonicalRoot/',
canonicalRoot: 'http://www.example.com/'
});
await assetGraph.loadAssets('index.html');
await assetGraph.populate();

expect(
assetGraph.findAssets({ type: 'Rss' })[0].text,
'to equal',
'<?xml version="1.0" encoding="UTF-8"?>\n<rss version="2.0">\n<channel>\n <title>RSS Title</title>\n <description>This is an example of an RSS feed</description>\n <link>index.html</link>\n <lastBuildDate>Mon, 06 Sep 2010 00:01:00 +0000 </lastBuildDate>\n <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>\n <ttl>1800</ttl>\n <item>\n <title>Example entry</title>\n <description>Here is some text containing an interesting description and an image: &lt;img src=http://www.someexamplerssdomain.com/static/foo.d65dd5318f.png>.</description>\n <link>http://www.wikipedia.org/</link>\n <guid>unique string per item</guid>\n <pubDate>Mon, 06 Sep 2009 16:20:00 +0000 </pubDate>\n </item>\n</channel>\n</rss>'
);
await assetGraph.buildProduction({
version: false,
inlineByRelationType: {}
});

expect(assetGraph, 'to contain asset', {
type: 'JavaScript',
url: `${assetGraph.root}static/script.7e514b97a9.js`
});
});
});

it('should keep identical inline styles in svg files inlined', async function() {
Expand Down
7 changes: 7 additions & 0 deletions testdata/transforms/buildProduction/canonicalRoot/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<script src="script.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('script');

0 comments on commit 1f49f38

Please sign in to comment.