Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Extended cache bust to custom checks #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ Offline.options = {checks: {image: {url: 'my-image.gif'}, active: 'image'}}
The one cavet is that with the image method, we can't distinguish a 404 from a genuine connection issue, so any error at all will
appear to Offline as a connection issue.

By default, Offline will add a 'cacheBust' parameter to the xhr or image URL to prevent the browser from caching the request.
You can disable this by setting the value to false:

```javascript
Offline.options = {checks: {xhr: {url: '/connection-test'}, cacheBust:false}};
```

Offline also includes a check called `'up'` and another called `'down'` which will always report being up or down respectively for
testing. You can activate them by setting the `active` option, adding a data attribute to your script tag with the name
`data-simulate` and value `'up'` or `'down'`, or by setting `localStorage.OFFLINE_SIMULATE` to `'up'` or `'down'`.
Expand Down
22 changes: 18 additions & 4 deletions coffee/offline.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ defaultOptions =
xhr:
url: ->
# This can be any endpoint, even one that will 404.
"/favicon.ico?_=#{ (new Date()).getTime() }"
"/favicon.ico"
timeout: 5000
type: 'HEAD'
image:
url: ->
# This can be any image, this is the better option if your image is on a different domain, otherwise just use XHR
"/favicon.ico?_=#{ (new Date()).getTime() }"
"/favicon.ico"

active: 'xhr'

cacheBust: true

checkOnLoad: false

interceptRequests: true
Expand All @@ -38,6 +40,7 @@ defaultOptions =

deDupBody: false


grab = (obj, key) ->
cur = obj
parts = key.split('.')
Expand Down Expand Up @@ -156,6 +159,16 @@ checkXHR = (xhr, onUp, onDown) ->

_onreadystatechange?(arguments...)

getURL = (url) ->
if Offline.getOption('checks.cacheBust')
bust = 'cacheBust' + '=' + (new Date).getTime()
if url.indexOf('?') > -1
bust = '&' + bust
else
bust = '?' + bust
url = url + bust
url

Offline.checks = {}
Offline.checks.xhr = ->
xhr = new XMLHttpRequest
Expand All @@ -164,7 +177,8 @@ Offline.checks.xhr = ->

# It doesn't matter what this hits, even a 404 is considered up. It is important however that
# it's on the same domain and port, so CORS issues don't come into play.
xhr.open(Offline.getOption('checks.xhr.type'), Offline.getOption('checks.xhr.url'), true)

xhr.open(Offline.getOption('checks.xhr.type'), getURL(Offline.getOption('checks.xhr.url')), true)

if xhr.timeout?
xhr.timeout = Offline.getOption('checks.xhr.timeout')
Expand All @@ -183,7 +197,7 @@ Offline.checks.image = ->
img = document.createElement 'img'
img.onerror = Offline.markDown
img.onload = Offline.markUp
img.src = Offline.getOption('checks.image.url')
img.src = getURL(Offline.getOption('checks.image.url'))

undefined

Expand Down
27 changes: 21 additions & 6 deletions js/offline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function() {
var Offline, checkXHR, defaultOptions, extendNative, grab, handlers, init;
var Offline, checkXHR, defaultOptions, extendNative, getURL, grab, handlers, init;

extendNative = function(to, from) {
var e, key, results, val;
Expand Down Expand Up @@ -29,17 +29,18 @@
checks: {
xhr: {
url: function() {
return "/favicon.ico?_=" + ((new Date()).getTime());
return "/favicon.ico";
},
timeout: 5000,
type: 'HEAD'
},
image: {
url: function() {
return "/favicon.ico?_=" + ((new Date()).getTime());
return "/favicon.ico";
}
},
active: 'xhr'
active: 'xhr',
cacheBust: true
},
checkOnLoad: false,
interceptRequests: true,
Expand Down Expand Up @@ -200,13 +201,27 @@
}
};

getURL = function(url) {
var bust;
if (Offline.getOption('checks.cacheBust')) {
bust = 'cacheBust' + '=' + (new Date).getTime();
if (url.indexOf('?') > -1) {
bust = '&' + bust;
} else {
bust = '?' + bust;
}
url = url + bust;
}
return url;
};

Offline.checks = {};

Offline.checks.xhr = function() {
var e, xhr;
xhr = new XMLHttpRequest;
xhr.offline = false;
xhr.open(Offline.getOption('checks.xhr.type'), Offline.getOption('checks.xhr.url'), true);
xhr.open(Offline.getOption('checks.xhr.type'), getURL(Offline.getOption('checks.xhr.url')), true);
if (xhr.timeout != null) {
xhr.timeout = Offline.getOption('checks.xhr.timeout');
}
Expand All @@ -225,7 +240,7 @@
img = document.createElement('img');
img.onerror = Offline.markDown;
img.onload = Offline.markUp;
img.src = Offline.getOption('checks.image.url');
img.src = getURL(Offline.getOption('checks.image.url'));
return void 0;
};

Expand Down
17 changes: 11 additions & 6 deletions offline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*! offline-js 0.7.14 */
(function() {
var Offline, checkXHR, defaultOptions, extendNative, grab, handlers, init;
var Offline, checkXHR, defaultOptions, extendNative, getURL, grab, handlers, init;
extendNative = function(to, from) {
var e, key, results, val;
results = [];
Expand All @@ -14,17 +14,18 @@
checks:{
xhr:{
url:function() {
return "/favicon.ico?_=" + new Date().getTime();
return "/favicon.ico";
},
timeout:5e3,
type:"HEAD"
},
image:{
url:function() {
return "/favicon.ico?_=" + new Date().getTime();
return "/favicon.ico";
}
},
active:"xhr"
active:"xhr",
cacheBust:!0
},
checkOnLoad:!1,
interceptRequests:!0,
Expand Down Expand Up @@ -86,9 +87,13 @@
}) :(_onreadystatechange = xhr.onreadystatechange, xhr.onreadystatechange = function() {
return 4 === xhr.readyState ? checkStatus() :0 === xhr.readyState && onDown(), "function" == typeof _onreadystatechange ? _onreadystatechange.apply(null, arguments) :void 0;
});
}, getURL = function(url) {
var bust;
return Offline.getOption("checks.cacheBust") && (bust = "cacheBust=" + new Date().getTime(),
bust = url.indexOf("?") > -1 ? "&" + bust :"?" + bust, url += bust), url;
}, Offline.checks = {}, Offline.checks.xhr = function() {
var e, xhr;
xhr = new XMLHttpRequest(), xhr.offline = !1, xhr.open(Offline.getOption("checks.xhr.type"), Offline.getOption("checks.xhr.url"), !0),
xhr = new XMLHttpRequest(), xhr.offline = !1, xhr.open(Offline.getOption("checks.xhr.type"), getURL(Offline.getOption("checks.xhr.url")), !0),
null != xhr.timeout && (xhr.timeout = Offline.getOption("checks.xhr.timeout")),
checkXHR(xhr, Offline.markUp, Offline.markDown);
try {
Expand All @@ -100,7 +105,7 @@
}, Offline.checks.image = function() {
var img;
return img = document.createElement("img"), img.onerror = Offline.markDown, img.onload = Offline.markUp,
void (img.src = Offline.getOption("checks.image.url"));
void (img.src = getURL(Offline.getOption("checks.image.url")));
}, Offline.checks.down = Offline.markDown, Offline.checks.up = Offline.markUp, Offline.check = function() {
return Offline.trigger("checking"), Offline.checks[Offline.getOption("checks.active")]();
}, Offline.confirmUp = Offline.confirmDown = Offline.check, Offline.onXHR = function(cb) {
Expand Down
Loading