From 8339a40acd960d4c6700c788c8550889a854fc65 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 19 Nov 2013 11:15:48 -0500 Subject: [PATCH] Prevent stale avatars from showing in new messages. When you upload a 2nd avatar to Zulip, the URL doesn't actually change, so even new messages can show the old avatar, if your browser is caching. We work against the cache by having the "stamp" argument, which we vary at reload time and also when we upload the new avatar. The browser still benefits from cached images as new messages come in. (imported from commit 84869c8d7f251c9f2498026a5e9e3b2451784879) --- static/js/settings.js | 9 ++++++--- static/js/ui.js | 5 ++--- tools/jslint/check-all.js | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/static/js/settings.js b/static/js/settings.js index 8ebee0ac66..5998e710cc 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -20,9 +20,11 @@ function is_local_part(value, element) { return this.optional(element) || /^[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+(\.[\-!#$%&'*+\/=?\^_`{}|~0-9A-Z]+)*$/i.test(value); } +// Choose avatar stamp fairly randomly, to help get old avatars out of cache. +exports.avatar_stamp = Math.floor(Math.random()*100); + $(function () { - var avatar_stamp = 1; function upload_avatar(file_input) { var form_data = new FormData(); @@ -44,9 +46,9 @@ $(function () { contentType: false, success: function (data) { util.destroy_loading_indicator($("#upload_avatar_spinner")); - var url = data.avatar_url + '&stamp=' + avatar_stamp; + var url = data.avatar_url + '&stamp=' + exports.avatar_stamp; $("#user-settings-avatar").expectOne().attr("src", url); - avatar_stamp += 1; + exports.avatar_stamp += 1; } }); @@ -274,4 +276,5 @@ $(function () { }); +return exports; }()); diff --git a/static/js/ui.js b/static/js/ui.js index 6302bb726c..36c2081347 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -25,7 +25,6 @@ exports.home_tab_obscured = function () { // scrollbar when we switch to a new tab (and restore it // when we switch back.) var scroll_positions = {}; -var gravatar_stamp = 1; exports.change_tab_to = function (tabname) { $('#gear-menu a[href="' + tabname + '"]').tab('show'); @@ -690,7 +689,7 @@ function toggle_star(row_id) { exports.small_avatar_url = function (message) { // Try to call this function in all places where we need 25px - // gravatar images, so that the browser can help + // avatar images, so that the browser can help // us avoid unnecessary network trips. (For user-uploaded avatars, // the s=25 parameter is essentially ignored, but it's harmless.) // @@ -699,7 +698,7 @@ exports.small_avatar_url = function (message) { if (message.avatar_url) { var url = message.avatar_url + "&s=50"; if (message.sent_by_me) { - url += "&stamp=" + gravatar_stamp; + url += "&stamp=" + settings.avatar_stamp; } return url; } else { diff --git a/tools/jslint/check-all.js b/tools/jslint/check-all.js index b4bf9ba883..10db957175 100644 --- a/tools/jslint/check-all.js +++ b/tools/jslint/check-all.js @@ -25,7 +25,7 @@ var globals = + ' compose compose_fade rows hotkeys narrow reload notifications_bar search subs' + ' composebox_typeahead typeahead_helper notifications hashchange' + ' invite ui util activity timerender MessageList MessageListView blueslip unread stream_list' - + ' message_edit tab_bar emoji popovers navigate message_tour' + + ' message_edit tab_bar emoji popovers navigate message_tour settings' + ' avatar feature_flags search_suggestion referral stream_color Dict' + ' Filter summary admin stream_data muting WinChan muting_ui Socket'