@@ -358,6 +358,18 @@ function locationReplace(url) {
358
358
window . location . replace ( url )
359
359
}
360
360
361
+
362
+ var initialPop = true
363
+ var initialURL = window . location . href
364
+ var initialState = window . history . state
365
+
366
+ // Initialize $.pjax.state if possible
367
+ // Happens when reloading a page and coming forward from a different
368
+ // session history.
369
+ if ( initialState && initialState . container ) {
370
+ pjax . state = initialState
371
+ }
372
+
361
373
// popstate handler takes care of the back and forward buttons
362
374
//
363
375
// You probably shouldn't use pjax on pages with other pushState
@@ -366,23 +378,28 @@ function onPjaxPopstate(event) {
366
378
var state = event . state
367
379
368
380
if ( state && state . container ) {
381
+ // When coming forward from a seperate history session, will get an
382
+ // initial pop with a state we are already at. Skip reloading the current
383
+ // page.
384
+ if ( initialPop && initialURL == state . url ) return
385
+
369
386
var container = $ ( state . container )
370
387
if ( container . length ) {
371
- var contents = cacheMapping [ state . id ]
388
+ var direction , contents = cacheMapping [ state . id ]
372
389
373
390
if ( pjax . state ) {
374
391
// Since state ids always increase, we can deduce the history
375
392
// direction from the previous state.
376
- var direction = pjax . state . id < state . id ? 'forward' : 'back'
393
+ direction = pjax . state . id < state . id ? 'forward' : 'back'
377
394
378
395
// Cache current container before replacement and inform the
379
396
// cache which direction the history shifted.
380
397
cachePop ( direction , pjax . state . id , container . clone ( ) . contents ( ) )
381
398
} else {
382
- // Page was reloaded but we have an existing history entry.
383
- // Set it to our initial state.
384
- pjax . state = state ;
385
- return ;
399
+ // Unknown case: If you happen to hit it, please open an issue on
400
+ // https://github.com/defunkt/jquery-pjax/issues?sort=updated&state=open
401
+ // with steps to reproduce.
402
+ throw "missing initial pjax state"
386
403
}
387
404
388
405
var popstateEvent = $ . Event ( 'pjax:popstate' , {
@@ -420,6 +437,7 @@ function onPjaxPopstate(event) {
420
437
locationReplace ( location . href )
421
438
}
422
439
}
440
+ initialPop = false
423
441
}
424
442
425
443
// Fallback version of main pjax function for browsers that don't
0 commit comments