$.xhr will now deal with Last-Modified and If-Modified-Since headers itself.
This commit is contained in:
parent
b2ec75510b
commit
e0cb19ac53
28
lib/$.coffee
28
lib/$.coffee
@ -36,17 +36,23 @@ $.extend = (object, properties) ->
|
||||
for key, val of properties
|
||||
object[key] = val
|
||||
return
|
||||
$.ajax = (url, options, extra={}) ->
|
||||
{type, headers, upCallbacks, form, sync} = extra
|
||||
r = new XMLHttpRequest()
|
||||
type or= form and 'post' or 'get'
|
||||
r.open type, url, !sync
|
||||
for key, val of headers
|
||||
r.setRequestHeader key, val
|
||||
$.extend r, options
|
||||
$.extend r.upload, upCallbacks
|
||||
r.send form
|
||||
r
|
||||
$.ajax = do ->
|
||||
# Status Code 304: Not modified
|
||||
# With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses.
|
||||
# This saves a lot of bandwidth and CPU time for both the users and the servers.
|
||||
lastModified = {}
|
||||
(url, options, extra={}) ->
|
||||
{type, whenModified, upCallbacks, form, sync} = extra
|
||||
r = new XMLHttpRequest()
|
||||
type or= form and 'post' or 'get'
|
||||
r.open type, url, !sync
|
||||
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 ->
|
||||
reqs = {}
|
||||
(url, cb, options) ->
|
||||
|
||||
@ -8,7 +8,6 @@ ThreadStats =
|
||||
@postCountEl = $ '#post-count', @dialog
|
||||
@fileCountEl = $ '#file-count', @dialog
|
||||
@pageCountEl = $ '#page-count', @dialog
|
||||
@lastModified = '0'
|
||||
|
||||
Thread::callbacks.push
|
||||
name: 'Thread Stats'
|
||||
@ -41,9 +40,8 @@ ThreadStats =
|
||||
return
|
||||
setTimeout ThreadStats.fetchPage, 2 * $.MINUTE
|
||||
$.ajax "//api.4chan.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad,
|
||||
headers: 'If-Modified-Since': ThreadStats.lastModified
|
||||
whenModified: true
|
||||
onThreadsLoad: ->
|
||||
ThreadStats.lastModified = @getResponseHeader 'Last-Modified'
|
||||
return if @status isnt 200
|
||||
pages = JSON.parse @response
|
||||
for page in pages
|
||||
|
||||
@ -25,7 +25,6 @@ ThreadUpdater =
|
||||
ThreadUpdater.root = @OP.nodes.root.parentNode
|
||||
ThreadUpdater.lastPost = +ThreadUpdater.root.lastElementChild.id.match(/\d+/)[0]
|
||||
ThreadUpdater.outdateCount = 0
|
||||
ThreadUpdater.lastModified = '0'
|
||||
|
||||
for input in $$ 'input', ThreadUpdater.dialog
|
||||
if input.type is 'checkbox'
|
||||
@ -95,7 +94,6 @@ ThreadUpdater =
|
||||
when 200
|
||||
g.DEAD = false
|
||||
ThreadUpdater.parse JSON.parse(req.response).posts
|
||||
ThreadUpdater.lastModified = req.getResponseHeader 'Last-Modified'
|
||||
ThreadUpdater.set 'timer', ThreadUpdater.getInterval()
|
||||
when 404
|
||||
g.DEAD = true
|
||||
@ -108,12 +106,7 @@ ThreadUpdater =
|
||||
thread: ThreadUpdater.thread
|
||||
else
|
||||
ThreadUpdater.outdateCount++
|
||||
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.
|
||||
###
|
||||
ThreadUpdater.set 'timer', ThreadUpdater.getInterval()
|
||||
[text, klass] = if req.status is 304
|
||||
[null, null]
|
||||
else
|
||||
@ -159,7 +152,7 @@ ThreadUpdater =
|
||||
ThreadUpdater.req.abort()
|
||||
url = "//api.4chan.org/#{ThreadUpdater.thread.board}/res/#{ThreadUpdater.thread}.json"
|
||||
ThreadUpdater.req = $.ajax url, onloadend: ThreadUpdater.cb.load,
|
||||
headers: 'If-Modified-Since': ThreadUpdater.lastModified
|
||||
whenModified: true
|
||||
|
||||
updateThreadStatus: (title, OP) ->
|
||||
titleLC = title.toLowerCase()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user