Skip to content

Commit

Permalink
Try HTTP HEAD before falling back to HTTP GET for external assets. Al…
Browse files Browse the repository at this point in the history
…so send some accept headers to improve odds of webserver responding. Refs #117
  • Loading branch information
Munter committed May 13, 2017
1 parent 631eaba commit 3e26dfa
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,17 @@ module.exports = function (options) {
}
}

function httpStatus(url, relations) {
function httpStatus(url, relations, shouldUseGetMethod) {
return function (callback) {
request({
method: shouldUseGetMethod ? 'get' : 'head',
url: url.replace(/#.*$/, ''),
strictSSL: true,
gzip: true,
headers: {
'User-Agent': 'Hyperlink v' + version + ' (https://www.npmjs.com/package/hyperlink)'
'User-Agent': 'Hyperlink v' + version + ' (https://www.npmjs.com/package/hyperlink)',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch, br'
}
}, function (error, res) {
var status,
Expand All @@ -178,6 +181,10 @@ module.exports = function (options) {

callback(undefined, status);
} else {
if (res.statusCode === 405) {
return httpStatus(url, relations, true);
}

status = res.statusCode;
redirects = res.request.redirects || [];
var firstRedirectStatus = redirects[0] && redirects[0].statusCode;
Expand Down

0 comments on commit 3e26dfa

Please sign in to comment.