Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hitting back button sometimes breaks and doesn't load page? #356

Open
Lippiun opened this issue Feb 8, 2018 · 3 comments
Open

Hitting back button sometimes breaks and doesn't load page? #356

Lippiun opened this issue Feb 8, 2018 · 3 comments

Comments

@Lippiun
Copy link

Lippiun commented Feb 8, 2018

Hello,

For some reason, every now and again when I navigate through the pages using the back and forward buttons the back button stops working.

The URL changes but the content remains the same?

I've checked the console for errors and there is this:
TypeError: Cannot read property 'state' of undefined

Do you know why this is happening? Like I said, the majority of the time it works okay but all of sudden it doesn't.

The code I'm using is like so:

$(function(){
  'use strict';
  var options = {
    prefetch: true,
	debug:true,
    cacheLength: 0,
	repeatDelay: 500,
	
	
    onStart: {
      duration: 0, // Duration of our animation
      render: function ($container) {
        // Add your CSS animation reversing class
        $container.addClass('is-exiting');

        // Restart your animation
        smoothState.restartCSSAnimations();
      }
    },
	
	onProgress: {
    // How long this animation takes
    duration: 0,
    // A function that dictates the animations that take place
    render: function ($container) {
					$container.addClass('is-loading');
										
					$('#progressBar').append('<div id="bar"></div>');	
					var progress = '100%';
						
					$('#bar').animate({
						width: progress
					}, 400);
    }
	},
    
    onReady: {
      duration: 0,
      render: function ($container, $newContent) {
        $container.removeClass('is-loading is-exiting');
        // Inject the new content
        $container.html($newContent);
      },
    },
	
	onAfter: function() {
			navbarAnimate();
			closeMenu();
			ImageSliders();
			initPhotoSwipeFromDOM('.gallery');
			ImageOverlay(); 
			window.parsePinBtns();
			backToTop();

	}
  },
  
  
  smoothState = $('#main').smoothState(options).data('smoothState');
});

@Eugene1984
Copy link

I have exactly the same issue on all my sites using smoothstate.

@Lippiun
Copy link
Author

Lippiun commented Feb 12, 2018

Yeah it looks like a common problem but no one seems to have the answer! Hopefully the developer will explain

@Luc45
Copy link

Luc45 commented Apr 30, 2019

Increase your cache.

Replace cacheLength: 0 with something like cacheLength: 10 or something.

The issue comes from this function:

    /** Handles the popstate event, like when the user hits 'back' */
    onPopState = function ( e ) {
        if(e.state !== null) {
            var url = window.location.href,
                $page = $('#' + e.state.id),
                page = $page.data('smoothState'),
                diffUrl = (page.href !== url && !utility.isHash(url, page.href)),
                diffState = (e.state !== page.cache[page.href].state);

            if(diffUrl || diffState) {
                if (diffState) {
                    page.clear(page.href);
                }
                page.load(url, false);
            }
        }
    },

As you can see, it compares the state of given object with the one in the page cache, in e.state !== page.cache[page.href].state.

If the page you are currently viewing is not saved in the cache, it will throw an undefined index at that line.

Worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants