How do we add/update meta tags dynamically so that they get picked by Facebook/Whatsapp share dialog?
I upgraded my angular 2 application to angular 4 in order to use Meta service to add/update meta tags dynamically once we get the data in component from API.
So far in my component, I have
this.metaService.updateTag({ property: 'og:title', content: pageTitle }); this.metaService.updateTag({ property: 'og:url', 'www.domain.com/page' }); this.metaService.updateTag({ property: 'og:image', content: coverUrl, itemprop: 'image' }); this.metaService.updateTag({ property: 'og:image:url', content: coverUrl, itemprop: 'image' }); this.metaService.updateTag({ property: 'og:image:type', content: 'image/png' });
I am using updateTag because I have added static tags already with default values. This code successfully updates the meta tags values when I inspect them.
I know it makes sense that Facebook/Whatsapp debugger tools doesn't execute any javascript so it won't ever probably be executed in their environment.
I'm using https://developers.facebook.com/tools/debug/
and it always picks up the default tag values which makes sense.
My question is, what is the way around so that Facebook/Whatsapp picks up the updated tag values dynamically? I'm using Angular 4 and loading all data via API calls so its not possible to get any sort of data before the page loads and script is executed.
You need to provide a static html page containing the the open graph tags like og:image og:title and og:description in the html sourcecode, since facebook, twitter and co a just scraping the plain html without rendering it through javascript. Angular dynamically updates the dom through js and therefore the crawlers just get the initial index.html.
There are several ways to serve an html containing open graph tags ans solve your issue:
I guess you already use something like ngx-meta to add og tags?
I guess server-side rendering is the most appropriate way to solve your issue. For this you can host a node server or use eg. AWS Lambda. The disadvantage with this, your app has to be actively hosted and can not be served statically anymore. Anyways this seems to be the best way since it also is improves SEO. Angular Universal is the term to search for:
You can also prerender specific routes on the build process and serve angular as a static app with multiple prerendered index.html files. If you only have few static routes this works perfectly fine. Thinking of more generic routes with dynamic parts, this is not the solution. Go for server side rendering. The angular universal boilerplate also contains an example for this. See prerender.ts
If you like to avoid implementing serverside / prerendering during the build process (setting up angular universal sometimes is a pain for not good structured apps.) you can try to use a proxy service prerendering you page. Take a look at eg. prerender.io.
Redirect all requests to an script which overwrites the og:tags. Eg. Using PHP and .htaccess to overwrite og tags this is possible with modern environments too. Eg. you could use cloudfront / api gateway and a lambda function. Have not seen an example for this though.
Be aware that caches may still have cached the open graph information from their first crawl. Ensure your source code is the lastest and all caches, reverse proxies like nginxx, cloudfront are cleared.
Use Facebook Debugger to debug open graph caches and clear facebook opengraph cache
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With