page.js icon indicating copy to clipboard operation
page.js copied to clipboard

support history.state, so page reloads preserve state

Open monsanto opened this issue 10 years ago • 9 comments

monsanto avatar Feb 26 '14 22:02 monsanto

I don't got it. Why? mb pushState option can resolve the problem? #90

A avatar Jul 01 '14 22:07 A

Er, you closed this issue without checking that you understood it first? :(

Right now, when you start routing with page.js, the first handler will be passed an empty context, because page.start passes null as state to page.replace, which blasts the current history.state with an unconditional ctx.save() (https://github.com/visionmedia/page.js/blob/master/index.js#L99)

An alternate approach would be to pass history.state instead of null. history.state is preserved across refreshes, meaning that your web app doesn't lose context if you decide to reload the page. Another approach is to have initialState in the options dictionary. Anything but passing unconditionally passing null. Since page.start is the only routine that sets up event binding, you cannot avoid it, which means at the moment you cannot avoid history.state being destroyed without a hack.

monsanto avatar Jul 02 '14 03:07 monsanto

All fine, we can reopen it if we need.

A avatar Jul 02 '14 07:07 A

Yes, I think it's a good idea. I like the option to pass history.state to page.start(). But I need a some time to understand what to do with the idea.

A avatar Jul 02 '14 07:07 A

I needed something similar, to allow the user browse back to initial the start page (base).

Added to page.start:

if (!options.state) options.state = null;
   [...]
page.replace(url, options.state, true, dispatch);

and calling page.start with:

page.start({
  state: { "optionalvalue": "here" }
});

or just use an empty object state: {}

supermensa avatar Jan 30 '15 22:01 supermensa

+1 this issue. I have hacked the start method to pass history.state

page.start = function(options) {
    options = options || {};
    if (running) return;
    running = true;
    if (false === options.dispatch) dispatch = false;
    if (false === options.decodeURLComponents) decodeURLComponents = false;
    if (false !== options.popstate) window.addEventListener('popstate', onpopstate, false);
    if (false !== options.click) window.addEventListener('click', onclick, false);
    if (true === options.hashbang) hashbang = true;
    if (!dispatch) return;
    var url = (hashbang && ~location.hash.indexOf('#!')) ? location.hash.substr(2) + location.search : location.pathname + location.search + location.hash;
    page.replace(url, *****history.state******, true, dispatch);
};

markkemper1 avatar Feb 02 '15 01:02 markkemper1

I have the same issue, where refreshing a page renders a 404. @markkemper1 passing history.state in page.start still returns 404. @shuvalov-anton: how do you propose to handle page refreshes with the current version?

priyatam avatar Apr 13 '15 18:04 priyatam

Any update on this? Was looking at using page.js and ran into this issue very quickly.

bmcinroy avatar Sep 21 '18 16:09 bmcinroy

Not yet, someone needs to implement it.

matthewp avatar Sep 22 '18 16:09 matthewp