Rails检测Facebook共享与正常页面视图(Rails detecting Facebook share vs normal page view)

编程入门 行业动态 更新时间:2024-10-12 10:28:24
Rails检测Facebook共享与正常页面视图(Rails detecting Facebook share vs normal page view)

当用户通过Facebook共享按钮共享页面时,我正在尝试检测爬行; 但是,在查看实时日志时,单击按钮时似乎没有任何东西击中我的控制器。

我们的想法是检测user-agent并呈现与用户实际访问网站时共享的不同内容。

在我的控制器中,我有以下内容:

def show Rails::logger.debug "********************************" Rails::logger.debug "#{self.class.name} - params: #{params.inspect}" Rails::logger.debug "#{self.class.name} - request.headers['HTTP_USER_AGENT']: #{request.headers['HTTP_USER_AGENT'].inspect}" Rails::logger.debug "********************************" render "pages/fb_test", layout: false end

分享按钮

.fb-share-button{:"data-href" => "http://myapp.herokuapp.com/pages/1", :"data-layout" => "button", :"data-size" => "small", :"data-mobile-iframe" => false} %a.fb-xfbml-parse-ignore{target: "_blank", href: "https://www.facebook.com/sharer/sharer.php?u=http://myapp.herokuapp.com/pages/1"} Share

我假设当我点击分享按钮时,Facebook会点击提供的URL,抓取页面并返回相关内容。 但是,当我使用heroku logs --tail时,当我点击分享按钮时,我根本没有看到任何活动,但内容仍然在Facebook共享弹出窗口中正确呈现。

我是否需要在Rack级别检测到这一点?

I'm trying to detect a crawl when a user shares a page via the Facebook share button; However, when looking at the live logs, nothing appears to be hitting my controller when I click on the button.

The idea was to detect the user-agent and render different content to be shared vs when a user actually visits the site.

In my controller I have the following:

def show Rails::logger.debug "********************************" Rails::logger.debug "#{self.class.name} - params: #{params.inspect}" Rails::logger.debug "#{self.class.name} - request.headers['HTTP_USER_AGENT']: #{request.headers['HTTP_USER_AGENT'].inspect}" Rails::logger.debug "********************************" render "pages/fb_test", layout: false end

share button

.fb-share-button{:"data-href" => "http://myapp.herokuapp.com/pages/1", :"data-layout" => "button", :"data-size" => "small", :"data-mobile-iframe" => false} %a.fb-xfbml-parse-ignore{target: "_blank", href: "https://www.facebook.com/sharer/sharer.php?u=http://myapp.herokuapp.com/pages/1"} Share

I would assume that when I click on the share button, Facebook would hit the URL provided, crawl the page and return the relevant content. However, when using heroku logs --tail I'm not seeing any activity at all when I click on the share button, and yet the content is still rendered properly in the Facebook share popup.

Do I need to detect this at the Rack level or something?

最满意答案

Facebook缓存您的网站。 您需要访问https://developers.facebook.com/tools/debug/sharing/ ,输入您网站的URL并单击“再次抓取”以刷新Facebook的缓存。

Facebook caches your website. You'll need to go to https://developers.facebook.com/tools/debug/sharing/, enter the URL of your website and click on "Scrape again" to refresh Facebook's cache.

更多推荐

本文发布于:2023-08-05 18:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   页面   Rails   Facebook   detecting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!