From bd9cccffcee44cecdd2790e523645f87ec61270c Mon Sep 17 00:00:00 2001 From: Jason Michalski Date: Wed, 29 Jan 2014 16:14:00 -0500 Subject: [PATCH] Use pushState where supported to change the URL hash This this removed one forced relayout of the page on unnarrow. This saves about 100ms for me. (imported from commit 0755f425abbe3d99b8a99765549a5bbf3c620b9a) --- static/js/hashchange.js | 16 +++++++++++++--- zerver/tests/frontend/tests/04-subscriptions.js | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/static/js/hashchange.js b/static/js/hashchange.js index 41956e19ad..19a08e0b3c 100644 --- a/static/js/hashchange.js +++ b/static/js/hashchange.js @@ -19,13 +19,23 @@ function decodeHashComponent(str) { return decodeURIComponent(str.replace(/\./g, '%')); } +function set_hash(hash) { + if (history.pushState) { + console.log(window.location); + // Build a full URL to not have same origin problems + var url = window.location.origin + window.location.pathname + hash; + history.pushState(null, null, url); + } else { + window.location.hash = hash; + } +} + exports.changehash = function (newhash) { if (changing_hash) { return; } $(document).trigger($.Event('hashchange.zulip')); - expected_hash = newhash; - window.location.hash = newhash; + set_hash(newhash); util.reset_favicon(); }; @@ -115,7 +125,7 @@ function do_hashchange(from_reload) { if (operators === undefined) { // If the narrow URL didn't parse, clear // window.location.hash and send them to the home tab - window.location.hash = ''; + set_hash(''); activate_home_tab(); return false; } diff --git a/zerver/tests/frontend/tests/04-subscriptions.js b/zerver/tests/frontend/tests/04-subscriptions.js index 9ae3b59ad0..ccc2fbe397 100644 --- a/zerver/tests/frontend/tests/04-subscriptions.js +++ b/zerver/tests/frontend/tests/04-subscriptions.js @@ -45,7 +45,7 @@ casper.then(function () { // Test the inline subscribe and unsubscribe in messages casper.then(function () { casper.click('a[href^="#"]'); - casper.test.assertUrlMatch(/^http:\/\/[^\/]+\/#$/, 'URL suggests we are on home page'); + casper.test.assertExists('#home.tab-pane.active', 'home page is active'); }); // Test an inline subscribe button for an unsubscribed stream