# Social Network Sharing for a Single Page Application source: https://developer.mastercard.com/blog/social-sharing-for-a-single-page-application/index.md Authors: Kenny Chow. A few months back we decided to introduce a blog section on Mastercard Developers. Like any other product feature, we built our UI in an iterative manner focusing on content first. After testing with a few blog posts we decided we should add social network share buttons. It sounds simple but it turned out to be more difficult than we initially thought. The first time we tried to share a blog post on LinkedIn, it looked something like this: As you can see, LinkedIn was unable to retrieve the title, author and other information about the post, resulting in a very plain preview. We never had to deal with this issue before because being a developers' website, our links were hardly shared on social network platforms. One big feature of a blog is for it to be actively read and shared by communities via posts and tweets. To make our blog more visible, we needed social network platforms to generate a better preview of our posts. We discovered the problem very quickly. Social network platforms generate these previews using the Open Graph protocol . This requires special meta tags to be present in the Head element of our page that identifies the social elements of a page such as title, short description, author, date of publication, etc. Since we built our website as a Single Page Application using React, a static HTML page is first loaded no matter which page you requested. The content is then dynamically retrieved and rendered using Javascript which runs on the client browser. However, when a social media bot requests that same page to generate a preview, it does not run the Javascript code and thus ends up with a plain generic preview of the page. The solution was pretty evident too; we needed to return the HTML page with all those meta tags customized for each page rather than the same generic code for all pages. Being a React Single Page Application website, we cannot simply do this because the HTML portion of the page is static. So we had to choose one of the following approaches: Conditional redirect: using the web proxy to redirect bots to different server side endpoints. Server-side rendering: the page will be dynamically generated on the server, typically within a Node application. Pre-rendering: inserting an intermediary between the client and the server using a headless browser. The headless browser runs all the Javascript code and then returns rendered content to the client. After evaluating the pros and cons of all 3 approaches, we decided to go with approach #1: using conditional redirect of our web traffic. This solution allows us to return dynamically generated content specifically for bots and leaves the existing flow for normal users as is. We chose this solution because it has the least impact on our existing infrastructure, code and user experience. It also allows us to scale this solution out quickly by reusing the existing caching and load balancing infrastructure. To enable conditional redirects, we needed to work on two things: Web proxy configuration Server side API The initial request for a page would look like this: Web Proxy Configuration The web proxy is the initial entry point for all web requests. So, we needed to add a configuration in our web proxy to identify requests coming from bots and then proxy those requests to a new server side API that will serve custom generated HTML code. Config # Example rewrite rule for conditional redirect of user agents RewriteEngine On # Check if request is coming from a bot based on user agent RewriteCond %{HTTP_USER_AGENT} facebookexternalhit\|twitterbot\|linkedinbot \[NC, OR\] RewriteRule \^(.\*)$ https://developer.mastercard.com/backend/api/content?id=$1 \[P,L\] Server Side API Once we can get the bot's traffic redirected to the new server side API endpoint, we can start handling the request in our backend. The API gets the original requested URL via a query parameter, which is then used to fetch the requested content from our content management system and generate some HTML code. We just need a barebones HTML that includes only specific parts of the page populated. There is no need for any Javascript or styling. This specially formulated page is then returned to the bot to consume. Example HTML code returned for a blog post: Html Partner with Mastercard to deliver great consumer experiences through APIs Validating the Solution Some major social network platforms provide tools to test if our solution works. Let's try a couple of them to see how it looks. Facebook https://developers.facebook.com/tools/debug/sharing/ LinkedIn https://www.linkedin.com/post-inspector/ Twitter https://cards-dev.twitter.com/validator Now if you share our blog posts on any popular social network platform, it generates a much better preview. Try it yourself! Special thanks to Naman and Senxian for their contributions to this blog post.