Add an option to set a timeout for XHR requests (#95)

Closes #24.
This commit was merged in pull request #95.
This commit is contained in:
BehindTheMath
2018-01-07 23:56:11 -05:00
committed by GitHub
parent d3d5ef7a11
commit 6491e32437
4 changed files with 12 additions and 1 deletions

View File

@@ -392,6 +392,10 @@ Useful to debug page layout differences.
When set to true, clicking on a link that point the current url trigger a full page reload. When set to true, clicking on a link that point the current url trigger a full page reload.
##### `timeout` (Integer, default to 0)
The timeout in milliseconds for the XHR requests. Set to 0 to disable the timeout.
### Events ### Events
Pjax fires a number of events regardless of how its invoked. Pjax fires a number of events regardless of how its invoked.

View File

@@ -158,6 +158,7 @@ Pjax.prototype = {
trigger(document, "pjax:send", options); trigger(document, "pjax:send", options);
// Do the request // Do the request
options.requestOptions.timeout = this.options.timeout
this.doRequest(href, options.requestOptions, function(html) { this.doRequest(href, options.requestOptions, function(html) {
// Fail if unable to load HTML via AJAX // Fail if unable to load HTML via AJAX
if (html === false) { if (html === false) {

View File

@@ -23,6 +23,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.cacheBust = (typeof this.options.cacheBust === 'undefined') ? true : this.options.cacheBust
this.options.debug = this.options.debug || false this.options.debug = this.options.debug || false
this.options.timeout = this.options.timeout || 0
// we cant replace body.outerHTML or head.outerHTML // we cant replace body.outerHTML or head.outerHTML
// it create a bug where new body or new head are created in the dom // it create a bug where new body or new head are created in the dom
@@ -37,4 +38,4 @@ module.exports = function(options){
if (typeof options.analytics !== "function") { if (typeof options.analytics !== "function") {
options.analytics = function() {} options.analytics = function() {}
} }
} }

View File

@@ -15,12 +15,17 @@ module.exports = function(location, options, callback) {
} }
} }
request.ontimeout = function() {
callback(null, request)
}
// Add a timestamp as part of the query string if cache busting is enabled // Add a timestamp as part of the query string if cache busting is enabled
if (this.options.cacheBust) { if (this.options.cacheBust) {
location += (!/[?&]/.test(location) ? "?" : "&") + new Date().getTime() location += (!/[?&]/.test(location) ? "?" : "&") + new Date().getTime()
} }
request.open(requestMethod.toUpperCase(), location, true) request.open(requestMethod.toUpperCase(), location, true)
request.timeout = options.timeout
request.setRequestHeader("X-Requested-With", "XMLHttpRequest") request.setRequestHeader("X-Requested-With", "XMLHttpRequest")
request.setRequestHeader("X-PJAX", "true") request.setRequestHeader("X-PJAX", "true")