Send "If-Modified-Since" header, make use of the 304 status, always fool the cache, change $.ajax.

Status Code 304: Not modified
 By sending the "If-Modified-Since" header we get a proper status code, and no response.
 This saves bandwidth for both the user and the servers, avoid unnecessary computation,
 and won't load images and scripts when parsing the response.
This commit is contained in:
Nicolas Stepien 2011-12-13 20:22:24 +01:00
parent fbfdefa3e0
commit 0a6e01accd
3 changed files with 61 additions and 23 deletions

View File

@ -326,13 +326,13 @@
$.add(d.head, script); $.add(d.head, script);
return $.rm(script); return $.rm(script);
}, },
ajax: function(url, cb, type) { ajax: function(url, cb, type, event) {
var r; var r;
if (type == null) type = 'get'; if (type == null) type = 'get';
if (event == null) event = 'onload';
r = new XMLHttpRequest(); r = new XMLHttpRequest();
r.onload = cb; r[event] = cb;
r.open(type, url, true); r.open(type, url, true);
r.send();
return r; return r;
}, },
cache: function(url, cb) { cache: function(url, cb) {
@ -354,6 +354,7 @@
} }
return _results; return _results;
})); }));
req.send();
req.callbacks = [cb]; req.callbacks = [cb];
return $.cache.requests[url] = req; return $.cache.requests[url] = req;
} }
@ -541,7 +542,6 @@
this.regexps[key].push(RegExp(f[1], f[2])); this.regexps[key].push(RegExp(f[1], f[2]));
} catch (e) { } catch (e) {
alert(e.message); alert(e.message);
alert(e);
} }
} }
this.callbacks.push(this[key]); this.callbacks.push(this[key]);
@ -1988,7 +1988,8 @@
$.on(input, 'click', updater.update); $.on(input, 'click', updater.update);
} }
} }
return $.add(d.body, dialog); $.add(d.body, dialog);
return updater.lastModified = 0;
}, },
cb: { cb: {
verbose: function() { verbose: function() {
@ -2029,6 +2030,21 @@
return; return;
} }
updater.timer.textContent = '-' + conf['Interval']; updater.timer.textContent = '-' + conf['Interval'];
/*
Status Code 304: Not modified
By sending the `If-Modified-Since` header we get a proper status code, and no response.
This saves bandwidth for both the user and the servers, avoid unnecessary computation,
and won't load images and scripts when parsing the response.
*/
updater.lastModified = this.getResponseHeader('Last-Modified');
console.log(this.status);
if (this.status === 304) {
if (conf['Verbose']) {
updater.count.textContent = '+0';
updater.count.className = null;
}
return;
}
body = $.el('body', { body = $.el('body', {
innerHTML: this.responseText innerHTML: this.responseText
}); });
@ -2050,7 +2066,7 @@
if (conf['Verbose']) { if (conf['Verbose']) {
updater.count.textContent = '+' + newPosts; updater.count.textContent = '+' + newPosts;
if (newPosts === 0) { if (newPosts === 0) {
updater.count.className = ''; updater.count.className = null;
} else { } else {
updater.count.className = 'new'; updater.count.className = 'new';
} }
@ -2077,12 +2093,13 @@
return updater.update(); return updater.update();
}, },
update: function() { update: function() {
var cb, url, _ref; var url, _ref;
updater.timer.textContent = 0; updater.timer.textContent = 0;
if ((_ref = updater.request) != null) _ref.abort(); if ((_ref = updater.request) != null) _ref.abort();
url = engine !== 'presto' ? location.pathname : location.pathname + '?' + Date.now(); url = location.pathname + '?' + Date.now();
cb = updater.cb.update; updater.request = $.ajax(url, updater.cb.update);
return updater.request = $.ajax(url, cb); updater.request.setRequestHeader('If-Modified-Since', updater.lastModified);
return updater.request.send();
} }
}; };
@ -2980,12 +2997,12 @@
thumb = this.previousSibling; thumb = this.previousSibling;
imgExpand.contract(thumb); imgExpand.contract(thumb);
if (engine === 'webkit') { if (engine === 'webkit') {
req = $.ajax(this.src, null, 'head'); req = $.ajax(this.src, (function() {
return req.onreadystatechange = function() {
if (this.status !== 404) { if (this.status !== 404) {
return setTimeout(imgExpand.retry, 10000, thumb); return setTimeout(imgExpand.retry, 10000, thumb);
} }
}; }), 'head', 'onreadystatechange');
return req.send();
} else if (!g.dead) { } else if (!g.dead) {
return setTimeout(imgExpand.retry, 10000, thumb); return setTimeout(imgExpand.retry, 10000, thumb);
} }

