question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose isSamePage to turbo:visit events

See original GitHub issue

#298 was a very welcome change, as now I can drop data: { turbo: !current_page?(thing_path) } from links that are anchors on the current page. However, turbo:visit events now fire for visits to anchors on the same page. I’m sure this is potentially useful in some ways, but for now, it’s meant that I’ve had to go from hooking into turbo:visit to using turbo:before-visit as follows:

import isEqual from "lodash.isequal"

document.addEventListener("turbo:before-visit", function (e) {
  const attributes = ({ origin, pathname }) => ({ origin, pathname })
  if (
    !isEqual(attributes(window.location), attributes(new URL(e.detail.url)))
  ) {
    // do things here
  }
})

What I’m doing there is pulling the origin (http://example.com) and the pathname (/blog, without an anchor or any query params) from the window location before the request is processed, and comparing it with those the target URL. If they’re identical, then it’s safe to say we shouldn’t do what would otherwise be triggered under turbo:visit.

It’d be nicer to be able to, for example, do this:

document.addEventListener("turbo:visit", function (e) {
  if(e.samePage) { return; }
  // do things here
})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
domchristiecommented, Aug 6, 2021

Latest builds are available here from https://github.com/hotwired/dev-builds/releases, so I think you could try:

yarn add https://github.com/hotwired/dev-builds/archive/refs/tags/@hotwired/turbo/latest.tar.gz
1reaction
domchristiecommented, Jul 22, 2021

Does seem a bit surprising for turbo:visit to fire if you’re staying on the same page?

I tend to agree. The same-page anchor system does use a Visit to do it’s thing, but the fired events do seem weird:

  1. turbo:click
  2. turbo:before-visit
  3. turbo:visit
  4. turbo:before-cache

This could lead to some odd situations, particularly if people are tearing down JavaScript behaviour on turbo:before-cache

I think same-page visits should be “silent” i.e. not fire turbo:before-visit, turbo:visit, or turbo:before-cache. This would also echo the behaviour that the native adapters take.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found