Skip to main content

Prevent Google from indexing Angular components

The problem

Our web page is multilanguage, server-side render Angular application, and creating a JavaScript file that will dynamically change content (like SPA does) will be a hard thing to do. Some of the problems:

  • We need to copy existing text from JSON files that we use for translation. And we need to keep in mind to update both places when changing the content

The solution

We need a solution for hiding content or whole components wherever we want in Angular Web application. This way we could keep using our Angular components and Angular features like before and hide contents from Google bots.

TLTR;

  • Create folder for JavaScript files

Create folder for JavaScript files

Create folder hidefrombot in /src where we will add our dummy JavaScript file that we will use to determine if the agent is a robot or not. You can add a hidefrombot folder somewhere else, but you will need to adjust angular-cli.json different also.

Hide folder from Google bot

To disallow a Google bot to see out the script we need to hide the whole folder. We can do that by adding restriction in robot.txt file:

Disallow: /hidefrombot

If we don’t have that file, create it.

Create dummy isRobot.js file

This dummy script will be used just to see if the browser agent can access it. If the browser agent can’t execute isRobot.js then the agent is a robot. This file can be empty, or add some comment, or whatever. We do not care what is in a file, we just want to know can it be executed or not.

Add folder in angular-cli.json

To make sure that our isRobot.js is present after the application is built and deployed we need to add it like an asset. We can do that by adjusting the angular-cli.json file. There we need to add our folder in the asset part. Something like this:

{
  "project": {
    "name": "hide.from.google"
  },
  "apps": [{
    "root": "src",
    "outDir": "dist",
    "assets": [
      "assets",
      "favicon.ico",
      "robots.txt",
      {
        "glob": "*.xml",
        "input": "config/sitemaps",
        "output": "./"
      },
      "denyrobots"
    ]
  }]
}


If you created your folder somewhere else, you need to adjust your angular-cli.json file.

isRobot method and how to use it in component

We should create a service for this method or add it to our existing service. The best way is to do a one-time check-in our app, and use that information through our whole app. That way, we will try to get isRobot.js file only once and our app will stay optimized.

Assume that we have a service called SeoOptimisationService in seo-optimisation.service.ts file. Create a new method checkForCrawlingRobot and try to execute our script. Our service should also contain a flag variable that will show if the agent is Google bot or not.

isNotCrawlingRobot: boolean = false;isRobot () { const contentScriptElement = this.document.createElement(‘script’); contentScriptElement.src = ‘/ hidefrombot / isRobot.js’; contentScriptElement.async = true; contentScriptElement.onload = () => {  this.isNotCrawlingRobot = true; }; this.document.head.appendChild(contentScriptElement);}

This method will add isRobot.js in our head and try to execute it. If this happens onload event will fire and there we can change our variable.

To keep our app optimized we should call this method in our app.compoment.ts file in ngAfterViewInit() life circle. This way, calling this script won’t block our app to render and we won’t get negative points from Google. Calling our service:

// check if browser agent is a crawling robotthis.seoOptimisationService.isRobot();

The last thing we need to do is to use this service in our component. Simple as that.

Just add seoOptimisationService to our component and use isNotCrawlingRobotflag in our template.

<div *ngIf=”seoOptimisationService.isNotCrawlingRobot” class=”info-content-box”><p> Our customer service is open, the store will be open again from the end of April due to the coronavirus. </p></div>

You may wonder: what if the component is rendered before we get onload event, because we do not know when will get a response from that call. Well, we do not have to worry. When variable isNotCrawlingRobot is changed in service, it will be changed also in our component and everything will be fine.

Now, we can hide whatever content or component in our Angular application using only one service! Nice, right?





Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...