You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the new or updated feature that you are suggesting?
The ability to add to and edit the existing history in MemoryRouter, without navigation.
Being able to add to the front (and adjusting the history.index and history.length accordingly), or add to the back (and just adjusting the history.length).
and the ability to update an entry without push/replace.
Why should this feature be included?
Due to certain requirements, my app ui needs to be in an iframe, but I want forward/back navigation to work, as well as the changed routes to reflect in the address bar. I can have some custom JS in the parent window.
This causes a couple requirements:
changes to route in iframe need to be sent to parent window
forward/back in parent window need to be passed to iframe
when page reloads, memory router loses all history. State in parent history needs to reflect everything the router needs to rebuild history
when rebuilding history, needs to change history with methods other than push/replace, as that's not what the parent's history actions are
I've come up with a hacky solution:
Parent window:
// if no history initially, first time loading app. Will be last item in history. Replace state with index
if (!history.state?.index) {
history.replaceState({
index: window.history.length - 1,
}, '');
}
let currentHistoryIndex = history.state.index;
const historyChangeListener = (event) => {
const newHistoryIndex = event.state.index;
const goDistance = newHistoryIndex - currentHistoryIndex;
currentHistoryIndex = newHistoryIndex;
if (goDistance) {
iframe.postMessage({ type: 'historyGo', args: [goDistance, event.state.entry]}, iframeOrigin);
}
};
window.addEventListener('popstate', historyChangeListener );
const messageListener = (e) => {
const eventOrigin = e.origin;
if (eventOrigin !== iframeOrigin) {
return;
}
const { type, args} = e.data;
switch(type) {
case 'historyPushState':
{
const index = window.history.length;
const entry = args[0];
currentHistoryIndex = index;
window.history.pushState({ index, entry }, '', entry.pathname);
return;
}
case 'historyReplaceState':
{
const index = window.history.state.index;
const entry = args[0];
currentHistoryIndex = index;
window.history.replaceState({ index, entry }, '', entry.pathname);
return;
}
default:
return;
}
};
// listen to parent window
window.addEventListener('message', messageListener, false);
I've cleared up all of the logic dealing with my app, so my apologies if I accidentally broke these snippets in the process.
This is currently working, however, since I am manually updating the history in the historyGo method (and sometimes adding undefined entries), my concern is that the logic could change in the future, and this hack might now work.
This discussion was converted from issue #9703 on December 08, 2022 19:06.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What is the new or updated feature that you are suggesting?
The ability to add to and edit the existing history in MemoryRouter, without navigation.
Being able to add to the front (and adjusting the history.index and history.length accordingly), or add to the back (and just adjusting the history.length).
and the ability to update an entry without push/replace.
Why should this feature be included?
Due to certain requirements, my app ui needs to be in an iframe, but I want forward/back navigation to work, as well as the changed routes to reflect in the address bar. I can have some custom JS in the parent window.
This causes a couple requirements:
I've come up with a hacky solution:
Parent window:
Child window:
I've cleared up all of the logic dealing with my app, so my apologies if I accidentally broke these snippets in the process.
This is currently working, however, since I am manually updating the history in the
historyGo
method (and sometimes adding undefined entries), my concern is that the logic could change in the future, and this hack might now work.Beta Was this translation helpful? Give feedback.
All reactions