diff --git a/src/General/Index.coffee b/src/General/Index.coffee index 8b614384e..90ade98ae 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -594,11 +594,11 @@ Index = location.reload() return - Index.req = $.ajax "#{location.protocol}//a.4cdn.org/#{g.BOARD}/catalog.json", - onloadend: Index.load - , - whenModified: 'Index' - bypassCache: true + Index.req = $.whenModified( + Site.urls.catalogJSON({boardID: g.BOARD.ID}), + 'Index', + Index.load + ) $.addClass Index.button, 'fa-spin' load: -> diff --git a/src/Monitoring/ThreadStats.coffee b/src/Monitoring/ThreadStats.coffee index ca210334d..014cd8a67 100644 --- a/src/Monitoring/ThreadStats.coffee +++ b/src/Monitoring/ThreadStats.coffee @@ -75,9 +75,11 @@ ThreadStats = $.addClass ThreadStats.pageCountEl, 'warning' return ThreadStats.timeout = setTimeout ThreadStats.fetchPage, 2 * $.MINUTE - $.ajax "#{location.protocol}//a.4cdn.org/#{ThreadStats.thread.board}/threads.json", onload: ThreadStats.onThreadsLoad, - whenModified: 'ThreadStats' - bypassCache: true + $.whenModified( + Site.urls.threadsListJSON({boardID: ThreadStats.thread.board}), + 'ThreadStats', + ThreadStats.onThreadsLoad + ) onThreadsLoad: -> if @status is 200 diff --git a/src/Monitoring/ThreadUpdater.coffee b/src/Monitoring/ThreadUpdater.coffee index c44851170..c3151569c 100644 --- a/src/Monitoring/ThreadUpdater.coffee +++ b/src/Monitoring/ThreadUpdater.coffee @@ -231,12 +231,12 @@ ThreadUpdater = clearTimeout ThreadUpdater.timeoutID ThreadUpdater.set 'timer', '...', 'loading' ThreadUpdater.req?.abort() - ThreadUpdater.req = $.ajax "#{location.protocol}//a.4cdn.org/#{ThreadUpdater.thread.board}/thread/#{ThreadUpdater.thread}.json", - onloadend: ThreadUpdater.cb.load - timeout: $.MINUTE - , - whenModified: 'ThreadUpdater' - bypassCache: true + ThreadUpdater.req = $.whenModified( + Site.urls.threadJSON({boardID: ThreadUpdater.thread.board.ID, threadID: ThreadUpdater.thread.ID}), + 'ThreadUpdater', + ThreadUpdater.cb.load, + {timeout: $.MINUTE} + ) updateThreadStatus: (type, status) -> return if not (hasChanged = ThreadUpdater.thread["is#{type}"] isnt status) diff --git a/src/Monitoring/ThreadWatcher.coffee b/src/Monitoring/ThreadWatcher.coffee index 0f72de536..da6d04a4a 100644 --- a/src/Monitoring/ThreadWatcher.coffee +++ b/src/Monitoring/ThreadWatcher.coffee @@ -174,7 +174,6 @@ ThreadWatcher = if ThreadWatcher.requests.length is 0 ThreadWatcher.status.textContent = '...' $.addClass ThreadWatcher.refreshButton, 'fa-spin' - ajax = if (siteID is Site.hostname) then $.ajax else CrossOrigin.ajax onloadend = -> return if @finished @finished = true @@ -184,11 +183,19 @@ ThreadWatcher = else ThreadWatcher.status.textContent = "#{Math.round(ThreadWatcher.fetched / ThreadWatcher.requests.length * 100)}%" cb.apply @, args - req = ajax url, - onloadend: onloadend - timeout: $.MINUTE - , - whenModified: if force then false else 'ThreadWatcher' + if siteID is Site.hostname + if force + delete $.lastModified.ThreadWatcher?[url] + req = $.whenModified( + url, + 'ThreadWatcher', + onloadend, + {timeout: $.MINUTE} + ) + else + req = CrossOrigin.ajax url, + onloadend: onloadend + timeout: $.MINUTE ThreadWatcher.requests.push req clearRequests: -> diff --git a/src/platform/$.coffee b/src/platform/$.coffee index 24ddde8c6..8e7772478 100644 --- a/src/platform/$.coffee +++ b/src/platform/$.coffee @@ -41,34 +41,22 @@ $.extend = (object, properties) -> return $.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 = {} if window.wrappedJSObject and not XMLHttpRequest.wrappedJSObject pageXHR = XPCNativeWrapper window.wrappedJSObject.XMLHttpRequest else pageXHR = XMLHttpRequest (url, options={}, extra={}) -> - {type, whenModified, bypassCache, upCallbacks, form} = extra + {type, upCallbacks, form, headers} = extra options.responseType ?= 'json' # XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310 url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?4c(?:ha|d)n\.org)\/adv\//, '$1//adv/' - if whenModified - params = [] - # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=643659 - params.push "s=#{whenModified}" if $.engine is 'blink' - params.push "t=#{Date.now()}" if Site.software is 'yotsuba' and bypassCache - url0 = url - url += '?' + params.join('&') if params.length r = new pageXHR() type or= form and 'post' or 'get' try r.open type, url, true - if whenModified - r.setRequestHeader 'If-Modified-Since', lastModified[whenModified][url0] if lastModified[whenModified]?[url0]? - $.on r, 'load', -> (lastModified[whenModified] or= {})[url0] = r.getResponseHeader 'Last-Modified' + for key, value of (headers or {}) + r.setRequestHeader key, value $.extend r, options $.extend r.upload, upCallbacks # connection error or content blocker @@ -89,6 +77,29 @@ $.ajax = do -> $.queueTask $.event, event, null, r r +# 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 = {} +$.whenModified = (url, bucket, cb, options={}) -> + {timeout} = options + params = [] + # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=643659 + params.push "s=#{bucket}" if $.engine is 'blink' + params.push "t=#{Date.now()}" if Site.software is 'yotsuba' + url0 = url + url += '?' + params.join('&') if params.length + headers = {} + if (t = $.lastModified[bucket]?[url0])? + headers['If-Modified-Since'] = t + r = $.ajax url, { + onloadend: -> + ($.lastModified[bucket] or= {})[url0] = @getResponseHeader('Last-Modified') + cb.call @ + timeout + }, {headers} + r + do -> reqs = {} $.cache = (url, cb, options={}) ->