Skip to content

Commit

Permalink
feat: support optional preload attributes
Browse files Browse the repository at this point in the history
Support adding additional attributes to preload Link headers, to signal
to downstream servers if the resources should be converted to HTTP/2
Server Push.

The Preload specification defines "nopush" to signal that the downstream
server should not push the resources.[0] Fastly also supports
"x-http2-push-only"[1], other downstreams may implement other
attributes.

[0]: https://www.w3.org/TR/preload/#server-push-http-2
[1]: https://docs.fastly.com/guides/performance-tuning/http2-server-push

[[email protected]: added commit message]
Signed-off-by: [email protected]
  • Loading branch information
SamyPesse authored and terinjokes committed Feb 6, 2018
1 parent e1a09af commit 7900bd5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ express()

Object passed straight to [`lru-cache`][lru-cache]. It is highly recommended to set `cache.max` to an integer.

* **attributes**: `Array<String>`

List of custom attributes that should be added to the Preload Link headers.

## License

[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://www.tldrlegal.com/l/mit) see `LICENSE.md`.
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ module.exports = function netjet(options) {
scripts: true,
styles: true,
cache: {},
attributes: [],
});

var cache = new LRU(options.cache);
var attributes = [''].concat(options.attributes).join('; ');

return function netjetMiddleware(req, res, next) {
function appendHeader(field, value) {
Expand Down Expand Up @@ -60,7 +62,8 @@ module.exports = function netjet(options) {
(addBaseHref ? baseTag[0] : '') +
encodeRFC5987(unescape(url)) +
'>; rel=preload; as=' +
asType
asType +
attributes
);
});
}
Expand Down
29 changes: 29 additions & 0 deletions test/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,35 @@ describe('preload', function() {
});
});
});

describe('attributes', function() {
it('should add a custom attribute', function(done) {
var server = createServer({
attributes: ['nopush'],
});

request(server)
.get('/404')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect('Link', '</images/droids.png>; rel=preload; as=image; nopush')
.expect(404, done);
});

it('should add the custom attributes', function(done) {
var server = createServer({
attributes: ['nopush', 'x-http2-push-only'],
});

request(server)
.get('/404')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(
'Link',
'</images/droids.png>; rel=preload; as=image; nopush; x-http2-push-only'
)
.expect(404, done);
});
});
});

function createServer(options) {
Expand Down

0 comments on commit 7900bd5

Please sign in to comment.