View File

@ -1,5 +1,6 @@
master master
- mayhem - mayhem
thread updater network optimization
prevent regexp errors with the filter prevent regexp errors with the filter
2.32.3 2.32.3

View File

@ -231,11 +231,10 @@ $.extend $,
textContent: "(#{code})()" textContent: "(#{code})()"
$.add d.head, script $.add d.head, script
$.rm script $.rm script
ajax: (url, cb, type='get') -> ajax: (url, cb, type='get', event='onload') ->
r = new XMLHttpRequest() r = new XMLHttpRequest()
r.onload = cb r[event] = cb
r.open type, url, true r.open type, url, true
r.send()
r r
cache: (url, cb) -> cache: (url, cb) ->
if req = $.cache.requests[url] if req = $.cache.requests[url]
@ -245,6 +244,7 @@ $.extend $,
req.callbacks.push cb req.callbacks.push cb
else else
req = $.ajax url, (-> cb.call @ for cb in @callbacks) req = $.ajax url, (-> cb.call @ for cb in @callbacks)
req.send()
req.callbacks = [cb] req.callbacks = [cb]
$.cache.requests[url] = req $.cache.requests[url] = req
cb: cb:
@ -1587,6 +1587,8 @@ updater =
$.add d.body, dialog $.add d.body, dialog
updater.lastModified = 0
cb: cb:
verbose: -> verbose: ->
if conf['Verbose'] if conf['Verbose']
@ -1619,8 +1621,23 @@ updater =
updater.timer.textContent = '-' + conf['Interval'] updater.timer.textContent = '-' + conf['Interval']
###
Status Code 304: Not modified
By sending the `If-Modified-Since` header we get a proper status code, and no response.
This saves bandwidth for both the user and the servers, avoid unnecessary computation,
and won't load images and scripts when parsing the response.
###
updater.lastModified = @getResponseHeader('Last-Modified')
console.log @status
if @status is 304
if conf['Verbose']
updater.count.textContent = '+0'
updater.count.className = null
return
body = $.el 'body', body = $.el 'body',
innerHTML: @responseText innerHTML: @responseText
#this only works on Chrome because of cross origin policy #this only works on Chrome because of cross origin policy
if $('title', body).textContent is '4chan - Banned' if $('title', body).textContent is '4chan - Banned'
updater.count.textContent = 'banned' updater.count.textContent = 'banned'
@ -1639,7 +1656,7 @@ updater =
if conf['Verbose'] if conf['Verbose']
updater.count.textContent = '+' + newPosts updater.count.textContent = '+' + newPosts
if newPosts is 0 if newPosts is 0
updater.count.className = '' updater.count.className = null
else else
updater.count.className = 'new' updater.count.className = 'new'
@ -1666,10 +1683,11 @@ updater =
update: -> update: ->
updater.timer.textContent = 0 updater.timer.textContent = 0
updater.request?.abort() updater.request?.abort()
#Opera needs to fool its cache #fool the cache
url = if engine isnt 'presto' then location.pathname else location.pathname + '?' + Date.now() url = location.pathname + '?' + Date.now()
cb = updater.cb.update updater.request = $.ajax url, updater.cb.update
updater.request = $.ajax url, cb updater.request.setRequestHeader 'If-Modified-Since', updater.lastModified
updater.request.send()
watcher = watcher =
init: -> init: ->
@ -2302,8 +2320,10 @@ imgExpand =
imgExpand.contract thumb imgExpand.contract thumb
#navigator.online is not x-browser/os yet #navigator.online is not x-browser/os yet
if engine is 'webkit' if engine is 'webkit'
req = $.ajax @src, null, 'head' req = $.ajax @src, (->
req.onreadystatechange = -> setTimeout imgExpand.retry, 10000, thumb if @status isnt 404 setTimeout imgExpand.retry, 10000, thumb if @status isnt 404
), 'head', 'onreadystatechange'
req.send()
#Firefox returns a status code of 0 because of the same origin policy #Firefox returns a status code of 0 because of the same origin policy
#Oprah doesn't send any request #Oprah doesn't send any request
else unless g.dead else unless g.dead