Skip to content

Commit

Permalink
Added netlify header auto generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Dec 22, 2017
1 parent 7fb4b5f commit a4b684d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Strict-Transport-Security: max-age=31536000, includeSubDomains

## Far future expires for hashed file names
/static/*
Cache-Control: max-age=31536000
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"prebuildDocs": "rm -rf docs/_dist && node scripts/docs-update-toc.js",
"buildDocs": "bundle exec jekyll build --source ./docs --destination ./docs/_site --config ./docs/_config.yml --safe --drafts",
"postbuildDocs": "buildProduction docs/_site/index.html --outroot docs/_dist --canonicalroot https://mochajs.org/ --optimizeimages --inlinehtmlimage 10000 --asyncscripts ",
"postbuildDocs": "cp docs/_headers docs/_site/_headers && buildProduction docs/_site/index.html docs/_site/_headers --outroot docs/_dist --canonicalroot https://mochajs.org/ --optimizeimages --inlinehtmlimage 10000 --asyncscripts && cp docs/_headers docs/_dist/_headers && node scripts/netlify-headers.js >> docs/_dist/_headers",
"prewatchDocs": "node scripts/docs-update-toc.js",
"watchDocs": "bundle exec jekyll serve --source ./docs --destination ./docs/_site --config ./docs/_config.yml --safe --drafts --watch"
},
Expand Down
103 changes: 103 additions & 0 deletions scripts/netlify-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
var AssetGraph = require('assetgraph');

var headers = [
'Content-Security-Policy'
];

var resourceHintTypeMap = {
HtmlPreloadLink: 'preload',
HtmlPrefetchLink: 'prefetch',
HtmlPreconnectLink: 'preconnect',
HtmlDnsPrefetchLink: 'dns-prefetch'
};

function getHeaderForRelation (rel) {
let header = `Link: <${rel.href}>; rel=${resourceHintTypeMap[rel.type]}; as=${rel.as}; type=${rel.to.contentType}`;

if (rel.crossorigin || rel.as === 'font') {
header = `${header}; crossorigin=anonymous`;
}

return header;
}

new AssetGraph({ root: 'docs/_dist' })
.loadAssets('*.html')
.populate({
followRelations: { type: 'HtmlAnchor', crossorigin: false }
})
.queue(function (assetGraph) {
var assets = assetGraph.findAssets({ type: 'Html', isInline: false });

var headerMap = {};

assets.forEach(function (asset) {
var url = '/' + asset.url.replace(assetGraph.root, '').replace(/#.*/, '').replace('index.html', '');
if (!headerMap[url]) {
headerMap[url] = [];
}

headers.forEach(function (header) {
var node = asset.parseTree.querySelector('meta[http-equiv=' + header + ']');

if (node) {
headerMap[url].push(`${header}: ${node.getAttribute('content')}`)

node.parentNode.removeChild(node);
asset.markDirty();
}
});

var firstCssRel = asset.outgoingRelations.filter(r => {
return r.type === 'HtmlStyle'
&& r.crossorigin === false
&& r.href !== undefined;
})[0];

if (firstCssRel) {
const header = `Link: <${firstCssRel.href}>; rel=preload; as=style`;


headerMap[url].push(header);
}

var resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreloadLink', 'HtmlPrefetchLink'].includes(r.type));

resourceHintRelations.forEach(rel => {
headerMap[url].push(getHeaderForRelation(rel));

rel.detach();
});

var resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreconnectLink'].includes(r.type));

resourceHintRelations.forEach(rel => {
let header = `Link: <${rel.href}>; rel=preconnect`;

if (rel.crossorigin) {
header = `${header}; crossorigin=anonymous`;
}

headerMap[url].push(header);

rel.detach();
});
});

console.log('\n## Autogenerated headers:\n')

Object.keys(headerMap).forEach(function (url) {
console.log(url);

var httpHeaders = headerMap[url];

httpHeaders.forEach(function (header) {
console.log(` ${header}`)
});

console.log('');
});

})
.writeAssetsToDisc({ isLoaded: true })
.run();

0 comments on commit a4b684d

Please sign in to comment.