hotwired / turbo

The speed of a single-page web application without having to write any JavaScript
https://turbo.hotwired.dev
MIT License
6.65k stars 421 forks source link

BUG: The turbo:frame-load event is fired before window.location is updated when using turbo-action="advance" #651

Closed rathboma closed 2 years ago

rathboma commented 2 years ago

To perform some actions when the turbo frame has finished loading and the URL has changed is currently not possible.

When turbo-frame-load fires, the window.location has not yet been updated, so checking window.location.* still gives the old URL.

Steps to reproduce:

function logEvent(event) {
  console.log("comparing src vs window:", event.target.src, window.location.href)
)

document.documentElement.addEventListener('turbo:frame-load', logEvent)
seanpdoyle commented 2 years ago

Thank you for opening this issue!

Is this potentially a duplicate of #617, which might be resolved by https://github.com/hotwired/turbo/pull/618? #618 is merged, but not yet released.

rathboma commented 2 years ago

I checked out the issue. It's similar but not exactly the same -- regardless of whether you change location before or after rendering, certainly it should be changed before turbo:frame-load fires.

That fix might fix it, I have a hard time knowing -- does that change the behavior to address #617? If so it probably will

seanpdoyle commented 2 years ago

I've just tested this locally to confirm that it has been resolved by https://github.com/hotwired/turbo/pull/618.

It's covered by a test that makes the same check during the turbo:frame-render event, which is dispatched before the turbo:frame-load event.