szurubooru
szurubooru copied to clipboard
OpenGraph embeds
It would be nice if sharing a link to a szurubooru post on supported platforms (Twitter, Facebook, chat clients etc) would embed a preview of the post. However, as far as I'm aware (not sure though) this requires the metadata to be embedded in the HTML, rather than added by client side javascript, so it might require the backend to also start serving frontend pages or something along those lines.
Here's my proposed design for enabling OpenGraph tags without changing the structure of the single-page app (SPA), based on the idea from https://stackoverflow.com/questions/50263513/redirect-crawlers-to-internal-microservice-in-nginx
- Request comes in from crawler / bot
- Nginx performs a match against the user agent (if it's not a crawler/bot, serve normally)
- For bots, redirect internally (as the reverse proxy) to
http://backend/opengraph/$1
- Server backend generates an HTML stub page with just the opengraph tags (this is where permissions will be handled - e.g. denying access if the default post access level is not public)
Step 2 is the part where I am not sure it's the best implementation, since matching the user agent is an evolving problem which needs to be updated for every service that OG tags would be displayed on. (For example the discordbot user agent is not handled in the stackoverflow post I linked: "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)"
)
Or we could make the frontend a server side app which serves static HTML except for all pages that have OpenGraph tags, for which it serves static HTML + the tags
@ioistired how would the frontend go about retrieving that content for the og tags from the backend server? AFAIK the current nginx config has no serverside processing going on at all right now, so what would that additional code look like?
There'd be a backend server for the frontend. index.htm currently looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta content="width=device-width,initial-scale=1,maximum-scale=1" name="viewport"/>
<meta content="#24aadd" name="theme-color"/>
<meta content="yes" name="apple-mobile-web-app-capable"/>
<meta content="black" name="apple-mobile-web-app-status-bar-style"/>
<meta content="#ffffff" name="msapplication-TileColor"/>
<meta content="/img/mstile-150x150.png" name="msapplication-TileImage"/>
<title>
Loading...
</title>
<!-- snip -->
</head>
<body>
<!-- snip -->
</body>
</html>
i'm proposing we set up a simple little web server in Python that normally continues to serve that, but when requesting something it can generate OpenGraph embeds for, it additionally requests info about the URL from the backend and adds it to the <head>
element. Does that make more sense?
I have implemented the approach I described above as #421. Here's a screenshot:
Note that Twitter video embeds are still a work in progress. Currently they're cut off for some reason: