Skip to content

Commit

Permalink
[links] prevent redirect loop by mobile-canonical.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nleush committed Aug 21, 2015
1 parent 6bbf09f commit 3164afb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,8 @@
redirect = urlLib.resolve(uri, redirect);
}
options.redirectsCount = (options.redirectsCount || 0) + 1;
options.redirectsHistory = options.redirectsHistory || [];
options.redirectsHistory.push(uri);
run(redirect, options, cb);
aborted = true;
return;
Expand Down
20 changes: 13 additions & 7 deletions plugins/links/mobile-canonical.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
module.exports = {

getLink: function(url, meta, cb) {
getLink: function(url, meta, options, cb) {

var canonical = meta.canonical && meta.canonical.href || meta.canonical;

if (!canonical || url == canonical || !url.match(/^https?:\/\/(m|mobile)\./i)) {
cb(null);
// Redirect to canonical from mobile url.
if (canonical && url !== canonical && url.match(/^https?:\/\/(m|mobile)\./i)) {

// Do not redirect to url from redirects history.
if (!options.redirectsHistory || options.redirectsHistory.indexOf(canonical) === -1) {

return cb({
redirect: canonical
});
}
}

cb({
redirect: canonical
});

cb(null);
}

// ex. http://mobile.abc.net.au/news/2014-10-22/government-wants-rooftop-solar-program-to-continue/5833664
Expand Down

0 comments on commit 3164afb

Please sign in to comment.