Turbo Drive不拦截链接点击(但是Turbo:负载事件火灾)

发布于 2025-01-24 20:03:35 字数 955 浏览 4 评论 0 原文

我正在尝试将Turbo添加到Rails 6.1应用程序中。

它似乎正在加载(请参阅“我尝试过的内容”),但是在单击时,单击似乎并没有截获。我仍在全页重新加载。

谁能帮我发现这里缺少什么?

我尝试

  1. 使用console.log()输出添加了一个事件处理程序,该输出表明 turbo:load 的确被触发了
  2. ,确保了 turbo.session.drive = true = true in application.js
  3. 甚至尝试将 data-turbo =“ true” 添加到< main> 标签,但这似乎没有任何区别。鉴于PT,也许并不奇怪。 2。Firefox
  4. 和Chrome

设置

// app/javascript/application.js

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = true
# config/importmap.rb

pin 'application', preload: true
pin '@hotwired/turbo-rails', to: 'turbo.js'
// Debugging statement 

document.addEventListener('turbo:load', function() {
  console.log('turbo:load');
})

I'm experimenting with adding Turbo to a Rails 6.1 app.

It seems to be loading (see "What I've tried"), but when clicking around the clicks does not seem to be intercepted. I'm still getting full page reloads.

Can anyone help me spot what is missing here?

What I've tried

  1. Added an event handler with console.log() output that shows that the turbo:load is indeed getting triggered
  2. Ensured Turbo.session.drive = true in application.js
  3. Even tried adding data-turbo="true" to a <main> tag, but that seems to have made no difference. Perhaps not too surprising, given pt. 2.
  4. Firefox and Chrome

Setup

// app/javascript/application.js

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = true
# config/importmap.rb

pin 'application', preload: true
pin '@hotwired/turbo-rails', to: 'turbo.js'
// Debugging statement 

document.addEventListener('turbo:load', function() {
  console.log('turbo:load');
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅冕 2025-01-31 20:03:35

因此,我发现仅包含Google Analytics(分析)4脚本标签&lt; head&gt; 导致Turbo不起作用:

<script async src="https://www.googletagmanager.com/gtag/js?id=<OUR ID>"/>

原因? GA4的自动出站链接点击跟踪,我们已启用。有一个关于此的Github最新问题。正如评论中提到的:

“ ...似乎他们在链接单击事件上称Destractefault,即使
这不是出站点击。 Turbo然后确定这不是因此,允许导航按正常进行“


方式继续前进。

So I found out that the mere inclusion of the Google Analytics 4 script tag in <head> caused Turbo not to work:

<script async src="https://www.googletagmanager.com/gtag/js?id=<OUR ID>"/>

The reason? GA4's automatic outbound link click tracking, which we have enabled. There's a very recent Github issue about this. As mentioned in a comment:

"... seems like they call preventDefault on link-click events, even if
it's not an outbound click. Turbo then decides it's not a significant
event
so allows the navigation to go ahead as normal"

I disabled this feature in GA4, and was good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文