Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 and Turbolinks - Meta Tags not changing

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

like image 687
vladCovaliov Avatar asked Oct 19 '13 08:10

vladCovaliov


3 Answers

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:

  • https://github.com/rails/turbolinks/issues/81
  • https://github.com/rails/turbolinks/pull/165
like image 85
Carlos Ramirez III Avatar answered Nov 01 '22 10:11

Carlos Ramirez III


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

like image 2
Richard Peck Avatar answered Nov 01 '22 09:11

Richard Peck


I extend the current turbolinks js ;)

It will replace meta tags and canonical link!

https://github.com/philklei/turbolinks

like image 1
Philip Kleimeyer Avatar answered Nov 01 '22 11:11

Philip Kleimeyer