-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,99 @@ | ||
var AssetGraph = require('assetgraph'); | ||
'use strict'; | ||
|
||
var headers = [ | ||
'Content-Security-Policy' | ||
const AssetGraph = require('assetgraph'); | ||
|
||
const headers = [ | ||
'Content-Security-Policy' | ||
]; | ||
|
||
var resourceHintTypeMap = { | ||
HtmlPreloadLink: 'preload', | ||
HtmlPrefetchLink: 'prefetch', | ||
HtmlPreconnectLink: 'preconnect', | ||
HtmlDnsPrefetchLink: 'dns-prefetch' | ||
const 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}`; | ||
let header = `Link: <${rel.href}>; rel=${resourceHintTypeMap[rel.type]}; as=${rel.as}; type=${rel.to.contentType}`; | ||
|
||
if (rel.as === 'font') { | ||
header = `${header}; crossorigin=anonymous`; | ||
} | ||
if (rel.as === 'font') { | ||
header = `${header}; crossorigin=anonymous`; | ||
} | ||
|
||
return header; | ||
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] = []; | ||
} | ||
.loadAssets('*.html') | ||
.populate({ | ||
followRelations: { type: 'HtmlAnchor', crossorigin: false } | ||
}) | ||
.queue(function (assetGraph) { | ||
const assets = assetGraph.findAssets({ type: 'Html', isInline: false }); | ||
|
||
headers.forEach(function (header) { | ||
var node = asset.parseTree.querySelector('meta[http-equiv=' + header + ']'); | ||
const headerMap = {}; | ||
|
||
if (node) { | ||
headerMap[url].push(`${header}: ${node.getAttribute('content')}`) | ||
assets.forEach(function (asset) { | ||
const url = '/' + asset.url.replace(assetGraph.root, '').replace(/#.*/, '').replace('index.html', ''); | ||
if (!headerMap[url]) { | ||
headerMap[url] = []; | ||
} | ||
|
||
node.parentNode.removeChild(node); | ||
asset.markDirty(); | ||
} | ||
}); | ||
headers.forEach(function (header) { | ||
const node = asset.parseTree.querySelector('meta[http-equiv=' + header + ']'); | ||
|
||
var firstCssRel = asset.outgoingRelations.filter(r => { | ||
return r.type === 'HtmlStyle' | ||
&& r.crossorigin === false | ||
&& r.href !== undefined; | ||
})[0]; | ||
if (node) { | ||
headerMap[url].push(`${header}: ${node.getAttribute('content')}`); | ||
|
||
if (firstCssRel) { | ||
const header = `Link: <${firstCssRel.href}>; rel=preload; as=style`; | ||
node.parentNode.removeChild(node); | ||
asset.markDirty(); | ||
} | ||
}); | ||
|
||
const firstCssRel = asset.outgoingRelations.filter(r => { | ||
return r.type === 'HtmlStyle' && | ||
r.crossorigin === false && | ||
r.href !== undefined; | ||
})[0]; | ||
|
||
headerMap[url].push(header); | ||
} | ||
if (firstCssRel) { | ||
const header = `Link: <${firstCssRel.href}>; rel=preload; as=style`; | ||
|
||
var resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreloadLink', 'HtmlPrefetchLink'].includes(r.type)); | ||
headerMap[url].push(header); | ||
} | ||
|
||
resourceHintRelations.forEach(rel => { | ||
headerMap[url].push(getHeaderForRelation(rel)); | ||
const resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreloadLink', 'HtmlPrefetchLink'].includes(r.type)); | ||
|
||
rel.detach(); | ||
}); | ||
resourceHintRelations.forEach(rel => { | ||
headerMap[url].push(getHeaderForRelation(rel)); | ||
|
||
var resourceHintRelations = asset.outgoingRelations.filter(r => ['HtmlPreconnectLink'].includes(r.type)); | ||
rel.detach(); | ||
}); | ||
|
||
resourceHintRelations.forEach(rel => { | ||
let header = `Link: <${rel.href}>; rel=preconnect`; | ||
const preconnectRelations = asset.outgoingRelations.filter(r => ['HtmlPreconnectLink'].includes(r.type)); | ||
|
||
headerMap[url].push(header); | ||
preconnectRelations.forEach(rel => { | ||
let header = `Link: <${rel.href}>; rel=preconnect`; | ||
|
||
rel.detach(); | ||
}); | ||
}); | ||
headerMap[url].push(header); | ||
|
||
console.log('\n## Autogenerated headers:\n') | ||
rel.detach(); | ||
}); | ||
}); | ||
|
||
Object.keys(headerMap).forEach(function (url) { | ||
console.log(url); | ||
console.log('\n## Autogenerated headers:\n'); | ||
|
||
var httpHeaders = headerMap[url]; | ||
Object.keys(headerMap).forEach(function (url) { | ||
console.log(url); | ||
|
||
httpHeaders.forEach(function (header) { | ||
console.log(` ${header}`) | ||
}); | ||
const httpHeaders = headerMap[url]; | ||
|
||
console.log(''); | ||
}); | ||
httpHeaders.forEach(function (header) { | ||
console.log(` ${header}`); | ||
}); | ||
|
||
}) | ||
.writeAssetsToDisc({ isLoaded: true }) | ||
.run(); | ||
console.log(''); | ||
}); | ||
}) | ||
.writeAssetsToDisc({ isLoaded: true }) | ||
.run(); |