Should og:image and og:url put in <meta> or <link>?

我与影子孤独终老i 提交于 2019-12-11 04:49:33

问题


For og:image and og:url, since they have URL, can I place them in a link tag instead of a meta tag, and is it preferable?

Also what is the difference in using these two tags w.r.t. og:image and og:url?


回答1:


I don't know about other major consumers of the OGP so this might not be a problem for your case, but if you plan to implement it for Facebook,

stick to meta tags.

Doing a quick live test with the open graph debugger and fiddling with the og: tags, it appears that facebook only recognizes them if they are placed in <meta>s.

Try it

If you create an empty HTML file with a single og:url tag in its <head> section, upload it to any URL and run it through the debugger above, both

<link rel="og:url" href="http://www.imdb.com/title/tt0117500/" />

and

<link property="og:url" content="http://www.imdb.com/title/tt0117500/" />

edit or, as suggested in the comments

<link property="og:url" href="http://www.imdb.com/title/tt0117500/" />

will fail to parse and return the original URL, while

<meta property="og:url" content="http://www.imdb.com/title/tt0117500/" /> 

parses correctly and returns the contents of The Rock on IMDb instead. At the end of the day semantics are nice, but working code is even better.




回答2:


You must use link instead of meta if the value is a URL.

From the definition of the meta element:

The meta element represents various kinds of metadata that cannot be expressed using the […] link […] elements.

But a URL can of course be expressed using a link element (as it’s its purpose).

If you use a meta element for a URL value, RDFa parsers would extract a string value instead of a URL value. This also means that they wouldn’t be able to apply relative references against the base URL.

Example

Let’s say these elements are in a document with the URL http://example.com/foo:

<link property="schema:url"    href="/bar" />
<meta property="schema:url" content="/bar" />

The value from the link element is the URL http://example.com/bar.
The value from the meta element is the string /bar.

However, in the case of OGP …

The examples in OGP’s documentation use the meta element for URL values; there is no example with a link element.

While this means that many of their examples are invalid HTML+RDFa, and RDFa parsers will extract strings instead of URLs in those cases,
it may be the case that Facebook (or any other consumer) isn’t actually using RDFa to parse the content, and it may be the case that they don’t support link elements.

Note: These are just possibilities, judging from the quality of their documentation. I don’t have any experience/insight, as I don’t use Facebook. So you might want to test it with Facebook if you care about their OGP-based features.



来源:https://stackoverflow.com/questions/41915689/should-ogimage-and-ogurl-put-in-meta-or-link

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