Rails 4 and Turbolinks - Meta Tags not changing

2019-03-11 05:16发布

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.

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

def meta_tag(title, description)

content_for :meta_keywords do
  title
end

content_for :meta_description do
  description
end   

end

And this is how I call them

= meta_tag('here s my title', 'and the keywords')

Thanks

4条回答
爷、活的狠高调
2楼-- · 2019-03-11 05:42

I extend the current turbolinks js ;)

It will replace meta tags and canonical link!

https://github.com/philklei/turbolinks

查看更多
We Are One
3楼-- · 2019-03-11 05:43

It's a TurboLinks Problem

By design, Turbolinks basically keeps the <head> of your page the same & calls the <body> of your document via ajax if it's going to remain the same (you're using the same controller / action). It's some crazy thing to maintain application performance

Here is a good explanation of Turbolinks


I've had a similar problem to this with Javascript, and was able to use the Jquery-Turbolinks gem to keep JS rendering

For your meta tags, although I don't know a solution off hand, I managed to find a decent gem you might benefit from: MetaMagic. It allows you to define meta tags in the view, kind of like how the content_block facility works. This should be loaded on every http request

查看更多
beautiful°
4楼-- · 2019-03-11 05:51

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.

What about SEO?

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.

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.

Will this behavior ever change?

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.

See:

查看更多
Emotional °昔
5楼-- · 2019-03-11 05:54

Have a look on meta-tags gem

"Using with pjax" section

查看更多
登录 后发表回答