facebook open graph crawler triggering json response in rails actions

前端 未结 2 757
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 14:49

For some reason the facebook crawler is triggering the json response in my rails actions. This causes the action to just return a json representation of the object, without

相关标签:
2条回答
  • 2020-12-20 15:26

    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.

    0 讨论(0)
  • 2020-12-20 15:30

    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.

    0 讨论(0)
提交回复
热议问题