OpenGraph on Ajax Based Website

怎甘沉沦 提交于 2019-11-28 06:01:23

No. Open Graph markup must be present on HTML pages which are GETable with pure HTTP.

This is because when a user interacts with an OG object (like, performs an action etc) Facebook will perform an HTTP GET on the OG URL, and expect to see OG tags returned in the markup.

The solution is to create canonical URLs for each of your objects. These URLs contain basic HTML markup including OG tags.

On requests to these URLs, if you see the incoming useragent string containing 'facebookexternalhit' then you render the HTML. If you don't, you serve a 302 which redirects to your ajax URL. On the ajax URLs, your like buttons and any OG actions you publish should point to the canonical URL object

Example:

As a user, I'm on http://yoursite.com/#!/artists/monet. I click a like button, or publish an action, but the like button's href parameter, or the URL of the object when you post the action should be a web hittable canonical URL for the object - in this case, perhaps http://yoursite.com/artists/monet

When a user using a browser hits http://yoursite.com/artists/monet you should redirect them to http://yoursite.com/#!/artists/monet, but if the incoming useragent says it is Facebook's scraper, you just return markup which represents the artist Monet.

For real world examples, see Deezer, Rdio and Mog who all use this design pattern.

polyclick

A little bit more investigation lead to the following findings:

Let's say you made an application with a hash that looks like this:

http://yoursite.com/#/artists/monet

The Facebook scraper will call your url without the /#/artists/monet part. This is a problem because you have no way of knowing what information you have to parse into the meta og: tags.

Then try the same with the suggested url as Simon says:

http://yoursite.com/#!/artists/monet

Now you'll notice that the Facebook scraper is respecting the google ajax specifications and it will convert the #! to ?_escaped_fragment_= so the URL looks like this:

http://yoursite.com/?_escaped_fragment_=/artists/monet

You can check this out for yourself with the facebook debugger: https://developers.facebook.com/tools/debug

  • upload the php script to your server
  • go to the facebook debugger
  • enter the url with the /#/ part
  • click 'See exactly what our scraper sees for your URL' - no hash fragment
  • enter the url again with /#!/
  • click 'See exactly what our scraper sees for your URL' - hash fragment has been turned to
    ?_escaped_fragment_=

The script

<html>
  <head>
    <title>Scraping</title>
  </head>
  <body>
    <?
      print_r($_SERVER);
    ?>
  </body>
</html>

Or summarized: always use /#!/ (hashbang) deeplinks ;)

I ran a quick test that seems to work. Dependant on the fact the FB scraper doesn't run JavaScript.

As most of my sites are static Single Page Apps with no server logic, I can generate the static pages quickly with tools such as Grunt and Gulp.

If you Share http://wh-share-test.s3-website-eu-west-1.amazonaws.com/test

Facebook will scrape the test page meta tags, when a user clicks the link the JS redirects to /#/test for my single page app to react and present the correct view.

Seems hacky but works;

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>This is a shared item</title>
</head>
<body>
    <h1>This is a shared item page.</h1>
    <script>
        var path = window.location.pathname;
        window.location = '/#' + path;
    </script>
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!