Skip to content

Commit 72cf603

Browse files
committed
Delay calling pushState() until the xhr succeeds. Fixes #319
1 parent 67fbdce commit 72cf603

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
@@ -222,7 +222,7 @@ function pjax(options) {
222222

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

@@ -239,16 +239,17 @@ function pjax(options) {
239239

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

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

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

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

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

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

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

375381

@@ -452,7 +458,7 @@ function onPjaxPopstate(event) {
452458
// scroll position.
453459
container[0].offsetHeight
454460
} else {
455-
locationReplace(location.href)
461+
hardLoad(location.href, true)
456462
}
457463
}
458464
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)