Single Page Applications and Open Graph Single Page Applications and Open Graph ajax ajax

Single Page Applications and Open Graph


The way I handle this is to create a dynamic page which I use as my open graph object, which is simply populated from the url parameters and redirects back to my SPA using the meta redirect.

<meta http-equiv="refresh" content="0;URL=http://YOUR_WEBSITE_WITH_DYNAMIC_CONTENT">


Thanks to this tutorial I found a solution: https://speakerdeck.com/sienatime/facebook-sharing-for-single-page-apps?slide=15

I send only meta tags if the user-agent of the http request includes "facebookexternalhit".

Here is some code for a backend with node and express:

app.get('/', (req,res) => {  if(req.header('user-agent').includes('facebookexternalhit'){    res.send(`      <meta property="og:title" content="The Slits' Cut" />    `);  } else {    res.sendFile(index.html);  }  })

You could also write your own middleware for that.


Our solution was to use Netlify pre-rendering, which pre-renders and then caches the rendered HTML to serve to crawlers and bots specifically.

This seems to work well. It allows us to dynamically update the OG meta tags, which are then cached by Netlify and served to crawlers, so they see the correct content.

It was easy to setup too, so if you are using Netlify this may be a good solution for you.