Skip to content

Commit

Permalink
Change the root of the graph if all entry points get redirected to an…
Browse files Browse the repository at this point in the history
…other origin
  • Loading branch information
papandreou committed Jun 6, 2020
1 parent bf15531 commit 2f1f76e
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 52 deletions.
18 changes: 18 additions & 0 deletions lib/subfont.js
Expand Up @@ -136,6 +136,8 @@ module.exports = async function subfont(

await assetGraph.checkIncompatibleTypes();

const entrypointAssets = assetGraph.findAssets({ isInitial: true });
const redirectOrigins = new Set();
for (const relation of assetGraph
.findRelations({ type: 'HttpRedirect' })
.sort((a, b) => a.id - b.id)) {
Expand All @@ -145,6 +147,22 @@ module.exports = async function subfont(
);
relation.to.isInitial = true;
relation.from.isInitial = false;

redirectOrigins.add(relation.to.origin);
}
}
if (
entrypointAssets.length === redirectOrigins.size &&
redirectOrigins.size === 1
) {
const newRoot = `${[...redirectOrigins][0]}/`;
if (newRoot !== assetGraph.root) {
assetGraph.info(
new Error(
`All entrypoints redirected, changing root from ${assetGraph.root} to ${newRoot}`
)
);
assetGraph.root = newRoot;
}
}

Expand Down
211 changes: 159 additions & 52 deletions test/subfont.js
Expand Up @@ -218,25 +218,25 @@ describe('subfont', function () {
});

describe('when fetching an entry point results in an HTTP redirect', function () {
it('should issue a warning', async function () {
httpception([
{
request: 'GET http://example.com/',
response: {
statusCode: 301,
headers: {
Location: 'https://somewhereelse.com/',
describe('with a single entry point', function () {
beforeEach(function () {
httpception([
{
request: 'GET http://example.com/',
response: {
statusCode: 301,
headers: {
Location: 'https://somewhereelse.com/',
},
},
},
},
{
request: 'GET https://somewhereelse.com/',
response: {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
body: `
<!DOCTYPE html>
{
request: 'GET https://somewhereelse.com/',
response: {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
body: `<!DOCTYPE html>
<html>
<head>
Expand All @@ -256,49 +256,156 @@ describe('subfont', function () {
</body>
</html>
`,
},
},
},
{
request: 'GET http://somewhereelse.com/OpenSans.woff',
response: {
headers: {
'Content-Type': 'font/woff',
{
request: 'GET http://somewhereelse.com/OpenSans.woff',
response: {
headers: {
'Content-Type': 'font/woff',
},
body: openSansBold,
},
body: openSansBold,
},
},
]);
]);
});

const root = 'http://example.com/';
sinon.stub(AssetGraph.prototype, 'info');
it('should issue a warning', async function () {
const root = 'http://example.com/';
sinon.stub(AssetGraph.prototype, 'info');

const assetGraph = await subfont(
{
root,
inputFiles: [root],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
);

const assetGraph = await subfont(
{
root,
inputFiles: [root],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
);
const htmlAssets = assetGraph.findAssets({
isInitial: true,
type: 'Html',
});
expect(htmlAssets, 'to have length', 1);
expect(
htmlAssets[0].url,
'to equal',
'https://somewhereelse.com/index.html'
);
expect(assetGraph.info, 'to have a call satisfying', () => {
assetGraph.info(
new Error(
'http://example.com/ redirected to https://somewhereelse.com/'
)
);
});
});

it('should change the root of the graph so that files get written to disc', async function () {
const root = 'http://example.com/';

sinon.stub(AssetGraph.prototype, 'info');
const assetGraph = await subfont(
{
root,
inputFiles: [root],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
);

expect(assetGraph.root, 'to equal', 'https://somewhereelse.com/');

const htmlAssets = assetGraph.findAssets({
isInitial: true,
type: 'Html',
expect(assetGraph.info, 'to have a call satisfying', () => {
assetGraph.info(
new Error(
'All entrypoints redirected, changing root from http://example.com/ to https://somewhereelse.com/'
)
);
});
});
expect(htmlAssets, 'to have length', 1);
expect(
htmlAssets[0].url,
'to equal',
'https://somewhereelse.com/index.html'
);
expect(assetGraph.info, 'to have a call satisfying', () => {
assetGraph.info(
new Error(
'http://example.com/ redirected to https://somewhereelse.com/'
)
});

describe('but other entry points do not get redirected', function () {
beforeEach(function () {
httpception([
{
request: 'GET http://example.com/',
response: {
statusCode: 301,
headers: {
Location: 'https://somewhereelse.com/',
},
},
},
{
request: 'GET http://example.com/page2',
response: {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
body: `<!DOCTYPE html><html></html>`,
},
},
{
request: 'GET https://somewhereelse.com/',
response: {
headers: {
'Content-Type': 'text/html; charset=utf-8',
},
body: `<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: Open Sans;
src: url(OpenSans.woff) format('woff');
}
div {
font-family: Open Sans;
}
</style>
</head>
<body>
<div>Hello</div>
</body>
</html>
`,
},
},
{
request: 'GET http://somewhereelse.com/OpenSans.woff',
response: {
headers: {
'Content-Type': 'font/woff',
},
body: openSansBold,
},
},
]);
});

it('should not change the root', async function () {
const root = 'http://example.com/';

const assetGraph = await subfont(
{
root,
inputFiles: [root, `${root}page2`],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
);

expect(assetGraph.root, 'to equal', 'http://example.com/');
});
});
});
Expand Down

0 comments on commit 2f1f76e

Please sign in to comment.