Skip to content

Commit 469040a

Browse files
committed
Delay cachePush and pushState until XHR success
1 parent c7915b7 commit 469040a

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

jquery.pjax.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function pjax(options) {
244244

245245
var allowed = fire('pjax:error', [xhr, textStatus, errorThrown, options])
246246
if (options.type == 'GET' && textStatus !== 'abort' && allowed) {
247-
locationReplace(container.url)
247+
window.location.assign(container.url)
248248
}
249249
}
250250

@@ -269,16 +269,20 @@ function pjax(options) {
269269

270270
// If there is a layout version mismatch, hard load the new url
271271
if (currentVersion && latestVersion && currentVersion !== latestVersion) {
272-
locationReplace(container.url)
272+
window.location.assign(container.url)
273273
return
274274
}
275275

276276
// If the new response is missing a body, hard load the page
277277
if (!container.contents) {
278-
locationReplace(container.url)
278+
window.location.assign(container.url)
279279
return
280280
}
281281

282+
if (options.push) {
283+
cachePush(pjax.state.id, cloneContents(context))
284+
}
285+
282286
pjax.state = {
283287
id: options.id || uniqueId(),
284288
url: container.url,
@@ -288,7 +292,9 @@ function pjax(options) {
288292
timeout: options.timeout
289293
}
290294

291-
if (options.push || options.replace) {
295+
if (options.push) {
296+
window.history.pushState(pjax.state, container.title, container.url)
297+
} else if (options.replace) {
292298
window.history.replaceState(pjax.state, container.title, container.url)
293299
}
294300

@@ -355,13 +361,6 @@ function pjax(options) {
355361
var xhr = pjax.xhr = $.ajax(options)
356362

357363
if (xhr.readyState > 0) {
358-
if (options.push && !options.replace) {
359-
// Cache current container element before replacing it
360-
cachePush(pjax.state.id, cloneContents(context))
361-
362-
window.history.pushState(null, "", options.requestUrl)
363-
}
364-
365364
fire('pjax:start', [xhr, options])
366365
fire('pjax:send', [xhr, options])
367366
}

0 commit comments

Comments
 (0)