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

Support replacing state #335

Closed
boardfish opened this issue Aug 6, 2021 · 5 comments
Closed

Support replacing state #335

boardfish opened this issue Aug 6, 2021 · 5 comments

Comments

@boardfish
Copy link

As highlighted here, if I use Turbo.navigator.history.replace({ absoluteURL: '/posts/1/edit' }) to replace the current state, I get the following error when clicking a link on the page:

Uncaught TypeError: url.href is undefined
    getAnchor turbo.es2017-esm.js:261
    getRequestURL turbo.es2017-esm.js:280
    locationWithActionIsSamePage turbo.es2017-esm.js:3450
    Visit turbo.es2017-esm.js:2229
    startVisit turbo.es2017-esm.js:3289
    visitProposedToLocation turbo.es2017-esm.js:2788
    visitProposedToLocation turbo.es2017-esm.js:4372
    proposeVisit turbo.es2017-esm.js:3281
    visit turbo.es2017-esm.js:4274
    followedLinkToLocation turbo.es2017-esm.js:4339
    clickBubbled turbo.es2017-esm.js:3217
    clickCaptured turbo.es2017-esm.js:3204
    start turbo.es2017-esm.js:3230
    start turbo.es2017-esm.js:4235
    start turbo.es2017-esm.js:5435

The link is inside a Turbo frame which has target="_top" set.

Since Turbo's own history breaks under native window.history.replaceState, it'd be good to have a supported alternative in a similar way to how Turbo callbacks replace $(document).ready() in jQuery, for example.

@tortuetorche
Copy link

Hi @boardfish,

If you try with this command: Turbo.navigator.history.replace(new URL('http://localhost/posts/1/edit')), is it OK?

Cheers,
Tortue Torche

@boardfish
Copy link
Author

Thanks @tortuetorche, that appears to have worked. I ended up using the following:

window.Turbo.navigator.history.replace(new URL(this.turboFrameTarget.src, `${window.location.protocol}//${window.location.host}`))

@seanpdoyle
Copy link
Contributor

@boardfish When constructing a new URL instance, have you tried passing the value of Node.baseURI as the second argument?

const { src, baseURI } = this.turboFrameTarget

const url = new URL(src, baseURI)

@boardfish
Copy link
Author

I didn't know that was a thing! Good shout - thanks @seanpdoyle

@pySilver
Copy link

While it works almost OK, I bet it is not necessarily what a user might expect. Two issues:

  • User cannot return to the previous state anymore
  • Preview holds outdated state

This is what I've come up with instead: #792 (comment)

It feels a little bit hackish and I'd love to have a cleaner way to update URLs from the stream responses.

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