Skip to content

Commit 2da9e0e

Browse files
committed
Delay calling pushState() until the xhr succeeds. Fixes #319
1 parent 9930024 commit 2da9e0e

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

jquery.pjax.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function pjax(options) {
221221

222222
var allowed = fire('pjax:error', [xhr, textStatus, errorThrown, options])
223223
if (options.type == 'GET' && textStatus !== 'abort' && allowed) {
224-
locationReplace(container.url)
224+
hardLoad(container.url)
225225
}
226226
}
227227

@@ -238,16 +238,17 @@ function pjax(options) {
238238

239239
// If there is a layout version mismatch, hard load the new url
240240
if (currentVersion && latestVersion && currentVersion !== latestVersion) {
241-
locationReplace(container.url)
241+
hardLoad(container.url)
242242
return
243243
}
244244

245245
// If the new response is missing a body, hard load the page
246246
if (!container.contents) {
247-
locationReplace(container.url)
247+
hardLoad(container.url)
248248
return
249249
}
250250

251+
// Update browser location and history
251252
pjax.state = {
252253
id: options.id || uniqueId(),
253254
url: container.url,
@@ -257,7 +258,9 @@ function pjax(options) {
257258
timeout: options.timeout
258259
}
259260

260-
if (options.push || options.replace) {
261+
if (options.push) {
262+
window.history.pushState(pjax.state, container.title, container.url)
263+
} else if (options.replace) {
261264
window.history.replaceState(pjax.state, container.title, container.url)
262265
}
263266

@@ -335,8 +338,6 @@ function pjax(options) {
335338
if (options.push && !options.replace) {
336339
// Cache current container element before replacing it
337340
cachePush(pjax.state.id, context.clone().contents())
338-
339-
window.history.pushState(null, "", stripPjaxParam(options.requestUrl))
340341
}
341342

342343
fire('pjax:start', [xhr, options])
@@ -360,15 +361,20 @@ function pjaxReload(container, options) {
360361
return pjax($.extend(defaults, optionsFor(container, options)))
361362
}
362363

363-
// Internal: Hard replace current state with url.
364-
//
365-
// Work for around WebKit
366-
// https://bugs.webkit.org/show_bug.cgi?id=93506
364+
// Internal: Hard load a URL, optionally replacing the current
365+
// history instead of adding a new history item. Used to load
366+
// fallbacks, reload layout on version changes, etc.
367367
//
368368
// Returns nothing.
369-
function locationReplace(url) {
370-
window.history.replaceState(null, "", "#")
371-
window.location.replace(url)
369+
function hardLoad(url, replace) {
370+
if (!!replace) {
371+
// Work for around WebKit
372+
// https://bugs.webkit.org/show_bug.cgi?id=93506
373+
window.history.replaceState(null, "", "#")
374+
window.location.replace(url)
375+
} else{
376+
window.location.assign(url)
377+
}
372378
}
373379

374380

@@ -451,7 +457,7 @@ function onPjaxPopstate(event) {
451457
// scroll position.
452458
container[0].offsetHeight
453459
} else {
454-
locationReplace(location.href)
460+
hardLoad(location.href, true)
455461
}
456462
}
457463
initialPop = false

test/app.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ def title(str)
8383
erb :boom, :layout => !pjax?
8484
end
8585

86+
get '/referer_timeout.html' do
87+
if pjax?
88+
sleep 1
89+
erb :referer, :layout => false
90+
else
91+
erb :referer
92+
end
93+
end
94+
8695
get '/:page.html' do
8796
erb :"#{params[:page]}", :layout => !pjax?
8897
end

test/unit/pjax.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,6 @@ if ($.support.pjax) {
264264
data: { foo: 1, bar: 2 },
265265
container: "#main"
266266
})
267-
268-
// URL is set immediately
269-
equal(frame.location.pathname, "/env.html")
270-
equal(frame.location.search, "?foo=1&bar=2")
271267
})
272268

273269
asyncTest("GET data is merged into query string", function() {
@@ -287,10 +283,6 @@ if ($.support.pjax) {
287283
data: { bar: 2 },
288284
container: "#main"
289285
})
290-
291-
// URL is set immediately
292-
equal(frame.location.pathname, "/env.html")
293-
equal(frame.location.search, "?foo=1&bar=2")
294286
})
295287

296288

@@ -695,7 +687,7 @@ if ($.support.pjax) {
695687
var frame = this.frame
696688

697689
frame.$("#main").on("pjax:complete", function() {
698-
equal(frame.location.pathname, "/boom.html")
690+
equal(frame.location.pathname, "/home.html")
699691
start()
700692
})
701693
frame.$("#main").on("pjax:error", function(event, xhr) {

test/unit/pjax_fallback.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ asyncTest("sends correct HTTP referer"+s, function() {
9999
})
100100
})
101101

102+
asyncTest("sends correct HTTP referer after failed request"+s, function() {
103+
var frame = this.frame
104+
105+
$('iframe')[0].onload = function() {
106+
var referer = frame.document.getElementById("referer").textContent
107+
ok(referer.match("/home.html"), referer)
108+
start()
109+
}
110+
111+
frame.$.pjax({
112+
url: "/referer_timeout.html",
113+
container: "#main"
114+
})
115+
})
116+
102117
asyncTest("adds entry to browser history"+s, function() {
103118
var frame = this.frame
104119
var count = 0

0 commit comments

Comments
 (0)