<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ayush Thakur on Medium]]></title>
        <description><![CDATA[Stories by Ayush Thakur on Medium]]></description>
        <link>https://medium.com/@just-another-developer?source=rss-fee096957eba------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qZk0Zee2kLp0fis1eoF_Hg.jpeg</url>
            <title>Stories by Ayush Thakur on Medium</title>
            <link>https://medium.com/@just-another-developer?source=rss-fee096957eba------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:10:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@just-another-developer/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dynamic/Manual lazy loading in Angular]]></title>
            <link>https://just-another-developer.medium.com/dynamic-manual-lazy-loading-in-angular-b5851fc75e0c?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5851fc75e0c</guid>
            <category><![CDATA[dynamic-module]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[ivy]]></category>
            <category><![CDATA[dynamic-components]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Sun, 10 Jul 2022 13:04:45 GMT</pubDate>
            <atom:updated>2022-07-10T13:08:06.437Z</atom:updated>
            <content:encoded><![CDATA[<p>Lazy Loading as a concept simply means to load resources when it is required. This can speed up the initial load time of our application since we only get what we need at that particular moment.</p><p>The most common way of lazy loading in angular is through use of routes where we specify a dynamic import statement with a path to the module. Based on our route config angular decides when to lazily load the module during runtime.</p><p>The scenario we will be covering is initiating lazy loading of modules and assigning component to the view programmatically (for example with a button click)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7A7BZrpF0wOUpCzM" /><figcaption>Photo by <a href="https://unsplash.com/@rstone_design?utm_source=medium&amp;utm_medium=referral">Ryan Stone</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>prerequisites to better understand this article</h3><ul><li>lazy loading in angular through routes.</li><li>working of ViewChild.</li></ul><h3>The Sample Application</h3><p>We have a simple application with a single button.</p><p>On click we want to lazy load a module over the network, instantiate a component defined in it and assign it to the view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/148/1*JrwMDIpR6cgl9MX9AxdtrQ.png" /><figcaption>button to lazy load module</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-PQrDImVqpg4GpOOVdDjAw.png" /><figcaption>View after lazy loading is successful</figcaption></figure><p>On click of the button, we see a console log coming from the constructor of the lazy loaded module and the component is shown below the button.</p><h3>The Code</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*tx7FhJHPlKcXp7AzaPBHiQ.png" /><figcaption>folder structure</figcaption></figure><p>We have the app component and a service along with a lazy loaded module with its own component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*igC9spW0qwPZ1RvcfOqYZg.png" /><figcaption>app.component.html</figcaption></figure><p>Here we just define a button with a click listener and an ng-container within which we will show the lazy loaded component.</p><p>Note — We could have lazy loaded the component directly as well but in real scenario there is always a dependency on other packages even for simple components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uR-0u7hGF3vDMGcTZX9LlQ.png" /><figcaption>app.component.ts</figcaption></figure><p>Here, using viewchild, we get the reference inside which we will show our lazy component.</p><p>We have moved the code that handles instantiating the module to a service. we pass the dynamic import statement with path to our lazy loaded module. This syntax should look familiar if you have created lazy loaded routes.</p><p>We also pass the container reference to our helper function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ykW0IF66gy_SRFIIxhzY8w.png" /><figcaption>app.service.ts</figcaption></figure><p>The modulePath is resolved as a promise and we use compileModuleAsync which compiles the given NgModule and all of its components.</p><p>post compilation, we get the moduleFactory instance using which we can resolve our lazy component. once that is done, using the container reference we can attach our component to the container.</p><p>Note — if clear() is not done, then each time button is clicked lazy component will keep on appending to the previous view in the container. Also, the above code works for Ivy Compiler which is default in Angular 9+ versions.</p><h3>Conclusion</h3><p>With all steps mentioned above, we can lazy load modules with all dependencies in various scenarios ranging from simple button clicks to complex use cases like conditional loading on module federation, user authorization etc.</p><p><a href="https://github.com/Hulk914/dynamic-loading">GitHub - Hulk914/dynamic-loading: lazy loading modules and components programmatically</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5851fc75e0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating custom HTML Elements with NgRx Forms]]></title>
            <link>https://just-another-developer.medium.com/creating-custom-html-elements-with-ngrx-forms-4650183d11d3?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/4650183d11d3</guid>
            <category><![CDATA[angular-forms]]></category>
            <category><![CDATA[custom-html]]></category>
            <category><![CDATA[ngrx-form]]></category>
            <category><![CDATA[ngrx]]></category>
            <category><![CDATA[ngrx-from-view-adapter]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Sun, 15 May 2022 10:03:27 GMT</pubDate>
            <atom:updated>2022-05-15T10:09:18.851Z</atom:updated>
            <content:encoded><![CDATA[<p>This article will help you create custom form components in Angular that work in sync with NgRx form controls. The most common example can be a star rating component where in the feedback form you can rate by clicking on the stars.</p><p>We will consider a simpler example so that we don’t have a lot of HTML and CSS code, since that is not the objective. After finishing this article you can easily extend the functionality in any way you want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3N-ON2X6id-vR2rO" /><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=medium&amp;utm_medium=referral">Kelly Sikkema</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>prerequisites to better understand this article</h3><ul><li>Understanding of NgRx &amp; NgRx Forms is essential since we wont be covering it in this article. Please go through below links to gain better understanding on above topics.</li></ul><ul><li><a href="https://just-another-developer.medium.com/beginner-level-guide-to-ngrx-ngrx-forms-422403ef2018">Beginner level guide to NgRx &amp; NgRx Forms</a></li><li><a href="https://awstip.com/creating-custom-validators-with-ngrx-forms-64b9c07caa17">Creating custom validators with NgRx Forms</a></li></ul><h3>Example App</h3><p>We are going to create a custom input component that works with form controls and adds any suffix provided, to the input view.</p><p>The form control always stores just the value. Our custom component will handle showing the suffix (in this example a % symbol) when the user is not actively typing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*JQN8jQcqt7TIm0gCZrsNqQ.png" /><figcaption>Initial Value of Form Control is 100</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*SmKlSETYB_sc6ug4K-a9Jw.png" /><figcaption>while typing, the % vanishes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*rpBIdFbJhqAiGYkm7MJZKg.png" /><figcaption>returns on blur</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*da6PLRsZS6CoQusxkJ2Qgw.png" /><figcaption>value of formControl is without % symbol</figcaption></figure><h3>Creating the Custom Component</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MX2-MNuZreNHloZR0rMr4A.png" /><figcaption>custom-input.component.html</figcaption></figure><p>The HTML can have anything, it need not be a form element depending on the requirement. For a star rating component you can have 5 divs with star shape CSS. In that scenario the trigger would be click events instead of input value changes.</p><p>The main code to link the form control with any operation on our html content will be in the typescript file of the component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*CYdTC4WwnrVGnTcWDlaCkA.png" /><figcaption>custom-input.component.ts</figcaption></figure><p>We can divide the code as follows —</p><h4>Dependency Injection of NGRX_FORM_VIEW_ADAPTER:</h4><p>for the token ‘NGRX_FORM_VIEW_ADAPTER’ we want to provide our existing CustomInputComponent.</p><p>ForwardRef is used to handle cases when out component may not have been initialized before use.</p><p>multi: true is used because the same token now provides different values.</p><p>If you need more clarity on ForwardRef or multi: true there are lots of good articles on the internet. Feel free to Google it out!</p><h4>Scenario Based Code Section</h4><p>This can be any logic, its based on your scenario. In our case we need to show a suffix based on focus and value changes so we have methods related to those.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*RZVpASz-mbquZEgVh86kUg.png" /><figcaption>Example code</figcaption></figure><p>We are simply binding ‘value’ with ngModel to the native input (this will have the suffix added when required).</p><p>The ‘localValue’ keeps the numerical values of the input field without the suffix which is used when we focus into the input. (You can add any validation like allowing only number inputs, in the inputChanged method, for invalid values just make this.value = ‘’)</p><h4>Adapter Functions</h4><p>We implement the FormViewAdapter interface that gives us 4 methods</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*NdJFFpbmH-lTkY4YAILlWA.png" /><figcaption>adapter methods</figcaption></figure><p>These methods update the formControl value when required based on scenario. In our case the formControl should update when value changes in the input field.</p><ul><li>setViewValue: This is called when our custom component is first initialized. Use this to get initial value from the FormControl and update the html bindings accordingly.</li><li>setOnChangeCallback: use the callback function here to update the formControl value as per your scenario. In this case when input changes we will use this callback.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*iTOl9TT7Ffj6nR3zN51xrA.png" /><figcaption>the callback we receive in setOnChangeCallback()</figcaption></figure><p>It takes updatedValue from our html and does a setValue on the NgRx FormControl.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*tJ1coY79bdiKfL0AzRYgqA.png" /><figcaption>Assigning the callback</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*wKiFu9d6orkTGWYSisa_MQ.png" /><figcaption>Using the callback</figcaption></figure><p>The Touch and Disable callbacks work similarly. You can refer below official doc link for more info</p><p><a href="https://ngrx-forms.readthedocs.io/en/master/user-guide/custom-form-elements/">Custom HTML Form Elements - ngrx-forms</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_aVOTR6y7geS7jhiJw1CBw.png" /><figcaption>Parent component using our custom-input</figcaption></figure><p>Now we can directly use our ngrxFormControlState with our custom component.</p><h3>Conclusion</h3><p>If you go through the official docs, it mentions that NgRx Forms takes care of syncing with third party libraries out of the box by converting controlValueAccessor into FormViewAdapter but it fails in some scenarios.</p><p>In the above case or if we have to create our own in house HTML elements that can be used with formControls directly, this article should help you create such components.</p><p>Please find GitHub Link below</p><p><a href="https://github.com/Hulk914/ngrx-custom-validation">GitHub - Hulk914/ngrx-custom-validation</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4650183d11d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generic Angular directive to handle single and double clicks on the same element]]></title>
            <link>https://awstip.com/generic-angular-directive-to-handle-single-and-double-clicks-on-the-same-element-b6300b890964?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6300b890964</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular-directive]]></category>
            <category><![CDATA[directives]]></category>
            <category><![CDATA[doubleclick]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Tue, 19 Apr 2022 12:04:25 GMT</pubDate>
            <atom:updated>2022-04-20T11:19:07.509Z</atom:updated>
            <content:encoded><![CDATA[<p>HTML has a special event to handle double clicks which is: ‘dblclick’. The scenario becomes a little complicated though, when we need to have different operations for click and double click on the same div/button etc. In this article we will create an angular directive that can handle both clicks and double clicks for us without having to replicate the same logic in multiple components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*air6Fwh8pnnLT9JZ" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>prerequisites to better understand this article</h4><ul><li>basic knowledge on directives and hostlistener</li></ul><h3>The Directive</h3><p>The logic is pretty simple, we don’t want the click event to trigger when we intend to double click. Hence we will listen to both click and dblclick, but on click listener we will delay the execution by a small amount of time.</p><p>We then use a boolean flag to identify if while waiting, user did a double click. In this case we will cancel the click operation. Lets dive into the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*5f4oZUaUS5NVCcdFHePvkQ.png" /><figcaption>double-click.directive.ts</figcaption></figure><ul><li>2 hostlisteners for click and double click respectively</li><li>stopClick becomes true in case of double click and false when single click is done in future</li><li>the timer of 200ms starts on first click, meanwhile if second click comes, timer is discarded and hence only doubleClick works</li><li>2 separate emitters for single and double clicks used for clarity, you can also club into a single eventemitter and pass meta data as object along with the click event information. this.commonClick.emit({event: e, type: ‘doubleClick’}) but then conditions need to be added in the subscribing component.</li></ul><h3>Usage of the Directive</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*94fELR3wMNzXwPl9lWDqWA.png" /><figcaption>app.component.html</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*6L6oAPP8Z5_WPoZPVAoi1w.png" /><figcaption>app.component.ts</figcaption></figure><p>We attach the directive to the required element (button in this example). Then create different handlers for single and double click respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U75tlB3PjeeyOSuZjh5-QA.png" /><figcaption>output for single click and then double click</figcaption></figure><h3>Conclusion</h3><p>The same logic can be applied to the component directly if you only need this in a particular component but in general such tasks are better done in directives. It helps making the logic generic which can be extended later on if required. It also abstracts the actual logic to handle double clicks and keeps the code DRY.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6300b890964" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/generic-angular-directive-to-handle-single-and-double-clicks-on-the-same-element-b6300b890964">Generic Angular directive to handle single and double clicks on the same element</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dynamic routing on module load of an Angular Application]]></title>
            <link>https://awstip.com/dynamic-routing-on-module-load-of-an-angular-application-fb0d63451a55?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb0d63451a55</guid>
            <category><![CDATA[angular-routing]]></category>
            <category><![CDATA[usefactory]]></category>
            <category><![CDATA[dynamic-routing]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[route-config]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Wed, 02 Feb 2022 12:32:02 GMT</pubDate>
            <atom:updated>2022-02-03T05:49:38.912Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article we will take simple scenario to learn more about routing, dependency injection, making use of useFactory for providing dependencies during runtime etc. The same concept can be used to build more complex scenarios as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Kc3zaNZhF0-Oenyr" /><figcaption>Photo by <a href="https://unsplash.com/@martinirc?utm_source=medium&amp;utm_medium=referral">José Martín Ramírez Carrasco</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Prerequisites</h4><ul><li>Basic knowledge on angular routing and services</li></ul><h3>Scenario</h3><p>We will work with app.module.ts itself for simplicity. The requirement is that based on some logic we should decide whether to load TestComponent or DemoComponent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*McfdRvk_tKVfOLYZ8NTu9w.png" /><figcaption>Folder Structure</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/1*-zmGNE-qSsBZLD0OeXucAQ.png" /><figcaption>Application when TestComponent Loads</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*5-h9nk-0VcuOuwkKb5or1w.png" /><figcaption>Application when DemoComponent Loads</figcaption></figure><p>The route path is same for both, when the module loads we are deciding which component to load by changing the router config.</p><h3>Implementation</h3><p>Our decision here is made by a mock service that randomly decides which component to load. We can combine this for more complex scenario by making api calls in route resolvers to make such decisions based on backend data as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*E8NtBKxDCASgBF-4glqVvg.png" /><figcaption>service</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*BwvaliSHGw-PMbKOdq8BoQ.png" /><figcaption>app.module.ts</figcaption></figure><p>Here we are making use of few concepts that we will discuss below.</p><ul><li>using useFactory we have added the service as a dependency.</li><li>The service has the data based on which we decide which component to load</li><li>conditionally we return the route config by providing ROUTES using useFactory</li></ul><h4>ROUTES</h4><p>This is the Dependency Injection token used for router configuration. In angular when we request for some dependency it is searched through the token (in case of service that is the class name for example). Hence for routing configuration angular gets data from the token ‘ROUTES’ and if we update it, the new value is taken for the configuration.</p><h4>useFactory</h4><p>we use this to provide token values during runtime based on some logic. We can also add dependencies using ‘deps’ array. The order of deps should be same in the callback function as well. In our example we pass demoService as a dependency.</p><p>multi: true is required when same token can give back multiple values. In this case we are returning an additional update config, hence this needs to be set to true.</p><h3>Conclusion</h3><p>This is a very simple example, but using this concept the logic can be tweaked to real world scenarios wherein the decision is made from backend data received from api call or from some config file etc. The same logic applies for lazy loading configs as well.</p><p>The goal of the article was to clear the concept of dependency injection through useFactory and how we can update tokens like ROUTES with it.</p><p>LinkedIn — <a href="https://www.linkedin.com/in/ayush-thakur/">https://www.linkedin.com/in/ayush-thakur/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb0d63451a55" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/dynamic-routing-on-module-load-of-an-angular-application-fb0d63451a55">Dynamic routing on module load of an Angular Application</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tree Shakable Services and Lightweight Injection Tokens for optimizing Angular apps]]></title>
            <link>https://awstip.com/tree-shakable-services-and-lightweight-injection-tokens-for-optimizing-angular-apps-fc5d5eb177b3?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc5d5eb177b3</guid>
            <category><![CDATA[tree-shaking]]></category>
            <category><![CDATA[angular-optimization]]></category>
            <category><![CDATA[build-size]]></category>
            <category><![CDATA[build-tool]]></category>
            <category><![CDATA[injection]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Sun, 30 Jan 2022 08:20:51 GMT</pubDate>
            <atom:updated>2022-02-03T05:56:36.151Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article we will discuss the concept of tree shaking and how angular handles tree shaking in general and specifically for services. Once we are good with that, we will move on to how we can create tree shakable services for whole applications and how to write services in libraries so that the client application bundle size doesn’t increase. If all this doesn’t make sense at the moment, give the whole article a read and it should start making some sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E7BAS-vHarxrgdtK" /><figcaption>Photo by <a href="https://unsplash.com/@veeterzy?utm_source=medium&amp;utm_medium=referral">veeterzy</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Prerequisites</h4><ul><li>Understanding of angular dependency injection will help get a better grasp of whatever concepts we are going to discuss</li></ul><h3>What is Tree Shaking?</h3><p>Definition — It refers to the process of removing unused code during the build phase.</p><p>For example — If we use a library that has 5 functions but in our code we only make use of 3, then in the final bundle we wont have the code for the 2 unused functions, this helps reduce bundle size and may seem like a trivial thing but in larger enterprise applications it has a lot of impact.</p><p>Angular prod builds do tree shaking for us but there are certain scenarios that we need to take care of which we will be discussing next.</p><h3>Tree Shakable Services</h3><p>If we provide our services in any module, lets say the app module for a root level service, then our services will not be part of tree shaking irrespective of whether we actually inject the service in any component or not. In below example we are providing the TestService in the app.module.ts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*Aw-Jya_DBC4C34sDEWDdGg.png" /><figcaption>providing test service in module using import</figcaption></figure><p>How tree shaking works is that it checks for references and based on that decides whether or not to remove some code or dependency based on usage. Here, since we are importing the TestService in app.module.ts irrespective of the fact that we don’t inject it anywhere in our application our bundle will still have TestService present.</p><p>To create a tree shakable service we can remove this import from the module and make use of the new ‘providedIn:root’ syntax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*rzlWQlvsR-XdcIQiZ7v5IA.png" /><figcaption>test.service.ts</figcaption></figure><p>Now we don’t have any import reference and angular knows to provide the service at root level. If this service is not injected in any component and remains unused then the final bundle will not have this service code in it. It will be tree shaken.</p><h3><strong>Need for Lightweight Injection Token?</strong></h3><p>Lets take a simple example where we use a component from another 3rd party library that we might have installed probably using npm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*x0DHx8L4T29VGu08BukPxg.png" /><figcaption>app.component.html</figcaption></figure><p>Since SomeLibrary component is content projected inside the SomeLibraryContainer component we might have an implementation of the library as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*V2eujC-EvycyUw-kR6iRBg.png" /><figcaption>some-library-container.ts</figcaption></figure><p>Based on the code, there might be cases where in the SomeLibraryContainer component may not need or have the SomeLibraryComponent projected. Something like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*GcabhxVTCF07VsAMlUMu2Q.png" /><figcaption>no content projection (code commented)</figcaption></figure><p>Now we should expect tree shaking to remove SomeLibraryComponent related code in build but that does not happen. Reason is that we have 2 references to it.</p><ol><li>The Type reference — projectedContent: SomeLibraryComponent | null (To infer the type of projectedContent variable)</li><li>The Value reference — @ContentChild(SomeLibraryComponent)(here we pass the reference as a value to the ContentChild decorator)</li></ol><p>How does compiler handle the above?</p><ol><li>Type reference — gets removed after conversion from TypeScript so this has no impact on tree shaking.</li><li>Value reference — Value is needed during runtime and so tree shaking is not done.</li></ol><p>Another reason for tree shaking not happening can be when token is used in constructor for dependency injection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*YguvR2fzX6_xh1fB8GvWqQ.png" /><figcaption>constructor reference</figcaption></figure><p>Even though the above is a type reference, the component is not tree shaken because the code gets converted as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*J2_GEoXgsHtcj3Iz6JIp2A.png" /><figcaption>now the reference is of value type</figcaption></figure><p>The above reasons are why we need to use lightweight injection token.</p><h3>How to implement lightweight injection token?</h3><p>The lightweight token is a design pattern of using abstract classes which have minimal code. It will not be tree shaken but that will have negligible impact since the main implementation is in the component that implements the abstract class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*49MnsQ0Dmwl9EqQyZSb1xw.png" /><figcaption>some-library-component.ts with token implemented</figcaption></figure><ul><li>We created an abstract class which is the lightweight token that we will inject (naming convention — &lt;component name&gt;Token)</li><li>We provided the token and specified that we need to use the component class, hence providing the connection between token and the component.</li><li>The component class extends the token abstract class</li></ul><p>Now, we can use the token instead of the component class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/647/1*pQW7Kz7eLbKa0ALK2YvHLw.png" /><figcaption>using tokens instead of component class reference</figcaption></figure><p>Now, there is no type or value reference to SomeLibraryCompoent and it can be easily tree shaken if not used.</p><h4>Calling methods of child component from the parent when using tokens</h4><p>Since we are using abstract class we also need to write abstract methods for the parent component to access child component methods in type safe manner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/895/1*LJkdwveCw7wROMDQsSmZgA.png" /><figcaption>implementing the abstract method in library component</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/629/1*Tlois8EnPgsQj02qabqXlw.png" /><figcaption>using the abstract method in type safe manner</figcaption></figure><h3>Conclusion</h3><p>Tree shaking is an important concept to know about and I hope this article made it a bit more clearer to you than before. We also discussed angular specific ways to have better, tree shakable code in our applications and libraries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc5d5eb177b3" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/tree-shakable-services-and-lightweight-injection-tokens-for-optimizing-angular-apps-fc5d5eb177b3">Tree Shakable Services and Lightweight Injection Tokens for optimizing Angular apps</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Caching using RxJS]]></title>
            <link>https://awstip.com/angular-caching-using-rxjs-1e02e9ad3a7?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e02e9ad3a7</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[rxjs]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[caching]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Wed, 26 Jan 2022 17:27:25 GMT</pubDate>
            <atom:updated>2022-01-28T07:15:25.550Z</atom:updated>
            <content:encoded><![CDATA[<p>There are various ways in angular to increase the web application performance like lazy loading, tree shaking, compression etc. One of the ways is to implement a caching mechanism for network calls which will help get data in no network situations and also reduce load for low speed connections. In this article we will leverage RxJS to easily implement such a caching mechanism.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zfC1OxqpaixA_JhP" /><figcaption>Photo by <a href="https://unsplash.com/@fikrirasyid?utm_source=medium&amp;utm_medium=referral">Fikri Rasyid</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>prerequisites to better understand this article</h4><ul><li>Basic knowledge of angular and how to make api calls</li><li>Some knowledge of RxJS will be helpful but it is not mandatory</li></ul><h3>Initial state of our sample app</h3><p>I will be adding a GitHub repo link at the end which will have the completed application code. We start with a simple angular application that makes a call to an open API which return cat facts randomly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XaMunkPHqurlPPVf7HRRfA.png" /><figcaption>Sample App with cat data</figcaption></figure><p>Each time we click on ‘make call’ button, an API call will be made to update the fact data shown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*DR9e6u59XusZMC7IcrKjlg.png" /><figcaption>network.service.ts where we make api call</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*64SMqncn_TgyLSRk3qZyfw.png" /><figcaption>app.component.ts</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*nWEOadqFFXVa2IdL_caOOQ.png" /><figcaption>app.component.html</figcaption></figure><h3>Initial Caching</h3><p>To start with we will make the api call once and store the latest value in the cache using shareReplay from RxJS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*u3TgrernGSpsm55mcFoiKQ.png" /><figcaption>network.service.ts with caching</figcaption></figure><p>Now, no matter how many times we click on ‘make call’ button, we see the same initial data which we got from response of first API call and no new API call is made. This is because we have set cache size to 1 and shareReplay operator shares the value from the cache.</p><h3>Updating the cache</h3><p>Till now we only store one particular value that never updates. Let’s say we want to update the cache every few seconds or minutes so that we get updated data when we click the ‘make call’ button on UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*g2Ryt5oVgX3V_-R1E4qG8w.png" /><figcaption>updating cache every 10 seconds</figcaption></figure><p>In the above change we basically use ‘timer’ from RxJS to make the API call every 10 seconds and update the cache.</p><p>End result — once we get a new fact, no matter how many times we click ‘make call’ button it will not make the API call or update data. After 10s the timer will update the cache and then if we click on ‘make call’ button we get new data from the updated cache.</p><p>Note — even if you don’t click the ‘make call’ button the value on the screen updates and you get new cat fact when cache updates. This might not be what you want since the user may be reading that fact when it suddenly refreshes. To control this we will make further changes</p><h3>Update View only when user wants</h3><p>we will make use of the ‘take’ operator in RxJS to take only one value and end subscription.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*SpFd20NPEhzSdPsPdDMhnw.png" /><figcaption>network.service.ts</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*zWcyeG7R-bDE6L25fe5VNQ.png" /><figcaption>app.component.ts</figcaption></figure><p>Now each time we invoke the makeCall() method we take one value from the cache and end the subscription since we used take(1). the cache still updates and when we make a new subscription we get value from the latest cache.</p><h3>Conclusion</h3><p>When working with huge data responses, such caching mechanism is very useful as we can update the cache every fixed interval and UI wont handle the pressure of loading and rendering such large data sets every time a user lands on the page or part of the web application. The above is a very simple example to understand the concept but the same can be extended to much more complex and real world scenarios easily once the concept is understood. Please find the GitHub link below —</p><p><a href="https://github.com/Hulk914/angular-advanced-caching">GitHub - Hulk914/angular-advanced-caching: caching using RxJS</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e02e9ad3a7" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/angular-caching-using-rxjs-1e02e9ad3a7">Angular Caching using RxJS</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating custom validators with NgRx Forms]]></title>
            <link>https://awstip.com/creating-custom-validators-with-ngrx-forms-64b9c07caa17?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/64b9c07caa17</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[ngrx]]></category>
            <category><![CDATA[ngrx-form]]></category>
            <category><![CDATA[validation]]></category>
            <category><![CDATA[custom-validator]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Sat, 22 Jan 2022 06:48:52 GMT</pubDate>
            <atom:updated>2022-01-27T08:08:37.029Z</atom:updated>
            <content:encoded><![CDATA[<p>This article will help you to create and use custom validators when working with NgRx Forms. There is little documentation and very few examples regarding NgRx forms so I hope this helps to make things clearer</p><h4>prerequisites to better understand this article</h4><ul><li>NgRx and NgRx forms basic knowledge is required, refer below link for the same.</li></ul><p><a href="https://just-another-developer.medium.com/beginner-level-guide-to-ngrx-ngrx-forms-422403ef2018">Beginner level guide to NgRx &amp; NgRx Forms</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yHa-6frkjCFUIWXv" /><figcaption>Photo by <a href="https://unsplash.com/@kirklai?utm_source=medium&amp;utm_medium=referral">𝓴𝓘𝓡𝓚 𝕝𝔸𝕀</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The Example App</h3><p>We will be using a simple textbox which will be attached to NgRx using NgRx forms and then we will add some simple custom validations. The actual logic once you know the process of adding custom validations can be changed as per your requirements. No fancy CSS is used, simply we will show the error object when error comes. I will add the link to the app at the end of the article for your reference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*lC9xsFa-fqbuOUvhO7PRVg.png" /><figcaption>Default state of our sample app</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*C2G_S0CDJP4tp6-KmYGhNg.png" /><figcaption>Folder structure</figcaption></figure><p>We are not going to dispatch any custom actions so there is no actions file present here. To revise or learn what each of these files does and how the form is created, please go through the link mentioned above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*dIFBe1uYgKvlWJDrfMBpXg.png" /><figcaption>The redux state</figcaption></figure><p>This is what our redux state looks like. myFormGroupState is the formgroup which has inputString as a formControl. This formControl is then linked to the textbox of our application.</p><h3>Adding custom validator through the reducer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*JhagVtDlEhq23bdvW98e6w.png" /><figcaption>app.reducer.ts</figcaption></figure><p>Things to notice</p><ul><li>in appReducer onNgRxForms() is necessary to call required reducers when you change values in your form. Otherwise whatever you type in the textbox will not update in the redux store.</li><li>you need to wrapReducerWithFormStateUpdate where we extract the formGroup from the state and give it to the validationReducer where the actual validators are specified.</li><li>Here the ‘required’ validator is provided by NgRx, for more validators and info below is the documentation link</li></ul><p><a href="https://ngrx-forms.readthedocs.io/en/master/user-guide/validation/">Validation - ngrx-forms</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*BL9NTDS-HRE7U45skD_neg.png" /><figcaption>app.validator.ts</figcaption></figure><p>the customValidator is defined in app.validator.ts and it returns an error of type ValidationErrors. We receive the formControl value in the function using which we can write our logic and return an error object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*Zo8-D3pid7IQFGPeu2MBrQ.png" /><figcaption>in built ‘required’ validator error response</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*Hs7Wm6pA-2VVU7ObOPG4AQ.png" /><figcaption>customValidator error response</figcaption></figure><h3>Adding custom validator in the effects</h3><p>If the validation that we need to perform is more complex and dependent on other states in our store or other formGroups etc. then we can add such validations in the effects where we can obtain such dependent data easily using selectors and rxjs operators like withLatestFrom.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*HxavNUypmwAW83vft5l0Ww.png" /><figcaption>app.effects.ts</figcaption></figure><p>The action we are listening to is setValueAction that gets dispatched everytime value is set/changed on our form. if we have many formControls all dispatches will come into this effect, hence we need to filter out which formControl we are interested in.</p><p>Every form has a unique id which we use while creating the form</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*xv-MGsrij7NwxFiy3wnUyA.png" /><figcaption>FormGroup creation in app.reducer.ts</figcaption></figure><p>Our formControl’s id will be the FORM_ID.formControlName, in our example ‘[APP] FORM ID.inputString’</p><p>using this id we can filter the setValueAction.</p><p>in our example we call the customMattValidator and keep all validators in the validators.ts file but we can also have logic directly inside the effect (although that will bloat your effect if the validation logic is bigger).</p><h4>Main actions involved</h4><p>For all actions proper formcontrolId needs to be passed to make it work properly.</p><ul><li>StartAsyncValidationAction — dispatch this at the start of the validation (we are using startsWith operator for this purpose), it sets the proper states in the formControl</li><li>ClearAsyncErrorAction — dispatch to clear errors when validation passes</li><li>SetAsyncErrorAction — dispatch along with error message in case validation fails</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/1*hPVMc30E3O2hiBX0LJn_iQ.png" /><figcaption>actions for validation</figcaption></figure><h3>Conclusion</h3><p>Please find the github repo link below:</p><p><a href="https://github.com/Hulk914/ngrx-custom-validation">GitHub - Hulk914/ngrx-custom-validation</a></p><p>Hope this helps to start with custom validations and NgRx Forms. Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64b9c07caa17" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/creating-custom-validators-with-ngrx-forms-64b9c07caa17">Creating custom validators with NgRx Forms</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Firebase with Angular Applications — Beginners Guide]]></title>
            <link>https://awstip.com/using-firebase-with-angular-applications-beginners-guide-cecb40337f75?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/cecb40337f75</guid>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[angularfire]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Wed, 22 Dec 2021 18:19:02 GMT</pubDate>
            <atom:updated>2022-01-19T07:34:38.791Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Firebase with Angular Applications — Beginners Guide</h3><p>This article will help you setup a firebase real-time datastore which is a NoSQL database that Firebase provides as a service. This is easy to setup and free for limited traffic so frontend developers can use it to create complete web applications without backend knowledge. We will not cover the CRUD operations in-depth in this article, the main purpose is to connect an angular application with firebase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2HaWdZ1vYud3RQ7D" /><figcaption>Photo by <a href="https://unsplash.com/@raquelraclette?utm_source=medium&amp;utm_medium=referral">raquel raclette</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Prerequisites</h3><p>To better understand this article the following is required</p><ul><li>working knowledge on angular and npm</li><li>google account that you can use with firebase</li></ul><h3>Setting up firebase</h3><ul><li>Go to firebase website and create a new account or login to existing one. you need a google account for this.</li><li>Once account is created create a new project and give it a name, continue for the remaining steps</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/735/0*8YhMBtsVnROX-W4O" /><figcaption>Create Project</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/0*qG9LWdkhGvOJujuY" /><figcaption>Name the project</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ICD4E44mb9dJsZrA" /><figcaption>select web-app</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/0*uR5yEZeNw8nTxKBF" /><figcaption>select set up firebase hosting</figcaption></figure><ul><li>In settings go to the bottom and select web app, in next step select firebase hosting and name your app.</li><li>on creating the web app you will get the firebase config which you need to copy in the environment.ts of your angular application.</li></ul><pre>firebaseConfig = {<br>  apiKey: &quot;*******&quot;,<br>  authDomain: &quot;*****&quot;,<br>  projectId: &quot;*****&quot;,<br>  storageBucket: &quot;*****&quot;,<br>  messagingSenderId: &quot;****&quot;,<br>  appId: &quot;******&quot;,<br>  measurementId: &quot;*******&quot;<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tlfP0PV9TRQ6SzorVNogNQ.png" /><figcaption>create real-time database</figcaption></figure><ul><li>select real-time database and open in testing mode for initial learning.</li><li>you can add data here using the GUI using plus minus icons</li></ul><h3>Setting up the Angular application</h3><ul><li>Install below firebase related packages using npm</li></ul><pre>npm i firebase <a href="http://twitter.com/angular/fire">@angular/fire</a> --save</pre><ul><li>As mentioned before, put the firebase config in the environment.ts file</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*9tN41q9rxAi0YaW4Phx-eQ.png" /><figcaption>add required firebase modules</figcaption></figure><ul><li>Add the required modules and initialize AngularFireModule with the firebase config from environment.ts file</li><li>A simple example to set some values and listen to values changes is</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*PU8-f9COm2vekrJfDbfLxQ.png" /><figcaption>app.component.ts</figcaption></figure><ul><li>Inject AngularFireDatabase and using the object or list methods get a reference</li><li>using the reference we can perform crud operations</li></ul><h3>Conclusion</h3><p>This was a simple guide to connect angular with firebase real-time database. It is simple, fast &amp; free for limited usage and great for personal projects where we don’t want to spend time and resources for backend apis and data storage.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cecb40337f75" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/using-firebase-with-angular-applications-beginners-guide-cecb40337f75">Using Firebase with Angular Applications — Beginners Guide</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simplest way to deploy angular application on Heroku]]></title>
            <link>https://just-another-developer.medium.com/simplest-way-to-deploy-angular-application-on-heroku-b741c3fd0d3b?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/b741c3fd0d3b</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Thu, 09 Dec 2021 11:59:54 GMT</pubDate>
            <atom:updated>2021-12-09T11:59:54.258Z</atom:updated>
            <content:encoded><![CDATA[<h3>Simplest way to deploy angular applications on Heroku</h3><p>This article will help you deploy your angular application on Heroku without any charges. The process is pretty simple and with just simple configurations you can set up things like auto deployment each time you push your changes to the Github repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gRFlocLLyvm3E4TP" /><figcaption>Photo by <a href="https://unsplash.com/@kelvin1987?utm_source=medium&amp;utm_medium=referral">Kelvin Ang</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Prerequisites</h3><p>To better understand the article you should be aware about the basics of following topics</p><ul><li>github (pull, commit, push)</li><li>creating a sample angular application that runs locally</li><li>creating a simple node server using express (we will discuss changes required to make it compatible with Heroku)</li></ul><h3>Setting up the node server</h3><p>We will run our angular code using node. For that the first step is to install below dependencies. (don’t forget the ‘save’ option since Heroku installs dependencies from package.json and the save option will add below dependencies to it)</p><pre>npm i --save path express</pre><p>Create a file named server.js in the root level (same location where package.json resides). Here we will create a simple express server using below code that will serve our angular application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/213/1*tE5cyk57wBF3OTL7gC3Xvw.png" /><figcaption>server.js at root level</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*rVUTw23m7w_E5Xcshqg-3Q.png" /><figcaption>server.js code</figcaption></figure><p>Here make sure to give process.env.PORT as the port number since heroku will assign the value during deployment and HOST should be 0..0.0.0</p><p>The rest of the code is simple, we are pointing to the dist folder where our application resides after we perform a successful build on the source code. the same application is served using express at the specified port (which heroku will assign to us on its own)</p><h4>Package.json</h4><p>Make sure to add devDependencies like @angular/cli, @angular/compiler-cli, typescript etc. to dependencies, since heroku wont have these system level dependencies, it will only install what is present in package.json dependencies object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*2RjsLBnxYUMmFD-5MxzVIA.png" /><figcaption>Sample angular 12 package.json</figcaption></figure><p>Specify a heroku-postbuild script to use when heroku builds the application. When this is specified Heroku will ignore the build script.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*VEpUVUsjeeKQz3adlwRAIA.png" /><figcaption>heroku-postbuild</figcaption></figure><p>Specify the node and npm versions in your system (or whatever you want to use) in engines object</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*dQT6SEzRnMA867wvSCsXDA.png" /><figcaption>node and npm version</figcaption></figure><p>Heroku uses the start script to run your application, since we are running on node that needs to be changed from ng serve to node server.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*zjeN-qNhN5ckO8u6mgcs_w.png" /><figcaption>start script</figcaption></figure><h3>Setting up Heroku</h3><p>Once all these changes are done its time to setup Heroku<br>Go to Heroku and login or create new account</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*aBqvNu5N7WJqN-oKtE7z2g.png" /><figcaption>login page</figcaption></figure><p>Once you login, you can see the dashboard where you have an option to create new app</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4NLTCmEAjU09J9zHKEXwiw.png" /><figcaption>click on new and then create new app</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cp_rWdpO30aLLuw4PXg5XA.png" /><figcaption>Enter any app-name and click on create app</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c0Ue23P83LrExNgALSGYTA.png" /><figcaption>Select deployment method as Heroku</figcaption></figure><p>You need to connect to your github account where you have your application.</p><p>Search for the repo name and click on connect</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tEyOpmCb-qm7YNXUBPHDpQ.png" /><figcaption>search works on enter or clicking the search button not while typing</figcaption></figure><p>Once you connect you may get an error if the repo is empty so push application beforehand. Once connection is successful select option to auto deploy on each push to main branch</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*OAY5Rx0EDOWMtgYNI0jGLg.png" /><figcaption>Enable auto deploy</figcaption></figure><p>Once enabled, each time you push your code to this github repo in your main branch, Heroku will auto deploy your changes.</p><p>To open application click on the open app button</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*D-EB2BdpbgEt7-nNlIaHYg.png" /><figcaption>Open application</figcaption></figure><h3>Conclusion</h3><p>This is a very simple one time setup post which you just need to keep pushing your changes to git which you would have done anyways. There are no charges unless you need better performance or the website has heavy usage (check out dynos on heroku support page).</p><p>For any issues during build and deploy, you have to install heroku-cli in your system and run below command to see the error and resolve it</p><pre>heroku logs --tail</pre><p>Note that whatever we did from heroku website can also be done using heroku cli but its slightly more complicated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b741c3fd0d3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Module Federation Angular 12 — Beginners Guide with example]]></title>
            <link>https://just-another-developer.medium.com/module-federation-angular-12-beginners-guide-with-example-677e0c0543fd?source=rss-fee096957eba------2</link>
            <guid isPermaLink="false">https://medium.com/p/677e0c0543fd</guid>
            <category><![CDATA[module-federation]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular12]]></category>
            <category><![CDATA[micro-frontends]]></category>
            <dc:creator><![CDATA[Ayush Thakur]]></dc:creator>
            <pubDate>Fri, 08 Oct 2021 23:46:49 GMT</pubDate>
            <atom:updated>2021-12-16T20:52:53.747Z</atom:updated>
            <content:encoded><![CDATA[<h3>Module Federation Angular 12 — Beginners Guide with example</h3><p>This guide will help you create a simple angular application that is able to include modules from other angular application aka microfrontends and use them our sample angular application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pBXh7I4BD5EXbbTB" /><figcaption>Photo by <a href="https://unsplash.com/@glencarrie?utm_source=medium&amp;utm_medium=referral">Glen Carrie</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Brief intro on microfrontends and why they are popular?</h3><p>Microfrontends enable the developers to club together multiple small applications into a single application as opposed to the earlier monolith design. Basically if you want to create an application to book tickets for travel, movies, events etc. for the end user it will be a single application on a single domain but from development perspective dedicated teams will work on each aspect.<br>So we will have one team working on movie booking in separate repo and their application can run individually. Similarly there will be teams working on separate application for events &amp; travel respectively. <br>This has an advantage since we have loose coupling and each sub application can be developed and maintained separately. Changes to one part of code in one application will not cause bugs in another application. We also have some cons but considering the tradeoff, depending on your use-case microfrontends are a powerful tool. <br>Let’s dive into how we can implement it through module federation in Angular 12</p><h3>Project Structure &amp; Demo</h3><p>We have 1 shell application and 2 remote applications (microfrontends). On Clicking each link we load a module from the remote application and use it in our shell application. As far as angular is concerned, this is just another lazy loaded module.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/132/1*cuM2eDE57wHLIHqYD-YjSQ.png" /><figcaption>3 different angular applications</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AIIWEoleSMHUjUfhuf1v-Q.png" /><figcaption>Shell/main application running on port 5000</figcaption></figure><p>In our scenario the 3 applications are running on different ports on localhost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D3mKjO76bTBGccQoiXN6uQ.png" /><figcaption>remote application running on port 3000</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*22OqMHBMdVTpYWFwxT6SBQ.png" /><figcaption>remote1 application running on port 7000</figcaption></figure><p>When we click on the links, modules from remote applications are loaded like a lazy loaded module and we can see the result in the router outlet</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RowqXlWkQh6GRX0CL7y-Ag.png" /><figcaption>On Clicking MicroFrontend, we go to /remote and load remote application</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hTCsKFlxPqBNo2JQelktWQ.png" /><figcaption>On Clicking MicroFrontend1, we go to /remote1 and load remote1 application</figcaption></figure><p>This has simple html but it can be a module with complex routes, many components etc. Let’s go into the code now.</p><h3>Implementing Module Federation with Angular 12</h3><p>To use module federation we need to run below command in all the related applications.</p><pre>ng add <a href="http://twitter.com/angular">@angular</a>-architects/module-federation --project remote1 --port 7000</pre><p>Points to note -</p><ul><li>remote1 is the name of the project (which you used when creating the application with ng new command)</li><li>port can be anything, just make sure it free</li><li>The above command does few changes under the hood and adds a webpack.config file for configurations related to module federation. This is in addition to the angular configurations.</li><li>Run this command in the shell, remote, remote1 application with correct project name and unique port</li></ul><h4>Configure webpack file</h4><p>The package we added gives us a skeleton webpack.config file. We need to give details in the remote config files which can be accessed by the config file of the shell. (Note: for webpack.config changes to reflect, restart angular application)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*MmY3z02G7HVz7jjzWVRlNQ.png" /><figcaption>webpack.config.js of remote application</figcaption></figure><p>There are multiple things we can do here like giving shared modules so that we don’t have duplicates and version mismatch etc. But the main part is providing a name, filename &amp; exposing the module with a key. The mapping is done by the filename, in this case remoteEntry.js which is a small file that gets created during runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*DHfqbLvOulD1QpmngYz7Aw.png" /><figcaption>shell/container application webpack.config.js</figcaption></figure><p>In Shell application we define the keys with which to access the remoteEntry files so that angular will know from where to lazy load the module.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*G7gr7Wo3pTLb3O4vMuczoA.png" /><figcaption>shell application routing module</figcaption></figure><p>Now we need to load the modules on route change. Here to get the module we use a helper method from the package we added earlier called loadRemoteModule where we pass the proper remoteName, remoteEntry &amp; exposedModule which we used in the webpack.config.js file of remote applications.</p><p>Link to Code on github below</p><p><a href="https://github.com/Hulk914/moduleFederation">GitHub - Hulk914/moduleFederation</a></p><h3>Conclusion</h3><p>That’s is it. On a basic level this is how you can achieve microfrontend architecture with the help of module federation in angular 12. There are lots of good articles available online in case you want to deep dive into the topic. Happy Learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=677e0c0543fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>