$.xhr will now deal with Last-Modified and If-Modified-Since headers itself.

This commit is contained in:
Mayhem 2013-08-11 18:02:22 +02:00
parent b2ec75510b
commit e0cb19ac53
3 changed files with 20 additions and 23 deletions

View File

@ -36,17 +36,23 @@ $.extend = (object, properties) ->
for key, val of properties for key, val of properties
object[key] = val object[key] = val
return return
$.ajax = (url, options, extra={}) -> $.ajax = do ->
{type, headers, upCallbacks, form, sync} = extra # Status Code 304: Not modified
r = new XMLHttpRequest() # With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses.
type or= form and 'post' or 'get' # This saves a lot of bandwidth and CPU time for both the users and the servers.
r.open type, url, !sync lastModified = {}
for key, val of headers (url, options, extra={}) ->
r.setRequestHeader key, val {type, whenModified, upCallbacks, form, sync} = extra
$.extend r, options r = new XMLHttpRequest()
$.extend r.upload, upCallbacks type or= form and 'post' or 'get'
r.send form r.open type, url, !sync
r if whenModified
r.setRequestHeader 'If-Modified-Since', lastModified[url] or '0'
$.on r, 'load', -> lastModified[url] = r.getResponseHeader 'Last-Modified'
$.extend r, options
$.extend r.upload, upCallbacks
r.send form
r
$.cache = do -> $.cache = do ->
reqs = {} reqs = {}
(url, cb, options) -> (url, cb, options) ->

View File

@ -8,7 +8,6 @@ ThreadStats =
@postCountEl = $ '#post-count', @dialog @postCountEl = $ '#post-count', @dialog
@fileCountEl = $ '#file-count', @dialog @fileCountEl = $ '#file-count', @dialog
@pageCountEl = $ '#page-count', @dialog @pageCountEl = $ '#page-count', @dialog
@lastModified = '0'
Thread::callbacks.push Thread::callbacks.push
name: 'Thread Stats' name: 'Thread Stats'
@ -41,9 +40,8 @@ ThreadStats =
return return
setTimeout ThreadStats.fetchPage, 2 * $.MINUTE setTimeout ThreadStats.fetchPage, 2 * $.MINUTE
$.ajax "//api.4chan.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad, $.ajax "//api.4chan.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad,
headers: 'If-Modified-Since': ThreadStats.lastModified whenModified: true
onThreadsLoad: -> onThreadsLoad: ->
ThreadStats.lastModified = @getResponseHeader 'Last-Modified'
return if @status isnt 200 return if @status isnt 200
pages = JSON.parse @response pages = JSON.parse @response
for page in pages for page in pages

View File

@ -25,7 +25,6 @@ ThreadUpdater =
ThreadUpdater.root = @OP.nodes.root.parentNode ThreadUpdater.root = @OP.nodes.root.parentNode
ThreadUpdater.lastPost = +ThreadUpdater.root.lastElementChild.id.match(/\d+/)[0] ThreadUpdater.lastPost = +ThreadUpdater.root.lastElementChild.id.match(/\d+/)[0]
ThreadUpdater.outdateCount = 0 ThreadUpdater.outdateCount = 0
ThreadUpdater.lastModified = '0'
for input in $$ 'input', ThreadUpdater.dialog for input in $$ 'input', ThreadUpdater.dialog
if input.type is 'checkbox' if input.type is 'checkbox'
@ -95,7 +94,6 @@ ThreadUpdater =
when 200 when 200
g.DEAD = false g.DEAD = false
ThreadUpdater.parse JSON.parse(req.response).posts ThreadUpdater.parse JSON.parse(req.response).posts
ThreadUpdater.lastModified = req.getResponseHeader 'Last-Modified'
ThreadUpdater.set 'timer', ThreadUpdater.getInterval() ThreadUpdater.set 'timer', ThreadUpdater.getInterval()
when 404 when 404
g.DEAD = true g.DEAD = true
@ -108,12 +106,7 @@ ThreadUpdater =
thread: ThreadUpdater.thread thread: ThreadUpdater.thread
else else
ThreadUpdater.outdateCount++ ThreadUpdater.outdateCount++
ThreadUpdater.set 'timer', ThreadUpdater.getInterval() ThreadUpdater.set 'timer', ThreadUpdater.getInterval()
###
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 and avoid unnecessary computation.
###
[text, klass] = if req.status is 304 [text, klass] = if req.status is 304
[null, null] [null, null]
else else
@ -159,7 +152,7 @@ ThreadUpdater =
ThreadUpdater.req.abort() ThreadUpdater.req.abort()
url = "//api.4chan.org/#{ThreadUpdater.thread.board}/res/#{ThreadUpdater.thread}.json" url = "//api.4chan.org/#{ThreadUpdater.thread.board}/res/#{ThreadUpdater.thread}.json"
ThreadUpdater.req = $.ajax url, onloadend: ThreadUpdater.cb.load, ThreadUpdater.req = $.ajax url, onloadend: ThreadUpdater.cb.load,
headers: 'If-Modified-Since': ThreadUpdater.lastModified whenModified: true
updateThreadStatus: (title, OP) -> updateThreadStatus: (title, OP) ->
titleLC = title.toLowerCase() titleLC = title.toLowerCase()