skip to Main Content

For SEO reasons I want to give Google more info about our magazine, which is part of our website. Google recognizes that there is paginated content. So I was told to add, for every page of the magazine, next and prev metatags/links like so (for example for page 3):

<link rel="prev" href="http://www.bla.com/de/magazin?page=2" />
<link rel="next" href="http://www.bla.com/de/magazin?page=4" />

We’re using JSON with the Kaminari gem to paginate our articles.
Here’s the magazine_controller‘s index action:

def index
  @articles = MagazineArticle.visible
                             .includes(:magazine_category)
                             .order(published_at: :desc)
                             .page(params[:page]).per(7)
  @slider_articles = @articles.first(3)
  @advertisement = Advertisement.for_start_page

  #set_pagination_meta_tags(params[:page])
  set_magazine_meta_tags

  respond_to do |format|
    format.html
    format.json { render_new_page(@articles, @advertisement) }
  end
end

# the method that renders the new page (as JSON)
def render_new_page(articles, advertisement)
  render json: {
    content: {
      articles: render_to_string(
        partial: 'magazine/article_page',
        locals: { articles: articles, advertisement: advertisement }
      ),
      link: render_to_string(
        partial: 'magazine/load_more_button',
        locals: { articles: articles }
      )
    }
  }
end

Also we are using the Metatags gem to handle metatags.

I wrote a method which adds the links relative to
the current page with a method which I call in the index action
as you can see in above controller code:

def set_pagination_meta_tags(page)
  page = page.to_i
  case page
  when 0..1
    set_meta_tags(next: magazine_url(I18n.locale, page: 2))
  when @articles.total_pages
    set_meta_tags(prev: magazine_url(I18n.locale, page: @articles.total_pages - 1))
  when [email protected]_pages - 1
    set_meta_tags(prev: magazine_url(I18n.locale, page: page-1))
    set_meta_tags(next: magazine_url(I18n.locale, page: page+1))
  end
end

But this works only for the first page, then JSON is used to navigate / load more and this doesn’t change the meta tags.

Before going deeper i want to know: Is it possible at all to set these links up? Does it make sense for this kind of (dynamic) pagination?
Thanks!

2

Answers


  1. Kaminari will actually do this for you – see the relevant documentation here: https://github.com/kaminari/kaminari#the-rel_next_prev_link_tags-helper-method

    If you want to delve a little deeper into the implementation, then it looks like this is the PR that added it: https://github.com/kaminari/kaminari/pull/200/files

    TLDR – add this to your view

    # In head:
    <head>
      <%= yield :head %>
    </head>
    
    # Somewhere in body:
    <% content_for :head do %>
      <%= rel_next_prev_link_tags @articles %>
    <% end %>
    
    Login or Signup to reply.
  2. Similar to what you are already using you could just return the some rendered meta_pagination partial which contains

    = rel_next_prev_link_tags @articles
    

    Your render_new_page would look like this

    def render_new_page(articles, advertisement)
      render json: {
        content: {
          articles: render_to_string(
            partial: 'magazine/article_page',
            locals: { articles: articles, advertisement: advertisement }
          ),
          link: render_to_string(
            partial: 'magazine/load_more_button',
            locals: { articles: articles }
          ),
          meta_pagination: render_to_string(
            partial: 'layouts/meta_pagination',
            locals: { articles: articles }
          )
        }
      }
    end
    

    (adjust the path to the meta pagination partial)

    Then in your javascript remove the rel next/prev

    $("head link[rel='next']").remove();
    $("head link[rel='prev']").remove();
    

    and append the new content to html head with

    $('head').append(metaPagination);
    

    with var metaPagination = response.content.meta_pagination;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search