facebook open graph crawler triggering json response in rails actions

笑着哭i 提交于 2019-11-29 13:51:08

Ok so Facebook sends an accepts header of

*/*

Since no specific format is requested, rails just goes down the respond_to block in order. If you list your js first in the respond_to block like below rails will respond to the facebook open crawler with JSON which will not work:

respond_to do |format|
  format.js { render :json => @this.to_json }
  format.html
end

Just switch the order so by default rails responds with HTML:

respond_to do |format|
  format.html
  format.js { render :json => @this.to_json }
end

I'm not sure why Facebook does not specify the format they are looking for... seems pretty idiotic to me. Hopefully this is helpful to somebody down the road.

Check what HTTP request headers the Facebook crawler is sending – especially the Accept header.

Could well be that they send a value that let’s your application think it has to send something different then the normal HTML output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!