'Implementing oEmbed discovery with a static link

The oEmbed spec requires a site to link to its oEmbed endpoint and encode the current URL in that link. This is quite annoying for static/CDN-served websites that have to now encoding/return the request URL into the HTML response.

I'm wondering if it is known whether major oEmbed consumers (e.g. Slack, Facebook, or oEmbed client libraries) will add this URL themselves when requesting, so much so that it may be reasonable, in practice, to break the spec and do this statically. Any examples of a static implementation could be insightful.

Dynamic: Link: <http://flickr.com/services/oembed?url=http%3A%2F%2Fflickr.com%2Fphotos%2Fbees%2F2362225867%2F&format=json>; rel="alternate"; type="application/json+oembed"; title="Bacon Lollys oEmbed Profile"

Static: Link: <http://flickr.com/services/oembed?format=json>; rel="alternate"; type="application/json+oembed"; title="Bacon Lollys oEmbed Profile"



Solution 1:[1]

I implemented discovery using the html tag alternative (as opposed to the Link header).

Our frontend services are deployed by having an NGINX container serve up static build files. I wanted to add oEmbed discovery to all responses, so I used ndk_http_module and ngx_http_set_misc_module to create an escaped URI variable then inject it as a tag at the end of the html element. My experience after playing around with it for a few days on platforms like Slack and Teams is that including the URL query parameter and the format haven't conflicted so far.

Our frontend services are deployed by having an NGINX container serve up static build files. I wanted to add oEmbed discovery to all responses, so I used ndk_http_module and ngx_http_set_misc_module to create an escaped URI variable then injected it as a tag at the end of the html element like so:

server {
  listen 80;
  set_escape_uri $escaped_uri $http_host$request_uri;
  sub_filter '</head>' '<link rel=\"alternate\" type=\"application/json+oembed\" href=\"${OEMBED_URL}?format=json&amp;url=https%3A%2F%2F$escaped_uri\" title=\"Bacon Lollys oEmbed Profile\"></head>';
  ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikhil Shinday