简体   繁体   English

如何在静态网站上动态呈现社交分享按钮的元标记?

[英]How to dynamically render meta tags for social share button on a static site?

I have a static site where users can generate images and content in unlimited combinations. 我有一个静态网站,用户可以无限组合生成图像和内容。 When the user is finished, permalink is generated to allow them to share their creation via Facebook/Twitter/GooglePlus/etc. 用户完成后,会生成永久链接以允许他们通过Facebook / Twitter / GooglePlus /等分享他们的创作。 I want their shared post to contain the user's image and content, but I discovered that the social media bots only parse HTML meta tags from my permalinks, and they do not evaluate any Javascript. 我希望他们的共享帖子包含用户的图像和内容,但我发现社交媒体机器人只从我的永久链接中解析HTML元标记,并且他们不评估任何Javascript。

How can I make my user's content appear on the shared social media post? 如何让我的用户的内容出现在共享的社交媒体帖子上? When the permalink is clicked, client side Javascript will evaluate the permalink's query params and render the user's content. 单击永久链接时,客户端Javascript将评估永久链接的查询参数并呈现用户的内容。 I can render the user's content inside HTML meta tags too, which is used by Twitter and FB to populate a post's content, but the bots don't evaluate any javascript, so the meta tags are not rendered. 我也可以在HTML元标记内呈现用户的内容,Twitter和FB使用它来填充帖子的内容,但机器人不评估任何javascript,因此不会呈现元标记。

One possible solution that I have explored is pre-rendering. 我探索过的一个可能的解决方案是预渲染。 Perhaps my permalink can be the location of an HTML file on an AWS S3 bucket, which I can create as soon as the user shares their permalink. 也许我的永久链接可以是AWS S3存储桶上HTML文件的位置,我可以在用户共享其固定链接后立即创建该文件。 That HTML file on S3 can have the user-specific meta tags in it, and I can provide that to the social media bots. S3上的HTML文件可以包含用户特定的元标记,我可以将其提供给社交媒体机器人。 And when that file is accessed, I can have it redirect to my permalink. 当访问该文件时,我可以将其重定向到我的永久链接。 Does this seem reasonable? 这看起来合情合理吗? I can report back after trying this out. 尝试完之后我可以回复一下。

Another solution is to use a pre-rendering service, like https://prerender.io/ . 另一种解决方案是使用预渲染服务,如https://prerender.io/ It seems a bit bulky and black-boxed, but it might be more robust and reliable that my S3 solution above. 它似乎有点笨重和黑盒子,但它可能比我上面的S3解决方案更强大和可靠。 Any ideas? 有任何想法吗?

I am open to feedback and other solutions, and any suggestions would be great. 我愿意接受反馈和其他解决方案,任何建议都会很棒。


other relevant info: 其他相关信息:

This answer seems interesting, which involves activating the Facebook Javascript SDK: https://stackoverflow.com/a/34178987/1884158 这个答案似乎很有趣,涉及激活Facebook Javascript SDK: https//stackoverflow.com/a/34178987/1884158

And I am aware of tools like Twitter's Card Validator and Facebooks Share Debugger, here: https://cards-dev.twitter.com/validator https://developers.facebook.com/tools/debug/ which will be useful for testing any solutions. 我知道像Twitter的Card Validator和Facebooks Share Debugger这样的工具,这里: https//cards-dev.twitter.com/validator https://developers.facebook.com/tools/debug/这对测试很有用任何解决方案

And if it helps, my site is a React app that is bundled in HTML/CSS/JS and rendered entirely on the client. 如果它有帮助,我的网站是一个React应用程序,它捆绑在HTML / CSS / JS中并完全在客户端上呈现。

I haven't found a proper solution to this problem, but I did come up with a workaround/hack that involves setting up a separate service. 我没有找到解决此问题的正确方法,但我确实提出了一个涉及设置单独服务的解决方法/黑客攻击。 Here is the service that I came up with: https://github.com/mapseed/sharing/blob/master/share.js 以下是我提出的服务: https//github.com/mapseed/sharing/blob/master/share.js

If our static site is hosted at example.com , we can spin up a server running at sharing.example.com that accepts requests and will either redirect the request from sharing.example.com/my/route to example.com/my/route . 如果我们的静态站点在example.com上托管,我们可以启动在sharing.example.com上运行的服务器,该服务器接受请求,并将请求从sharing.example.com/my/route重定向到example.com/my/route But if the request has a user-agent HTTP header matching a social media crawler, as we are doing here , then we return an html page containing just the meta tags, as we are doing here . 但是,如果请求具有与社交媒体爬虫匹配的user-agent HTTP标头,就像我们在这里所做的那样,那么我们返回一个仅包含元标记的html页面,就像我们在这里所做的那样。

This approach has a couple of restrictions, however: 但是,这种方法有一些限制:

  • The links that we want to share on social media need point to our sharing.example.com subdomain. 我们想要在社交媒体上分享的链接需要指向我们的sharing.example.com子域。 This will make it possible for our service to return an HTML page with the proper meta tags if the request is coming from a social media bot, or otherwise redirect the request to our example.com website otherwise. 如果请求来自社交媒体机器人,或者以其他方式将请求重定向到我们的example.com网站,这将使我们的服务能够返回带有正确元标记的HTML页面。

  • In order to generate the content of our meta tags, our shareable links will have to have query params to pass all of the information to populate our meta tags. 为了生成元标记的内容,我们的可共享链接必须具有查询参数以传递所有信息以填充我们的元标记。 For example, our shareable link would look like this: sharing.example.com/post/my-post?title=my-title&desc=my-description . 例如,我们的可共享链接如下所示: sharing.example.com/post/my-post?title=my-title&desc=my-description This means that our shareable links can get quite long. 这意味着我们的可共享链接可能会很长。 You can see how we extract the information from our query params and inject it into our HTML template with the meta tags here: https://github.com/mapseed/sharing/blob/master/share.js#L18-L27 您可以看到我们如何从查询参数中提取信息,并将其注入带有元标记的HTML模板中: https//github.com/mapseed/sharing/blob/master/share.js#L18-L27

If these restrictions are acceptable, then this solution works quite well. 如果这些限制是可以接受的,那么这个解决方案效果很好。 Note that there is also a downside for SEO, because the links that will be shared are coming from sharing.example.com , and not our example.com website. 请注意,SEO也存在缺点,因为将要共享的链接来自sharing.example.com ,而不是我们的example.com网站。

Also, we will have to spin up an extra server to run this service. 此外,我们将不得不启动额外的服务器来运行此服务。 Although we should be able to migrate this implementation into a serverless architecture, like AWS Lambda. 虽然我们应该能够将此实现迁移到无服务器架构,例如AWS Lambda。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM