barba
barba copied to clipboard
Add data to history / pushState
The problem
It seems to be not possible to add data using pushState as the state gets overwritten by barba.js own data. There neither seems to be no method provided by barba.js to add data to the current History barba.history
Description
Details
In a similar way to scroll which seems to be managed automatically by barba.js, we would like to add our own data to the history object.
Code To Reproduce Issue [ Good To Have ]
Steps to reproduce issue [ Good To Have ]
browse to any page push some data
window.history.pushState('{"random_data":1}', '', `?new_random_state`);
check that data is there:
history.state
"{\"random_data\":1}"
click on any link click on previous button check history.state;
history.state
we lost all data previously added and history.state just contains other barba.js data:
{from: "barba", index: 0, states: Array(1)}
Hello @goyavo,
This is the intended behavior.
Barba uses pushState
to manage internal page history.
What's the goal of pushing random data into the window history here? :thinking:
What's is the goal of pushing random data into the window history here? 🤔
Thanks for answering and apologies, I somehow completely missed the reply notification.
Perhaps the expression "random data" is misleading, we were thinking of specific things related to the state of the page like:
the tab that was open on that page the product that was clicked when leaving the page etc
basically any kind of state data which could normally be pushed in window.history and then retrieved on load to restore the previous state of the page. Currently everything is being overwritten by barbajs.
@goyavo OK I understand what you mean :+1:
May be at least it would be better that Barba store their own data into a "subkey" inside the state object instead of erasing the whole state. We will discuss that with Thierry in our next meeting :wink:
cc @thierrymichel
That would be lovely to have a way to store data in the history state. It could in the same time solve issues with the back and forward button from the browser related to issue #601 .