ruby-on-rails 在Rails中,turbo streams中的链接会发出两次请求

hzbexzde  于 11个月前  发布在  Ruby
关注(0)|答案(1)|浏览(205)

我有3个模型,每个模型都有一个index页面,该页面通过指向show页面的链接延迟加载所有相应的示例。想象一下这样的情况:
x1c 0d1x的数据
在点击任何模型的任何示例的链接时,它会对同一页面和请求类型发出几个请求:

22:32:33 web.1  | Started GET "/transactions/5709759" for 127.0.0.1 at 2023-11-23 22:32:33 +0530
22:32:33 web.1  | Processing by TransactionsController#show as HTML
22:32:33 web.1  |   Parameters: {"id"=>"5709759"}
22:32:33 web.1  |   Transaction Load (0.4ms)  SELECT "transactions".* FROM "transactions" WHERE "transactions"."slug" = $1 ORDER BY "transactions"."date" DESC LIMIT $2  [["slug", "5709759"], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ config/initializers/cancancan.rb:18:in `find'
22:32:33 web.1  |   User Load (0.4ms)  SELECT "users"."id", "users"."slug" FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ app/controllers/application_controller.rb:19:in `current_user'
22:32:33 web.1  |   Thaali Load (1.0ms)  SELECT "thaalis".* FROM "thaalis" WHERE "thaalis"."id" = $1 LIMIT $2  [["id", 168], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ app/controllers/transactions_controller.rb:18:in `show'
22:32:33 web.1  |   Sabeel Load (0.5ms)  SELECT "sabeels".* FROM "sabeels" WHERE "sabeels"."id" = $1 LIMIT $2  [["id", 69], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ app/controllers/transactions_controller.rb:19:in `show'
22:32:33 web.1  |   Rendering layout /usr/share/rvm/gems/ruby-3.2.2/gems/turbo-rails-1.5.0/app/views/layouts/turbo_rails/frame.html.erb
22:32:33 web.1  |   Rendering transactions/show.html.erb within layouts/turbo_rails/frame
22:32:33 web.1  |   Rendered transactions/_transaction.html.erb (Duration: 0.5ms | Allocations: 143)
22:32:33 web.1  |   Rendered shared/_destroy_modal.html.erb (Duration: 0.5ms | Allocations: 126)
22:32:33 web.1  |   Rendered shared/_actions.html.erb (Duration: 1.2ms | Allocations: 243)
22:32:33 web.1  |   Rendered transactions/show.html.erb within layouts/turbo_rails/frame (Duration: 2.7ms | Allocations: 580)
22:32:33 web.1  |   Rendered layout /usr/share/rvm/gems/ruby-3.2.2/gems/turbo-rails-1.5.0/app/views/layouts/turbo_rails/frame.html.erb (Duration: 3.0ms | Allocations: 679)
22:32:33 web.1  | Completed 200 OK in 34ms (Views: 3.9ms | ActiveRecord: 2.3ms | Allocations: 3942)
22:32:33 web.1  | 
22:32:33 web.1  | 
22:32:33 web.1  | Started GET "/transactions/5709759" for 127.0.0.1 at 2023-11-23 22:32:33 +0530
22:32:33 web.1  | Processing by TransactionsController#show as HTML
22:32:33 web.1  |   Parameters: {"id"=>"5709759"}
22:32:33 web.1  |   Transaction Load (0.4ms)  SELECT "transactions".* FROM "transactions" WHERE "transactions"."slug" = $1 ORDER BY "transactions"."date" DESC LIMIT $2  [["slug", "5709759"], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ config/initializers/cancancan.rb:18:in `find'
22:32:33 web.1  |   User Load (0.2ms)  SELECT "users"."id", "users"."slug" FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ app/controllers/application_controller.rb:19:in `current_user'
22:32:33 web.1  |   Thaali Load (0.7ms)  SELECT "thaalis".* FROM "thaalis" WHERE "thaalis"."id" = $1 LIMIT $2  [["id", 168], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ app/controllers/transactions_controller.rb:18:in `show'
22:32:33 web.1  |   Sabeel Load (0.7ms)  SELECT "sabeels".* FROM "sabeels" WHERE "sabeels"."id" = $1 LIMIT $2  [["id", 69], ["LIMIT", 1]]
22:32:33 web.1  |   ↳ app/controllers/transactions_controller.rb:19:in `show'
22:32:33 web.1  |   Rendering layout layouts/application.html.erb
22:32:33 web.1  |   Rendering transactions/show.html.erb within layouts/application
22:32:33 web.1  |   Rendered transactions/_transaction.html.erb (Duration: 0.4ms | Allocations: 143)
22:32:33 web.1  |   Rendered shared/_destroy_modal.html.erb (Duration: 0.4ms | Allocations: 126)
22:32:33 web.1  |   Rendered shared/_actions.html.erb (Duration: 0.7ms | Allocations: 243)
22:32:33 web.1  |   Rendered transactions/show.html.erb within layouts/application (Duration: 1.6ms | Allocations: 580)
22:32:33 web.1  |   Rendered shared/navbar/_resources_dropdown.html.erb (Duration: 0.1ms | Allocations: 66)
22:32:33 web.1  |   Rendered shared/navbar/_statistics_dropdown.html.erb (Duration: 0.1ms | Allocations: 48)
22:32:33 web.1  |   Rendered shared/navbar/_admin_dropdown.html.erb (Duration: 0.2ms | Allocations: 113)
22:32:33 web.1  |   Rendered shared/navbar/_navbar.html.erb (Duration: 0.8ms | Allocations: 557)
22:32:33 web.1  |   Rendered shared/_flash_messages.html.erb (Duration: 0.0ms | Allocations: 19)
22:32:33 web.1  |   Rendered layout layouts/application.html.erb (Duration: 2.9ms | Allocations: 1529)
22:32:33 web.1  | Completed 200 OK in 17ms (Views: 3.3ms | ActiveRecord: 1.9ms | Allocations: 4808)

字符串
如果我是正确的,它应该发出一个HTML请求,我注意到第一个请求从这里呈现了一个布局:

Rendered layout /usr/share/rvm/gems/ruby-3.2.2/gems/turbo-rails-1.5.0/app/views/layouts/turbo_rails/frame.html.erb (Duration: 3.0ms | Allocations: 679)


而第二个请求是从这里提交的:

Rendered layout layouts/application.html.erb (Duration: 2.9ms | Allocations: 1529)


此内容从部分_transactions.html.erball.turbo_stream.erb页面呈现,而show.html.erb文件也通过单个HTML请求呈现它w/o turbo流
所以我的问题是,**在涡轮流的链接,使2个请求呈现一个页面是正常的吗?**有人经历过这个问题吗?

仅供参考

  • 这个bug并不是特定于某个环境的,因为我在生产日志中也看到过。
  • 这个bug也不是特定于turbo-rails版本,因为它在以前的1.3.2版本中有相同的bug。
    当前规格
  • Ruby3.2.2
  • 导轨7.1.2
  • 涡轮导轨1.5.0

我在hotwireturbo领域没有太多经验,所以我不知道该尝试什么。所以如果有人在这方面有经验,我会喜欢你的2美分!:)
谢谢你,谢谢

t0ybt7op

t0ybt7op1#

如果你在浏览器中查看js控制台,你很可能会看到类似“响应中缺少帧,正在进行全页加载”的内容。
这些链接会自动指向“最近的”turbo帧。然后,Turbo会在响应中期望一个相应的Turbo-Frame标签,而这个标签并不存在。因此,Turbo会重新加载整个布局(就像Turbo-Drive请求一样-这是您最初的意图)。
要解决这个问题,请在链接中添加data-turbo-frame="_top"
参见:https://turbo.hotwired.dev/handbook/frames#targeting-navigation-into-or-out-of-a-frame

相关问题