naja-js / naja

Modern AJAX library for Nette Framework
https://naja.js.org
MIT License
109 stars 15 forks source link

Distinguish replace state and push state in `buildState` event handler #380

Closed zipper closed 7 months ago

zipper commented 1 year ago

Inside buildState event handler, there is now no way to distinguish between new state being pushed, or initial state being replaced. You might distinguish initial state replace, if the window.history.state === null, but that doesn't always work.

Let's say you have a page and click an Naja link there. Initial state is being replaced by Naja state, ajax request is made and a new Naja state is pushed. Then if you refresh the page, there already is window.history.state with some Naja properties. When another Naja link is clicked, replaceInitialState is called, leading to buildState handler. There I have no idea if I'm pushing new state or replacing current state. But it does matter for me, because if latter, I need to preserve some options from window.history.state. At the same time, those options must not be preserved if new state is being pushed (because they obviously belong to now previous state).

For some objective reasons I can't build those options again inside buildState handler when replaceInitialState is being called. It would be very helpful, if I can tell, what led to buildState handler - if it was replaceInitialState or pushNewState call.