Rails 4 和 Turbolinks - 元标签不变 [英] Rails 4 and Turbolinks - Meta Tags not changing
问题描述
我有一个启用了 turbo-links 的 Rails 4 应用程序,但我无法在更改页面时刷新元标记(不是完全刷新).我读到需要在加载 turbo-links 的 javascript 之前包含元标记,但它没有效果.完全刷新可以完成这项工作,但不幸的是,这不是我要搜索的内容.
I have a Rails 4 app with turbo-links enabled, but I can't get the meta tags to refresh when changing the page ( not a full refresh ) . I read that the meta-tags need to be included before the javascript that loads the turbo-links but it has no effect. A full refresh does the job, but unfortunately this is not what i search for.
布局/application.html.haml
layout/application.html.haml
%html
%head
%title= "Title"
- if content_for?(:meta_description)
%meta{content: (yield :meta_description), name: "description"}/
%meta{content: (yield :meta_keywords), name: "keywords"}/
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= javascript_include_tag "application", "data-turbolinks-track" => true
= csrf_meta_tags
%body{class: params[:controller].gsub("/", "-"), id: 'top'}
= render "shared/header"
.body-content
= render "shared/flash"
= yield
= render "shared/footer"
= render "shared/analytics"
helpers/application_helper.rb
helpers/application_helper.rb
def meta_tag(title, description)
def meta_tag(title, description)
content_for :meta_keywords do
title
end
content_for :meta_description do
description
end
结束
我就是这样称呼他们的
= meta_tag('here s my title', 'and the keywords')
谢谢
推荐答案
Turbolinks 的工作原理
Turbolinks 旨在保持 <head>
标记相同,并且仅通过 AJAX 请求将 标记替换为所请求页面的内容.由于您的
标签位于
中,因此它们不会通过 Turbolinks 请求进行更改.
How Turbolinks works
Turbolinks is designed to keep the <head>
tag the same and merely replaces the <body>
tag with the contents of the requested page via an AJAX request. Since your <meta>
tags are located in the <head>
, they won't be changed via a Turbolinks request.
从 SEO 的角度来看,您无需担心 <meta>
标签在 Turbolinks 请求上更新,因为搜索引擎爬虫将始终执行完整页面请求并且不会t 执行 Turbolinks javascript.
From an SEO perspective, you don't need to worry about the <meta>
tags being updated on a Turbolinks request because the search engine crawlers will always do a full page request and won't execute the Turbolinks javascript.
从用户的角度来看, 中唯一需要为每个请求更改的标签是
标签,因此特殊处理这是 Turbolinks 本身内置的.
From a user's perspective, the only tag in the <head>
that needs to change for each request is the <title>
tag, so special handling for that was built-in to Turbolinks itself.
这个问题"已经被提出和驳回过几次,每次都被 DHH 自己宣布为非问题,所以我不会指望这种行为会很快改变.
This "issue" has been raised and shot down a couple times, each time being declared a non-issue by DHH himself, so I wouldn't count on this behavior changing any time soon.
见:
这篇关于Rails 4 和 Turbolinks - 元标签不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!