alphaleonis
alphaleonis

Reputation: 1349

Open Graph head meta tags not accessible to Facebook Crawler

I am trying to allow the Facebook Crawler to access the meta information with Open Graph tags on a site, but it is not reading the inner content of the head tag.

If I echo the scrape information, it shows the head tag as:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
  <head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head>
<body>

 ...

At the source of the page, however, the head information contains the meta tags:

<!DOCTYPE html>
<html>
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="referrer" content="always" />
  <meta property="fb:app_id" content="375576830972731" />
  <meta property="og:site_name" content="Site Name" />
  <meta property="og:title" content="Title of content" />
  <meta property="og:url" content="https://website.domain.com/123456" />
  <meta property="og:image" content="http://s3-us-west-1.amazonaws.com/domain/media/images/001/original/001" />
  <meta property="og:description" content="Description of content" />
  <meta property="og:type" content="article" />
</head>

My controller is set up to allow a `protect_from_forgery" exception, and I made sure to allow for the Facebook crawlers in robots.txt:

User-agent: Facebot
Allow: /

User-agent: facebookexternalhit/1.1
Allow: /

Why can't Facebook's crawler get to the meta tags in my header?

Upvotes: 0

Views: 1073

Answers (1)

alphaleonis
alphaleonis

Reputation: 1349

I was able to solve this problem by changing the respond_to block in the controller for the articles I was trying to display.

I had something like this:

class ArticlesController < ApplicationController

  def show
  ...

    respond_to do |format|
      format.js
      format.html
    end
  end

end

However, I noticed that when I executed curl https://website.domain.com/123456, the first thing returned was a javascript function associated with the view, instead of the proper html tags.

Placing format.html before format.js solved this issue.

class ArticlesController < ApplicationController

  def show
  ...

    respond_to do |format|
      format.html
      format.js
    end
  end

end

Upvotes: 1

Related Questions