Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually push history with action advance #518

Closed
acetinick opened this issue Jan 23, 2022 · 3 comments
Closed

Manually push history with action advance #518

acetinick opened this issue Jan 23, 2022 · 3 comments

Comments

@acetinick
Copy link

acetinick commented Jan 23, 2022

I have a situation where I would like to push a history item when I open modal window using a stimulus controller. I have this so far from looking at the code to try push history

window.Turbo.navigator.history.push(new URL(url))

This works good for replacing the URL and adding history state to the browser, however when you click the back button in the browser it replaces with the old URL but doesn't visit it. I would also like the back button to work similar to the way we can use data: { turbo_action: "advance" } on a regular tag. I know this is based on the work that @seanpdoyle worked on, is there perhaps a way to manually push history and make it still work with back button ?

@jaryl
Copy link

jaryl commented Mar 8, 2022

Try pushing with the current state:

history.pushState(history.state, "", new URL(url));

@dhh dhh closed this as completed Jun 19, 2022
@pySilver
Copy link

pySilver commented Nov 9, 2022

@acetinick did you manage to solve this?

@jaryl I've tried doing the following

Turbo.StreamActions.my_push_state = function () {
  const url = new URL(this.getAttribute("url"));
  Turbo.navigator.history.push(url);
  history.pushState(history.state, "", url);
  console.log("my_push_state", url, history.state);
};

My use case looks like a list of products and filter form. Whenever user triggers form submit I receive a stream response that updates the content of a page and page URL (to something shareable):

<turbo-stream action="replace"><template>...list of products</template></turbo-stream>

<turbo-stream
  action="my_push_state"
  url="http://localhost:3000/?foo=bar"
></turbo-stream>

It almost works. URL is being updated but when the back button is pressed the following error is dropped to the console:

turbo.es2017-esm.js?7ebb:1457 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
    at ProgressBar.uninstallProgressElement (webpack-internal:///./node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js:1481:38)
    at eval (webpack-internal:///./node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js:1455:22)

I'm not sure what I'm doing wrong here. What I want is to let the user return to original content before stream response changes were made.

@pySilver
Copy link

pySilver commented Nov 9, 2022

I've opened a separate issue for that #792

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants