Repository navigation
How to inject Open Graph/Meta tags for Social Media before the sendFile function? #7280
Replies: 3 comments 2 replies
|
Hi, I'd like to work on this issue. I think I can implement a solution by caching the HTML template and injecting dynamic Open Graph meta tags before sending the response. Please let me know if this approach aligns with the expected direction. |
|
The easiest thing that I can think of would be using a template (derived from your SPA's If you really want to use streams, then you can try using I'm not sure if serving open graph properties only to bots/crawlers makes sense, because while most will likely have |
|
Your option 1 is totally fine, it's what most SPAs do before moving to SSR. Read index.html once at startup, keep it in memory, and replace a placeholder per request. No need to touch sendFile. In the of index.html, add a marker: <!--OG_TAGS-->const fs = require("node:fs");
const path = require("node:path");
const template = fs.readFileSync(path.join(getWwwDir(), "index.html"), "utf8");
const escapeHtml = (s = "") =>
String(s).replace(/&/g, "&").replace(/</g, "<")
.replace(/>/g, ">").replace(/"/g, """);
app.get("/videos/:id", async (req, res, next) => {
try {
const video = await getVideo(req.params.id);
if (!video) return res.status(404).send(template.replace("<!--OG_TAGS-->", ""));
const tags = `
<title>${escapeHtml(video.title)}</title>
<meta property="og:title" content="${escapeHtml(video.title)}">
<meta property="og:description" content="${escapeHtml(video.description)}">
<meta property="og:image" content="${escapeHtml(video.thumbnailUrl)}">
<meta property="og:url" content="https://yourdomain.com/videos/${encodeURIComponent(video.id)}">`;
res.set("Cache-Control", "no-cache");
res.send(template.replace("<!--OG_TAGS-->", tags));
} catch (err) {
next(err);
}
});A few details that matter: escape the DB values (a title with a |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
In short: can we transform the file to be sent while piping? This for social media 🤷♂️
Longer version:
I wonder what's the recommendation how to inject HTML meta headers for social media scrapers, before sending the
index.html, using sendFile.Because, my SPA application, used by thousands of users a week, I load video X with a GET request by ID. That video comes with a title and description. This using Express.js in routes. Once loaded over DB, I want to set the ogDescription meta tag with its description, for example. But how?
Facebook comes with this tool to check:
developers.facebook.com/tools/debug
It requires a static HTML file. Before, I was using this npm package react-metatags-hook which was a bad idea. Because that Facebook scraper tool requires a plain HTML file.
I know, SSR or SSG would be the ideal solution. But I don't have the time to migrate my app to SSR/SSG right now. That would be a big step and requires a lot more testing and is too risky.
The current code to serve the static index.html for the SPA is:
This index.html does not come with
og:descriptionfor example because it's content/video based. I want to add it dynamically for bots/scrapers only.Some ideas
sendFile, open the original file, alter it, inject more HTML meta headers and send that (feels too complicated and poor performance)sendFilefunction: a transform stream. Customisable. Inject HTML headers etc.Perhaps this has been raised, discussed before?
Ideas welcome. Thanks :)
All reactions