Generating Dynamic meta Tags on the Server
Since Create React App doesn’t support server rendering, you might be wondering how to make <meta>
tags dynamic and reflect the current URL. To solve this, we recommend to add placeholders into the HTML, like this:
<!doctype html>
<html lang="en">
<head>
<meta property="og:title" content="__OG_TITLE__">
<meta property="og:description" content="__OG_DESCRIPTION__">
Then, on the server, regardless of the backend you use, you can read index.html
into memory and replace __OG_TITLE__
, __OG_DESCRIPTION__
, and any other placeholders with values depending on the current URL. Just make sure to sanitize and escape the interpolated values so that they are safe to embed into HTML!
If you use a Node server, you can even share the route matching logic between the client and the server. However duplicating it also works fine in simple cases.