From 574178cffc1b069499b50a4ccd9ff654c887c2a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 6 Sep 2016 11:56:54 +0200 Subject: [PATCH] Prevent accessing /nodes/undefined/view from search pages. `firstHit.attr('data-hit-id')` can be undefined; in that case we just ignore the siutation. Furthermore, I've removed the call to clearTimeout(), as it is only called after the timeout has been hit, and thus is a no-op. --- src/scripts/algolia_search.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/scripts/algolia_search.js b/src/scripts/algolia_search.js index 35a3cddd..9e1ff119 100644 --- a/src/scripts/algolia_search.js +++ b/src/scripts/algolia_search.js @@ -82,17 +82,24 @@ $(document).ready(function() { firstHit.addClass('active'); firstHit.find('#search-loading').addClass('active'); - var getNode = setTimeout(function() { - $.get('/nodes/' + firstHit.attr('data-hit-id') + '/view', function(dataHtml) { + function done() { + $('.search-loading').removeClass('active'); + $('#search-error').hide(); + $('#search-hit-container').show(); + } + + window.setTimeout(function() { + // Ignore getting that first result when there is none. + var hit_id = firstHit.attr('data-hit-id'); + if (hit_id === undefined) { + done(); + return; + } + + $.get('/nodes/' + hit_id + '/view', function(dataHtml) { $('#search-hit-container').html(dataHtml); }) - .done(function() { - $('.search-loading').removeClass('active'); - $('#search-error').hide(); - $('#search-hit-container').show(); - - clearTimeout(getNode); - }) + .done(done) .fail(function(data) { $('.search-loading').removeClass('active'); $('#search-hit-container').hide();