Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing on social media, the URL does not render any meta data

We have built a project (Web Application) in React .net core using react in client-side rendering.

We've used react-helmet for dynamically assigning meta tags.

The issue being when the app renders in the browser. The browser gets only the static HTML on initial load which does not include the dynamic meta tags we have set. However on inspecting you get those meta tags under "Elements".

Also, if we use these URL for sharing on any social media, like WhatsApp or Facebook, the URL does not render any metadata as it should.

Tried searching for solutions to our problem, the most obvious answer we came across was to try server-side rendering instead. We get that, but it is not a solution to try out at this juncture when we're ready with app to roll it out.

Others we came across were "react-snap", "react-snapshot", but no luck with react-snap, it requires to upgrade React's version to 16+, which we did but I guess not all dependencies were upgraded, there was an error saying "

hydrate is not a function

(hydrate concerns the react-dom)

With react-snapshot, we could not find the necessary type definition, which is required in react .net core to function properly

Please guide for the next probable step (except the paid ones like prerender, etc)?

Main goal: Social Applications should render the meta data when we paste/share the URL within them.

like image 967
Prasaad Patil Avatar asked Jun 21 '18 12:06

Prasaad Patil


People also ask

How does social media use metadata?

Metadata provides information about digital data. In other words, it's the data about data. As an example, the metadata of a social media post would include information about the author of the post, the message type, post date and time, versions, links (un-shortened), location, likes, and comments.

What is social meta data?

Social meta tags allow website owners to have some control over what content shows up when a web page is shared on social media platforms (Facebook, Twitter, LinkedIn, etc.). Social meta tags, also commonly referred to as open graph meta tags, rely on the Open Graph Protocol.

What is the use of react helmet?

React Helmet is a component to dynamically manage the document's head section. Some common use cases include setting the title , description , and meta tags for the document. When combined with server-side rendering, it allows you to set meta tags that will be read by search engines and social media crawlers.


3 Answers

Prerender is the only solution. I used a node dependency called "prerender" -> https://github.com/prerender/prerender

It works enabling a web server wich make http requests. Assigning value to a boolean: window.prerenderReady = true; in your website tells your server when the page is ready to "take the photo" and it returns the Html when so. You need to program an easy script that parses all the site urls and save those html contents to files. Upload them to your server and using .htaccess or similar target the crawlers external-hit-facebook,twitterbot,googlebot, etc.. to show them the prerendered version and 'the real site' to the rest of user-agents.

It worked for me.

like image 50
Becario Senior Avatar answered Sep 20 '22 10:09

Becario Senior


The meta tags for Open Graph need to be present in the HTML which is sent back to the client when fetching a URL. Browsers or bots will not wait until the app is rendered on the client side to determine what the metatags are - they will only look at the initially loaded HTML.

If you need the content of your Open Graph metadata to be dynamic (showing different content depending on the URL, device, browser etc.) you need to add something like react-meta-tags into your server code.

There are no type definitions available for any of the react meta tags libraries, but you can add your own. It can be a bit tricky, but check out the official documentation and the templates they have provided to get started.

If you don't need it to be dynamic, you could add the tags into the static parts of the <head>-tag in your index.html.

like image 40
maxpaj Avatar answered Sep 17 '22 10:09

maxpaj


I had the same issue today. I had two React Web applications that need this. Here is how I solved it:

  1. put your preview image in the public folder
  2. still in public folder, Open index.html, add the line <meta property="og:image" content="preview.png"/> or <meta property="og:image" content="%PUBLIC_URL%/preview.png"/>.

Go to https://www.linkedin.com/post-inspector/ to check if it works.

I hope this would help!

like image 43
yukiyao Avatar answered Sep 20 '22 10:09

yukiyao