-1

I'm using Angular11 in my current application and I'm using Title service to update the page title. But I can achieve the same using document.title="".

In my case, using Angular Title service is really beneficial? Or I'm just increasing my bundle size by importing the unnecessary Title service?

Angular documentation says - Angular Title is helpful if the application into different platform. And importing Title service costs my bundle 6.7KB.

Can I go with document.title? I'm looking for suggestions.

  • `And importing Title service costs my bundle 6.7KB.` Are you sure? The Title Service is really a simple class with 2 very short methods. I would be surprised if in a production build the bundle size could increase more than 1KB at most. Are you optimizing the build? Or is it just the reported size pre-minification? – Alberto Chiesa Jun 01 '21 at 08:31
  • I have used import cost VS Code plugin. It shows 6.7KB is getting imported. I believe in production build it would cost at least 1 or 2KB. Because the Title service internally importing getDOM. – Lakshmikanth Vallampati Jun 01 '21 at 10:23
  • "I believe"is not enough. If you look at the code of the service, it is at most 200 bytes. getDOM is required also by other modules (in any non trivial application), so it should not be counted twice. Try to build: you'll see you don't have any problem. I suspect the VS Code plugin should be used with a grain of salt – Alberto Chiesa Jun 01 '21 at 10:29

1 Answers1

1

It's always better to use Angular specific services to achieve component level configurations.

Though it might cost some bundle size, you will get features like getter and setter of the service.

In case of document, you are actually setting the title for entire doc but not just for specific component. Read more about title service here - https://angular.io/api/platform-browser/Title

Ganesh
  • 5,808
  • 2
  • 21
  • 41
  • Please read more about the Angular Title service codebase and try to understand how it works. We have already done lot of analysis. I'm just asking for Angular experts opinion. – Lakshmikanth Vallampati Jun 01 '21 at 10:17
  • Hope in your analysis is helpful to find that document will modify entire DOM object where it's not recommended in angular, so it's adding services like Title and DOCUMENT and so on.. ;) – Ganesh Jun 02 '21 at 05:51