How to dynamically change meta tags before the site is scraped in Angular 2

0 votes

How to dynamically change meta tags before the site is scraped in Angular 2?

How can I dynamically change meta tags in my Angular 2 application before the site is scraped by search engines or social media bots? I'm looking for a way to ensure that the correct meta information is presented when the page is crawled. What techniques or libraries should I use to achieve this?

Oct 25 in Web Development by Nidhi
• 2,660 points
104 views

1 answer to this question.

0 votes

To dynamically change meta tags before Angular 2 site is scraped,
 
we can use the following approach : Create a service first then inject the service into the components then call the service’s methods to update the meta tags with the appropriate values based on the current page or user interaction. 
This is done in the component’s lifecycle hooks or in response to user events. 
After that the service’s methods will update the meta tags in the DOM by accessing the document.head element and modifying the <meta> tags. 

This can be done by creating new <meta> tags with the desired attributes or by updating the existing ones.

Let’s take the example :

mport { Injectable } from '@angular/core';

@Injectable({

  providedIn: 'root', // This won't work in Angular 2, so remove it for Angular 2

})

export class MetaTagsService {

  updateTitle(title: string) {

    document.title = title;

  }

  updateDescription(description: string) {

    const descriptionMeta = document.querySelector('meta[name="description"]');

    if (descriptionMeta) {

      descriptionMeta.setAttribute('content', description); // Correct DOM manipulation method


/*Reasons to use setAttribute(): When you directly set name or content as properties of the meta element, you are modifying DOM element properties, not necessarily the HTML attributes. For most HTML elements, these properties correspond to attributes, but this is not always true for all elements.*/

    } else {

      const newMeta = document.createElement('meta');

      newMeta.setAttribute('name', 'description');

      newMeta.setAttribute('content', description);

      document.head.appendChild(newMeta);

    }

  }

}
answered Nov 6 by kavya

Related Questions In Web Development

0 votes
1 answer

How to load external scripts dynamically in Angular?

Hello @kartik, You can use following technique to ...READ MORE

answered Sep 8, 2020 in Web Development by Niroj
• 82,840 points
5,410 views
0 votes
0 answers
0 votes
1 answer

How can I remove a port from url for node app using nginx

If you run your node server on ...READ MORE

answered Apr 10, 2018 in DevOps on Cloud by ajs3033
• 7,300 points
4,026 views
0 votes
4 answers

ReactJS vs Angular Comparison: Which is better?

Parameters React Angular Type React is a JavaScript library, and it ...READ MORE

answered Jan 7, 2021 in Events & Trending Topics by Focusteck
• 140 points
1,729 views
+4 votes
9 answers

***IMPORTANT*** AngularJS Interview Questions.

Yes, I agree with Omkar AngularJs is ...READ MORE

answered Mar 17, 2019 in Career Counselling by Sharad
• 180 points
3,613 views
0 votes
1 answer

How to pass props to {this.props.children}?

Hello @kartik,  Try using this: <div> ...READ MORE

answered Jul 22, 2020 in Angular by Niroj
• 82,840 points
3,502 views
0 votes
1 answer

What is the command to merge branches in Git?

To merge branches in Git you can ...READ MORE

answered Nov 6 in Web Development by kavya
41 views
0 votes
1 answer

How to use logger in Java?

Loggers in Java are objects which trigger ...READ MORE

answered Nov 6 in Web Development by kavya
68 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP