page.js
page.js copied to clipboard
support history.state, so page reloads preserve state
I don't got it. Why? mb pushState option can resolve the problem? #90
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.
All fine, we can reopen it if we need.
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.
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: {}
+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);
};
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?
Any update on this? Was looking at using page.js and ran into this issue very quickly.
Not yet, someone needs to implement it.