npm install --save handlebars-helper-sri
Attach the helper to handlebars
:
var handlebars = require("handlebars");
handlebars = require("handlebars-helper-sri").register(handlebars);
Then, call the helper from your Handlebars template:
<script src="/script.js" integrity="{{sri "./public/script.js"}}"></script>
<link href="/style.css" integrity="{{sri "./public/style.css"}}" rel="stylesheet">
You may also select custom properties from subresource
:
<!-- select subresource("./public/script.js")["hashes"]["sha256"] -->
<script src="/script.js?{{sri "./public/script.js" "hashes" "sha256"}}"></script>
You may wish to attach a custom subresource instance, for pre-load caching:
var handlebars = require("handlebars");
var subresource = require("subresource");
[
"../public/script.js",
"../public/style.css"
].forEach(subresource);
handlebars = require("handlebars-helper-sri").register(handlebars, subresource);
Warning: The first instance attachment will persist as long as node is running (even if you require("handlebars")
again). Handlebars.registerHelper
appears to mutate the require
cache (potential Node bug). As such, you can only ever use one instance of Handlebars.