@@ -223,8 +223,23 @@ function pjax(options) {
223
223
}
224
224
225
225
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
+
226
234
var container = extractContainer ( data , xhr , options )
227
235
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
228
243
if ( ! container . contents ) {
229
244
locationReplace ( container . url )
230
245
return
@@ -676,6 +691,16 @@ function cachePop(direction, id, value) {
676
691
delete cacheMapping [ id ]
677
692
}
678
693
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
+
679
704
// Install pjax functions on $.pjax to enable pushState behavior.
680
705
//
681
706
// Does nothing if already enabled.
@@ -700,7 +725,8 @@ function enable() {
700
725
type : 'GET' ,
701
726
dataType : 'html' ,
702
727
scrollTo : 0 ,
703
- maxCacheLength : 20
728
+ maxCacheLength : 20 ,
729
+ version : findVersion
704
730
}
705
731
$ ( window ) . bind ( 'popstate.pjax' , onPjaxPopstate )
706
732
}
0 commit comments