Added: cacheBust option (#71)
* Refactor request test to allow further tests to be added * Make cache-busting optional Closes #70
This commit was merged in pull request #71.
This commit is contained in:
committed by
Maxime Thirouin
parent
ee530f4c0a
commit
7976f06043
@@ -382,6 +382,12 @@ It's called every time a page is switched, even for history buttons.
|
|||||||
|
|
||||||
Value (in px) to scrollTo when a page is switched.
|
Value (in px) to scrollTo when a page is switched.
|
||||||
|
|
||||||
|
##### `cacheBust` (Boolean, default true)
|
||||||
|
|
||||||
|
When set to true,
|
||||||
|
append a timestamp query string segment to the requested URLs
|
||||||
|
in order to skip browser cache.
|
||||||
|
|
||||||
##### `debug` (Boolean, default to false)
|
##### `debug` (Boolean, default to false)
|
||||||
|
|
||||||
Enable verbose mode & doesn't use fallback when there is an error.
|
Enable verbose mode & doesn't use fallback when there is an error.
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ module.exports = function(options){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.options.scrollTo = (typeof this.options.scrollTo === 'undefined') ? 0 : this.options.scrollTo;
|
this.options.scrollTo = (typeof this.options.scrollTo === 'undefined') ? 0 : this.options.scrollTo;
|
||||||
|
this.options.cacheBust = (typeof this.options.cacheBust === 'undefined') ? true : this.options.cacheBust
|
||||||
this.options.debug = this.options.debug || false
|
this.options.debug = this.options.debug || false
|
||||||
|
|
||||||
// we can’t replace body.outerHTML or head.outerHTML
|
// we can’t replace body.outerHTML or head.outerHTML
|
||||||
|
|||||||
@@ -12,7 +12,12 @@ module.exports = function(location, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
request.open("GET", location + (!/[?&]/.test(location) ? "?" : "&") + (new Date().getTime()), true)
|
// Add a timestamp as part of the query string if cache busting is enabled
|
||||||
|
if (this.options.cacheBust) {
|
||||||
|
location += (!/[?&]/.test(location) ? "?" : "&") + new Date().getTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
request.open("GET", location, true)
|
||||||
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
|
request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
|
||||||
request.send(null)
|
request.send(null)
|
||||||
return request
|
return request
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ tape("test parse initalization options function", function(t) {
|
|||||||
t.deepEqual(typeof body_1.options.analytics,"function");
|
t.deepEqual(typeof body_1.options.analytics,"function");
|
||||||
|
|
||||||
t.deepEqual(body_1.options.scrollTo,0);
|
t.deepEqual(body_1.options.scrollTo,0);
|
||||||
|
t.deepEqual(body_1.options.cacheBust,true);
|
||||||
t.deepEqual(body_1.options.debug,false);
|
t.deepEqual(body_1.options.debug,false);
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,7 +3,14 @@ var tape = require("tape")
|
|||||||
var request = require("../../lib/request.js")
|
var request = require("../../lib/request.js")
|
||||||
|
|
||||||
tape("test xhr request", function(t) {
|
tape("test xhr request", function(t) {
|
||||||
request("https://api.github.com/", function(result) {
|
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
||||||
|
var requestCacheBust = request.bind({
|
||||||
|
options: {
|
||||||
|
cacheBust: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
var r = requestCacheBust("https://api.github.com/", function(result) {
|
||||||
|
t.equal(r.responseURL.indexOf("?"), 23, "XHR URL is cache-busted when configured to be")
|
||||||
try {
|
try {
|
||||||
result = JSON.parse(result)
|
result = JSON.parse(result)
|
||||||
}
|
}
|
||||||
@@ -14,3 +21,16 @@ tape("test xhr request", function(t) {
|
|||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
t.test("- request is not cache-busted when configured not to be", function(t) {
|
||||||
|
var requestNoCacheBust = request.bind({
|
||||||
|
options: {
|
||||||
|
cacheBust: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
var r = requestNoCacheBust("https://api.github.com/", function() {
|
||||||
|
t.equal(r.responseURL, "https://api.github.com/", "XHR URL is left untouched")
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user