Turbolinks和特定于控制器的资产 [英] Turbolinks and Controller-specific assets
问题描述
我已修改application.html.erb
以使用控制器特定的资产:
I've modified application.html.erb
to use controller specific assets:
application.html.erb
:
<!DOCTYPE html>
<html>
<head>
<title>My Application</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag params[:controller], 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag params[:controller], 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
... other html template ...
问题是,我已经安装了turbolinks.当我在同一个控制器中导航时,turbolinks起作用.但是,当我切换到另一个控制器时,turbolinks将执行完全重载.有什么办法可以解决这个问题?
The problem is that, I have turbolinks installed. When I navigate through the same controller, turbolinks works. But when I switch to another controller, the turbolinks will perform a full reload. Is there any way to fix this?
推荐答案
涡轮链接
Turbolinks获取HTML页面的<body>
,并使用Ajax对其进行更改,使<head>
区域保持不变.
Turbolinks takes the <body>
of your HTML page, and changes it with Ajax, leaving the <head>
area intact.
如果您的<head>
保持不变,则此 only 有效-否则如何使其保持恒定?因此,在更改控制器页面/资产的意义上,您将必须进行不带Turbolinks的整页更新(至少,如果您使用Turbolinks而不被黑客入侵)
This only works if your <head>
will remain the same - otherwise how can it be kept constant? So in the sense of changing your controller page / assets, you're going to have to go through a full page update without Turbolinks (at least if you use Turbolinks without hacking it)
我建议对此的解决方法是更改您特定于控制器的资产结构,尤其是对<head>
区域进行尽可能少的更改.
I would recommend the fix for this would be to alter your controller-specific asset structure, specifically to make as few changes to the <head>
area as possible.
-
涡轮链接跟踪
阅读 Turbolinks documentation
后,您也许可以从中受益从您的控制器特定资产中删除turbolinks-data-track
选项:
<%= javascript_include_tag controller_name, 'data-turbolinks-track' => false %>
您可以跟踪某些资产,例如application.js和application.css, 您要确保始终是最新版本 Turbolinks会话.这是通过将这些资产链接标记为 data-turbolinks-track,就像这样:
You can track certain assets, like application.js and application.css, that you want to ensure are always of the latest version inside a Turbolinks session. This is done by marking those asset links with data-turbolinks-track, like so:
<link href="/assets/application-9bd64a86adb3cd9ab3b16e9dca67a33a.css"
rel="stylesheet"
type="text/css" data-turbolinks-track>
<link href="/assets/application-9bd64a86adb3cd9ab3b16e9dca67a33a.css"
rel="stylesheet"
type="text/css" data-turbolinks-track>
如果这些资产更改了网址(嵌入md5戳以确保做到这一点),则该页面将执行完整操作 重新加载,而不是通过Turbolinks.这样可以确保所有 Turbolinks会话将始终运行最新的JavaScript 和CSS.
If those assets change URLs (embed an md5 stamp to ensure this), the page will do a full reload instead of going through Turbolinks. This ensures that all Turbolinks sessions will always be running off your latest JavaScript and CSS.
-
使用controller_name
帮助器(代替params[:controller]
)会为您带来好处:
Something you'll benefit from is using the controller_name
helper (in place of params[:controller]
):
<%= javascript_include_tag 'application', controller_name, 'data-turbolinks-track' => true %>
这篇关于Turbolinks和特定于控制器的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!