当我通过Facebook“ www.testurl.com/profile”上的链接时,我没有获得正确的元数据。我正在使用React头盔组件,并且尝试添加{og:title和og:decription}无效。那么,我该怎么办?
答案 0 :(得分:2)
您应该在服务器上渲染网站,因为Facebook Bots不会渲染您的React应用。他们只是看到您的空应用程序,因此当社交媒体机器人访问您的网站时,您必须呈现您的应用程序并发送字符串。我们通过一些nginx配置和预渲染包来实现。您可以尝试。
默认预渲染使用4201端口
server {
listen 80;
listen 443 ssl;
server_name yourservername.com;
root /var/www/build;
index index.html;
location / {
try_files $uri @prerender;
}
location @prerender {
set $prerender 0;
if ($http_user_agent ~* "googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
set $prerender 1;
}
if ($args ~ "_escaped_fragment_") {
set $prerender 1;
}
if ($http_user_agent ~ "Prerender") {
set $prerender 0;
}
if ($uri ~* "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)") {
set $prerender 0;
}
#resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
resolver 8.8.8.8;
if ($prerender = 1) {
proxy_pass http://127.0.0.1:4201/render?url=https://www.yourwebsite.com$request_uri;
}
if ($prerender = 0) {
rewrite .* /index.html break;
}
}
}