Skip to content

Commit 3b7a4a3

Browse files
committed
Fallback to hard load when version header changes
1 parent ff6e817 commit 3b7a4a3

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

jquery.pjax.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,23 @@ function pjax(options) {
223223
}
224224

225225
options.success = function(data, status, xhr) {
226+
// If $.pjax.defaults.version is a function, invoke it first.
227+
// Otherwise it can be a static string.
228+
var currentVersion = (typeof $.pjax.defaults.version === 'function') ?
229+
$.pjax.defaults.version() :
230+
$.pjax.defaults.version
231+
232+
var latestVersion = xhr.getResponseHeader('X-PJAX-Version')
233+
226234
var container = extractContainer(data, xhr, options)
227235

236+
// If there is a layout version mismatch, hard load the new url
237+
if (currentVersion && latestVersion && currentVersion !== latestVersion) {
238+
locationReplace(container.url)
239+
return
240+
}
241+
242+
// If the new response is missing a body, hard load the page
228243
if (!container.contents) {
229244
locationReplace(container.url)
230245
return
@@ -676,6 +691,16 @@ function cachePop(direction, id, value) {
676691
delete cacheMapping[id]
677692
}
678693

694+
// Public: Find version identifier for the initial page load.
695+
//
696+
// Returns String version or undefined.
697+
function findVersion() {
698+
return $('meta').filter(function() {
699+
var name = $(this).attr('http-equiv')
700+
return name && name.toUpperCase() === 'X-PJAX-VERSION'
701+
}).attr('content')
702+
}
703+
679704
// Install pjax functions on $.pjax to enable pushState behavior.
680705
//
681706
// Does nothing if already enabled.
@@ -700,7 +725,8 @@ function enable() {
700725
type: 'GET',
701726
dataType: 'html',
702727
scrollTo: 0,
703-
maxCacheLength: 20
728+
maxCacheLength: 20,
729+
version: findVersion
704730
}
705731
$(window).bind('popstate.pjax', onPjaxPopstate)
706732
}

test/app.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def title(str)
2222
after do
2323
if pjax?
2424
response.headers['X-PJAX-URL'] = request.url
25+
response.headers['X-PJAX-Version'] = 'v1'
2526
end
2627
end
2728

test/unit/pjax.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ if ($.support.pjax) {
400400
}
401401
})
402402

403+
asyncTest("header version mismatch does a full load", function() {
404+
var frame = this.frame
405+
406+
frame.$.pjax.defaults.version = 'v2'
407+
408+
frame.$.pjax({
409+
url: "hello.html",
410+
container: "#main"
411+
})
412+
413+
this.iframe.onload = function() {
414+
equal(frame.$("#main p").html(), "Hello!")
415+
equal(frame.location.pathname, "/hello.html")
416+
start()
417+
}
418+
})
419+
403420

404421
asyncTest("triggers pjax:start event from container", function() {
405422
var frame = this.frame

0 commit comments

Comments
 (0)