<?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 Joost Zöllner on Medium]]></title>
        <description><![CDATA[Stories by Joost Zöllner on Medium]]></description>
        <link>https://medium.com/@joostio?source=rss-e9ad51f61d83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MAPxUYPNoVct2t-SdEN2lA.png</url>
            <title>Stories by Joost Zöllner on Medium</title>
            <link>https://medium.com/@joostio?source=rss-e9ad51f61d83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 05:53:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joostio/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[Web Fundamentals — Why Remix is here to stay]]></title>
            <link>https://medium.com/holisticon-consultants/web-fundamentals-why-remix-is-here-to-stay-339c3384b6ff?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/339c3384b6ff</guid>
            <category><![CDATA[web-fundamentals]]></category>
            <category><![CDATA[remix]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[accessibility]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Fri, 17 Dec 2021 13:50:11 GMT</pubDate>
            <atom:updated>2021-12-17T20:29:31.745Z</atom:updated>
            <content:encoded><![CDATA[<h3>Web Fundamentals — Why Remix is here to stay</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c-F1-srT6HbkUQEjjEKUIA.png" /></figure><p>If you’ve been following the frontend community lately, you will have noticed a newcomer on the block called <a href="https://remix.run/">Remix</a>. Now there are always new hyped up frameworks released as quickly as they die down, but for me Remix is different and I will tell you why.</p><p>Remix isn’t completely new. It has been in development since 2020 but was a paid framework up until now! Initially marketed as a “<strong>killer React framework from the creators of React Router</strong>” it is much more than that.</p><h3>What is Remix?</h3><p>Remix is a fullstack framework based on React that lays a big focus on <strong>web fundamentals</strong>. Those fundamentals are what I’m most excited about. If you’ve worked with <a href="https://nextjs.org/">NextJS</a>, Remix will look familiar. You have your React components in a file together with your backend code. The React components will either be rendered on the server or hydrated in the client, which gives you great performance and SEO. The routing is file-based similar to NextJS, but with one catch. It supports <strong>nested routing</strong>! Something other frameworks like Angular have been doing for a long time and I love it. It makes development a lot more flexible when working with dynamic content.</p><p>I don’t want to go too much into detail here as I want to lay the focus on what those <strong>web fundamentals</strong> actually mean.</p><h3><strong>Web Fundamentals</strong></h3><p>If you’ve been developing web applications for some time, you most likely know these fundamentals, but for newer developers they might come off as a surprise as most frameworks either don’t use them or abstract them away from the developer. But not Remix!</p><p>The philosophy of Remix is to work <em>with, not against</em> the foundations of the web. Those foundations include the browsers, HTTP and HTML.</p><blockquote><a href="https://remix.run/docs/en/v1/guides/philosophy#web-standards-http-and-html">These technologies have been around for a long time. They’re solid. Remix embraces them completely.</a></blockquote><h4>Actions</h4><p>One of the fundamentals embraced by Remix are <a href="https://remix.run/docs/en/v1/api/conventions#action">actions</a>. The last time I used form actions was in a PHP application years ago. Remember the method and action properties on the&lt;form&gt; element?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1326f21d3b5959f68bcbac6f32a365df/href">https://medium.com/media/1326f21d3b5959f68bcbac6f32a365df/href</a></iframe><p>In the times of frameworks shipping with Javascript-based solutions for form-handling it might be surprising, that this form works without any client-side Javascript. When submitting this form, a POST request will be sent to the relative login URL with a <a href="https://developer.mozilla.org/de/docs/Web/API/FormData">FormData</a> payload containing the username and password values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9ca1bdecd4f5b16bff178db0764e5ee/href">https://medium.com/media/d9ca1bdecd4f5b16bff178db0764e5ee/href</a></iframe><p>Remix uses this fundamental web feature by allowing you to define a function called <a href="https://remix.run/docs/en/v1/api/conventions#action">action</a> that will be called with the aforementioned FormData request. No need for libraries like Formik or Axios to read the form values and send them to the server. <strong>Everything is done natively by the browser without Javascript</strong>.</p><h4>Links</h4><p>Links have been a web foundation for a long time, but most developers might not come into contact with links regularly. They’re usually abstracted away or handled automatically by build tools. But they have been improved with new features like prefetch, preload or media queries over the years.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7965b0ae63910945b54321bad4b608e3/href">https://medium.com/media/7965b0ae63910945b54321bad4b608e3/href</a></iframe><p>Remix embraces these features by giving you the option to define a function called <a href="https://remix.run/docs/en/v1/api/conventions#links">links</a> for each route that returns the links the page should have.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e65746f83fabb100b4dfdeab3e3d852e/href">https://medium.com/media/e65746f83fabb100b4dfdeab3e3d852e/href</a></iframe><p>This gives you the ability to use the natively supported features mentioned above. Again, <strong>no Javascript required!</strong></p><h4>Meta</h4><p>When doing SEO, good content and metadata is very important. Most social media sites have their own set of metadata like <a href="https://ogp.me/">OpenGraph</a> they rely on, to give the users the best experience possible. This means that all metadata has to be properly maintained.</p><p>Let’s use a simple example with some OpenGraph meta properties to add metadata to an article about conventions in Remix in plain HTML.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52d8092cf188e633336d76ff9d633bd0/href">https://medium.com/media/52d8092cf188e633336d76ff9d633bd0/href</a></iframe><p>In Remix you define a <a href="https://remix.run/docs/en/v1/api/conventions#meta">meta</a> function that returns the metadata for the page. This enables you to define different metadata for the current page depending on its content <strong>without additional Javascript</strong>. The meta tags will be generated on the server which is especially important for SEO.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/913391e951143b4571fe560c1ad67a56/href">https://medium.com/media/913391e951143b4571fe560c1ad67a56/href</a></iframe><p>Now all these features work without client-side Javascript. That is one part of the design philosophy behind progressive enhancement and also a nice side-effect of Remix’s architecture.</p><h3>Progressive Enhancement</h3><p>In the age of web applications downloading multiple megabytes of Javascript it might seem crazy to develop applications without Javascript. But as you’ve seen above a lot is possible without the need for shipping Javascript.</p><p>The simple client/server model of <a href="https://remix.run/docs/en/v1/pages/philosophy#progressive-enhancement">Remix enables you to develop applications without the need for Javascript</a>. It certainly makes your life and the user’s easier, but it isn’t required. Which I think is a great feature as you can let the amount of client-side Javascript be as low as possible but also as high as needed for a good user experience.</p><p>If the user disabled Javascript, will your application still work? Most likely not. With Remix you definitely have the possibility to develop applications that do not require Javascript but progressively enhance the user’s experience with Javascript enabled.</p><h3>Accessibility</h3><p>Progressive Enhancement is something that implies accessibility, but web developers often neglect the accessibility of their applications. More often than not the fancy datepicker or multiselect dropdown components you install from NPM are not accessible. But the web should be for everyone and we as developers should always keep the accessibility of our applications in mind.</p><p>Now Remix isn’t doing this for you, but it’s certainly giving you all the tools you need to, without forcing you to use specific ones. What I think is important is that they embrace the need for accessibility <a href="https://remix.run/docs/en/v1/guides/accessibility">in their documentation</a>.</p><p>One thing they advocate for is the use of live announcements when using Javascript-based routing in the application. As React Router hooks into the normal browser behaviour, certain route transitions might not be obvious to every user. That’s why they mention important features like <strong>focus managemen</strong>t and <strong>live-region announcements</strong>.</p><p>What supports their commitment to accessibility, is the simple fact that the <a href="https://github.com/remix-run/remix/blob/4256f5e187140afe461de497ef732d01e8524812/examples/basic/app/root.tsx#L197">example application</a> contains such a live-region announcement by default. This is something that is a first for me but very much welcome!</p><p>Now why am I so excited about old features that have been part of the web for years? It’s about the approach Remix is taking. The fact they embrace those core fundamentals and accessibility is something I think is great for the web. Instead of trying to reinvent the wheel, they rely on the foundations of the web, which brings not only us as developers but also the web in general forward.</p><p>Want to read more? See my other articles and be sure to follow for more!</p><ul><li><a href="https://medium.com/holisticon-consultants/this-is-fine-when-everything-goes-wrong-on-release-day-f64dbbb1d89b">This is fine — When everything goes wrong on release day</a></li><li><a href="https://medium.com/holisticon-consultants/angular-bundle-size-reduction-ae244cd78ee0">Honey, I shrunk the bundle 📦— A story about Angular bundle size reduction</a></li><li><a href="https://medium.com/holisticon-consultants/angular-i18n-picking-the-right-tool-for-the-job-b543460801db">Angular i18n 🌐— Picking the right tool for the job</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=339c3384b6ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/holisticon-consultants/web-fundamentals-why-remix-is-here-to-stay-339c3384b6ff">Web Fundamentals — Why Remix is here to stay</a> was originally published in <a href="https://medium.com/holisticon-consultants">Holisticon Consultants</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[This is fine — When everything goes wrong on release day]]></title>
            <link>https://medium.com/holisticon-consultants/this-is-fine-when-everything-goes-wrong-on-release-day-f64dbbb1d89b?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/f64dbbb1d89b</guid>
            <category><![CDATA[failure]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[releases]]></category>
            <category><![CDATA[mail]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Mon, 27 Sep 2021 13:45:30 GMT</pubDate>
            <atom:updated>2021-09-27T13:45:30.605Z</atom:updated>
            <content:encoded><![CDATA[<h3>This is fine — When everything goes wrong on release day</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*2NeJzKUAFIJ6s6PvpYy9kQ.gif" /></figure><h3>A bit of background</h3><p>We were working on an application meant to manage permissions to various services for external suppliers. The internal support team could use this tool to onboard new suppliers and give them access to free and paid services. This tool was meant to replace the old one and improve the workflow for the internal and external users.</p><p>Technology-wise it included <strong>Keycloak </strong>as a base for the authentication and a self-built <strong>Spring Boot</strong> application with an <strong>Angular </strong>frontend for managing the permissions and authorization.</p><p>Besides migrating existing services to our new platform one major task was migrating all internal and external users to the new <strong>Keycloak</strong>. This wasn’t straight forward because the hashing algorithm for passwords used in the old system wasn’t compatible with our new <strong>Keycloak </strong>instance. To make the transition as easy as possible for the users, we decided to transfer all users to Keycloak without their old passwords and send a password activation mail to everyone, where they could set a new password. This mail should then be sent on release day together with a welcome message to onboard the users.</p><h3>Along came release day</h3><p>When release day came along we had everything prepared. Everyone was up early, we had a <strong>long checklist of tasks</strong> with exact timestamps in order to ensure a smooth transition from the old to the new system.</p><p>To send every user a mail with an activation link to set their new password, I wrote a simple NodeJS script that <strong>resets the password of every user</strong> in Keycloak via its API. Keycloak then sents a mail via its configured mail server to the email address of the user.</p><p>Before release day we tested this script extensively by using a mocked mail server and checked if the number of sent mails matched the number of users in Keycloak. <strong>Everything was fine by this stage</strong>. So on release day the only thing we had to do, was configure the correct mail server in Keycloak and let my script run.</p><p>We decided to release our new system on a friday. In case something went wrong we still had the weekend to fix things without users trying to work with the new system.</p><h3>Where are all the mails? 📨</h3><p>When almost everything was done, the last task remaining was to send the password activation emails. So we configured the correct mail server and let my script run. The Keycloak API was happily accepting the thousands of requests and we were already opening some beers in the light of a successful release.</p><p>But as we checked the dashboard of our mail provider we saw… <strong>nothing</strong>. No sent mails. Although Keycloak successfully accepted all requests via its API, no mail was actually sent.</p><p>We were clueless at this point because there were no errors in the Keycloak logs. <strong>The mail server acknowledged all requests</strong>. So the mails went missing somewhere on the side of our mail provider. We confirmed this suspicion by trying to manually send mails via the provider. Those also never arrived at their destination. So we tried to contact the support and asked if they had any outages, which they denied. We were scratching our heads because <strong>just a day prior this exact mail provider worked flawlessly</strong> on our test system.</p><h3>Give me your credit card 💳</h3><p>Because we were already way behind schedule at this point, we decided to switch mail providers and try again. We chose <strong>SendGrid</strong>, because we’ve had good experiences with its service in previous projects. But for sending such a vast amount of mails in such a short time you obviously need a paid account. But getting access to a company credit card in a timely manner was absolutely unrealistic. So <strong>a colleague sponsored her credit card to pay for a SendGrid account</strong>.</p><p>For our next try we configured Keycloak with SendGrid’s mail server and again let my script do its magic. This time we could see the mails immediately in the SendGrid dashboard and were relieved that everything seemed to work. So we went back to our previously opened beer, thinking we were done. 🍺</p><h3>Here comes Spamhaus 🚩</h3><p>When we checked SendGrid’s progress we noticed something odd. A majority of our mails were being <strong>flagged as spam</strong> and thus denied by our recipients. We checked SendGrid’s configuration and everything seemed fine. We had a proper <strong>Reverse DNS</strong> set up and the <strong>DKIM </strong>and <strong>SPF </strong>settings looked good as well.</p><p>That is until we checked the sender IP address on Spamhaus. On the <strong>Essentials</strong> <strong>Plan</strong>, SendGrid will send your mails through a random IP address, that is shared with others. The problem was, that this IP address had been flagged for sending spam by Spamhaus <strong>more than one month prior</strong>. Basically all our mails were being blocked because SendGrid sent our mails via an IP address that was flagged by Spamhaus for more than one month.</p><p>At this point we were banging our heads to the table. <strong>How can sending mails be so hard?</strong></p><p>But we had no time to curse SendGrid. We needed to get these mails out as soon as possible. To circumvent the flagged IP address we decided to upgrade our SendGrid plan to <strong>Pro Tier</strong> to get a dedicated unique IP address. Another 90$ charged on our colleague’s credit card. 💸</p><h3>Warmup before workout 🏃</h3><p>Onto the third round! Before trying again, we cancelled all running mail deliveries that were still using the shared IP address and set up for a new run. I executed my script and we began seeing new mail deliveries in the SendGrid dashboard. This time <strong>no mails were flagged</strong> as spam. But <strong>delivery took a magnitude longer</strong> than before.</p><p>This is when we learned about <strong>warming up IP addresses</strong> prior to sending large amounts of mails. <a href="https://docs.sendgrid.com/ui/sending-email/warming-up-an-ip-address">SendGrid has a detailed blogpost about this</a>. Warming up an IP address means, not sending all your mails at once, but in smaller chunks over a bigger stretch of time. This is done to prevent services like Spamhaus to flag your IP address. You basically have to build up a reputation, before being able to send large amounts of mails in smaller timeframes.</p><p>At this stage, the workday was almost over. While we were still fighting with our mails, the support team helped all the users, who did not receive the activation mail, to set a new password. The number of support requests decreased steadily, so we decided to just wait it out over the weekend.</p><p>We only had a few thousand mails and more than two days to properly warm up the IP address. <strong>That should be enough, right?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*HhrcurfBrGpiEydgeaDJHA.jpeg" /><figcaption>Two days warming up should be enough, right?</figcaption></figure><h3>The queue. It’s getting bigger!</h3><p>When monday rolled around, we already knew we were in trouble. Over the weekend <strong>merely half of the queued mails were sent ou</strong>t. And users were already itching to use the new system, but couldn’t get in.</p><p>We now had two problems: The activation mails sent on friday were <strong>stuck in the SendGrid queue</strong> and the users were actively trying to reset their password using — you guessed it — SendGrid. We were in a major dilemma. The SendGrid <strong>queue was getting bigger and bigger</strong> as more users wanted to reset their password. But the IP address still wasn’t warmed up properly, so SendGrid restricted the amount of mails that were being sent out.</p><p>The only solution we had in our minds was to configure Keycloak with a different (third!) mail server, so at least the password reset mails could be delivered in a timely manner. Thus ensuring active users could get in, without having to wait for the SendGrid queue. <strong>But were do we get a new mail server from? </strong>We had been burned two times already.</p><h3>Mail server? Oh you mean this one.</h3><p>By sheer luck we talked to a colleague from the Ops team, who told us that they had an internal mail server that was used for occasional marketing mails. <strong>We were absolutely dumbfounded</strong>. In the months prior, we asked multiple times for an internal mail server, but were always left with a <strong>“not possible”</strong> because of various firewall settings. But this mail server was reachable from our Keycloak instance!</p><p>We quickly configured the server in our Keycloak test environment and tested it. <strong>Boom! Mail instantly delivered</strong>. No spam, no queue, everything worked out of the box. We were relieved beyond words. We configured the mail server on our Keycloak production instance and our mail issues were gone instantly.</p><p>The SendGrid queue was still running for a few days, so a few users got duplicate mails. But in the end all users got access to the new system pretty quickly by then.</p><h3>It can’t get worse, right? 💣</h3><p>At the end of monday everything seemed fine. The mail server was working wonderfully. Our problems seemed to have been solved. Until… we got a call from the support team that <strong>Keycloak was not reachable</strong>. And indeed, all our Keycloak instances were not responding.</p><p>Our frustration reached an all time high. What was especially confusing, was that our <strong>Kubernetes cluster did not kill the non-responding Keycloak</strong> pods, but instead left them running, until all instances were in a deadlocked state.</p><p>When analyzing the log files, we noticed that the amount of database connections was steadily rising before the system got unreachable. We thought that the <strong>amount of allowed connections was too low</strong> for the amount of users we had using our system, so we increased it. This did not fix the issue but only delayed it by a few minutes.</p><p>After countless hours of analyzing logs and debugging the Keycloak instance we were able to narrow the issue down to a <strong>Keycloak extension we developed</strong>. When a user accepts the terms and conditions on the initial login, we save the version of the terms and conditions as well as the timestamp into an auditing system.</p><p>This request to the auditing system was the issue. It was a simple POST request, so what can be the issue? The problem was the <strong>response body</strong>. Even though the body was always empty, the HttpResponse object contained the body as a stream. And if this stream isn’t consumed, the <strong>connection for the request is not freed again</strong>. We never consumed this stream because we only checked the status of the response. That meant that for each new user that accepted the terms and conditions, a new <strong>connection is taken from the pool, but never freed</strong> after the request is done.</p><p>Eventually the connection was freed after a timeout was reached. But this took too long and the size of the connection pool was too small for the amount of users accepting the terms and conditions. The fix for this bug was only one line:</p><pre>HttpClientUtils.closeQuietly(response);</pre><p>This consumed the stream and the connection was properly freed and went back to the connection pool for the next request. It took the whole monday and part of tuesday to get to this solution and from then on the system has been running smoothly without any major hiccups.</p><h3>Remember the first mail provider?</h3><p>Remember the first mail provider that silently swallowed our mails but didn’t deliver any of them? Turns out <strong>they silently blocked our account without informing us</strong>. Even when we actually called their support and asked for help, they didn’t tell us. They most likely blocked us, because we were using the mail server on all our stages with varying IP addresses. What an easy way to lose customers. But in the end, we were better off with the internal mail server anyway (if only it had been discovered earlier ¯\_(ツ)_/¯).</p><h3>Lessons learned 📝</h3><p>What do we learn from this epic story of failures?</p><ul><li>Sending mails is hard. <strong>Don’t ever underestimate sending mails</strong>.</li><li>Keep calm. It’ll eventually work out. Just keep going. And don’t blame the others!</li><li>Learn from the mistakes.</li><li>You deserve that beer after all of this. 🍺</li></ul><p><strong>Now tell me: What have been your worst experiences in software projects and how did you work them out?</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f64dbbb1d89b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/holisticon-consultants/this-is-fine-when-everything-goes-wrong-on-release-day-f64dbbb1d89b">This is fine — When everything goes wrong on release day</a> was originally published in <a href="https://medium.com/holisticon-consultants">Holisticon Consultants</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make your Angular tests faster with automatic TestBed module teardown]]></title>
            <link>https://joostio.medium.com/make-your-angular-tests-faster-with-automatic-testbed-module-teardown-1a49d133beea?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a49d133beea</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[testbed]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Thu, 01 Jul 2021 13:59:50 GMT</pubDate>
            <atom:updated>2021-07-01T13:59:50.601Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fQBX16SD2vUOa1Gi" /><figcaption>Photo by <a href="https://unsplash.com/@chrisliverani?utm_source=medium&amp;utm_medium=referral">Chris Liverani</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Angular v12.1.0 introduces a new opt-in <strong>option </strong>to speed up your tests by automatically tearing down each TestBed after the tests have run.</p><p>To enable this feature use the new teardown option from TestBed. You can either enable it on the application level by passing the option to TestBed.initTestEnvironment or on the test suite level by passing it to TestBed.configureTestingModule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8fcaed4f2b7ab16f0b9a53a45cd4cf6/href">https://medium.com/media/a8fcaed4f2b7ab16f0b9a53a45cd4cf6/href</a></iframe><p>The TestBed will automatically be teared down after each test to boost performance and properly remove all the module references.</p><p>This fixes the issue (<a href="https://github.com/angular/angular/issues/18831">#18832</a>) where onDestroy handlers on PlatformRef and NgModuleRef as well as ngOnDestroy on providers were not called because the module was never properly destroyed.</p><p>Additionally this reduces the amount of elements in the DOM while testing because the test components and their styles as well as the test root elements are now removed after each test.</p><p>See the <a href="https://github.com/angular/angular/pull/42566">Pull Request</a> for more details.</p><p>Want to know more about Angular? See my other posts for more cool tech.</p><ul><li><a href="https://medium.com/holisticon-consultants/angular-bundle-size-reduction-ae244cd78ee0">Honey, I shrunk the bundle 📦— A story about Angular bundle size reduction</a></li><li><a href="https://medium.com/holisticon-consultants/angular-i18n-picking-the-right-tool-for-the-job-b543460801db">Angular i18n 🌐— Picking the right tool for the job</a></li><li><a href="https://medium.com/ngxp/optimize-angular-component-test-performance-ed1b261a2d97">⏰ Optimize Angular Component Test Performance</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a49d133beea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Honey, I shrunk the bundle — A story about Angular bundle size reduction]]></title>
            <link>https://medium.com/holisticon-consultants/angular-bundle-size-reduction-ae244cd78ee0?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae244cd78ee0</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[webpack-bundle-analyzer]]></category>
            <category><![CDATA[bundle-size]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Mon, 07 Jun 2021 07:48:47 GMT</pubDate>
            <atom:updated>2021-06-07T07:48:47.019Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E63Q4W8BazSUPE9C" /><figcaption>node_modules in the real world | Photo by <a href="https://unsplash.com/@purzlbaum?utm_source=medium&amp;utm_medium=referral">Claudio Schwarz | @purzlbaum</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently I joined a team developing an Angular application that made heavy use of third-party libraries. One main problem we were facing was the bundle size and the overall performance of the application in the browser. It felt non-responsive and slow. So I began investigating. This articles will guide you through my journey in reducing the overall production bundle size from around <strong>7.5MB</strong> to <strong>less than 5MB</strong>.</p><h3>Initial findings</h3><p>To analyze the initial state of the bundle I used the <a href="https://github.com/webpack-contrib/webpack-bundle-analyzer">webpack-bundle-analyzer</a> . By running the Angular build with the --stats-json flag, webpack will generate a stats.json file in the dist folder which you can analyze with the CLI provided by the webpack-bundle-analyzer .</p><pre># Install the webpack-bundle-analyzer CLI globally<br>npm i -g webpack-bundle-analyzer</pre><pre># Build the app<br>ng build --prod --stats-json</pre><pre># Analyze the stats.json <br>webpack-bundle-analyzer ./dist/app/stats.json</pre><p>The webpack-bundle-analyzer will then start a local webserver that will give you a visual overview of your generated bundles including all the third-party dependencies and their size.</p><p>With a total size of <strong>7.29MB </strong>the overview looked like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6vhFlwPN5-ca459ypNQC5w.png" /><figcaption>Bundle overview before</figcaption></figure><p>From this overview I gathered the following information:</p><ul><li>The highcharts library includes several different chart-types like highcharts-gantt and highmaps we did not use in our application at all.</li><li>The whole bundle of moment as well as moment-timezone are included even though we only use a fraction of the provided functions and locales.</li><li>The whole lodash library is included but very few functions are actually used in our code.</li><li>Some @syncfusion packages appear twice in different contexts like ej2-base and ej2-inputs .</li></ul><p>With these information gathered, I continued to the next step of evaluating possibilities to reduce, replace or remove the mentioned bundles.</p><h3>Spring-cleaning 🧹</h3><h4>1. Highcharts</h4><p>The first step was to analyze why all the different chart types for highcharts were included in our bundle even though we only used one type. So I waded through the code and checked all references to highcharts . The only thing seemingly suspicious were several plugins we used. So I removed all the plugins, built the application, but the unused chart types were still there 😕</p><p>The only other thing in our code related to highcharts was the third-party library <a href="https://github.com/cebor/angular-highcharts">angular-highcharts</a> that acted as a wrapper around highcharts and provided Angular integration. Luckily the library was open-source, so I had a look at the source code on GitHub and saw a few potential causes for the issue. There were multiple imports to the different chart types in the code which imported the whole package.</p><pre>import * as Highcharts from &#39;highcharts/highcharts-gantt&#39;;</pre><pre>import * as Highstock from &#39;highcharts/highstock&#39;;</pre><pre>import * as Highmaps from &#39;highcharts/highmaps&#39;;</pre><p>To test my suspicion I copied the library’s relevant code into our application and removed the import references of the unused chart types and rebuilt our application.</p><p>🎉 Success! webpack-bundle-analyzer showed that the size of highcharts decreased by around <strong>60% </strong>or<strong> 1MB </strong>and the offending packages were gone. One down, three to go!</p><h4>2. Moment.js</h4><p>moment has been a problem in several projects I worked in. The problem is that tree-shaking the unused locales does not work out of the box in Angular. So the whole moment library is included in the final app bundle with all of the locales you might never even use.</p><p>In addition, the Moment team <a href="https://momentjs.com/docs/#/-project-status/">announced that they generally consider Moment to be a legacy project in maintenance mode.</a> So that’s another reason I wanted to move away from moment and to a more actively developed alternative. In the Moment team’s statement they mention a few recommendations in terms of alternatives to Moment. After looking through those I chose <a href="https://day.js.org/">Day.js</a> because it provides a similar API to moment so the migration path should be simple.</p><p>In our code I replaced all the moment imports and function calls with dayjs and was mostly done. I had to extend dayjs with a few plugins like <a href="https://day.js.org/docs/en/plugin/utc">utc</a> and <a href="https://day.js.org/docs/en/plugin/timezone">timezone</a> for everything to work but in general the migration was painless.</p><p>🎉 Another success! By replacing moment with dayjs we saved around <strong>500kB</strong> or around <strong>98%</strong> of the initial size of moment and moment-timezone . Half way there!</p><h4>3. lodash</h4><p>lodash has a similar problem to moment . When using the library in an Angular context, tree-shaking does not work, so the whole lodash library will be included in your bundle once you use it. Luckily the lodash team provides an alternative called lodash-es that uses ECMAScript modules to enable proper tree-shaking capabilities with Angular and webpack.</p><pre># Before<br>import * as _ from &#39;lodash&#39;;</pre><pre>_.isNull(variable);<br>_.entries(variable, ...);</pre><pre># After<br>import { entries, isNull } from &#39;lodash-es&#39;;</pre><pre>isNull(variable);<br>entries(variable, ...);</pre><p>🎉 Success! This one was easy but also saved around <strong>400kB </strong>or <strong>80%</strong> of the initial size of lodash. On to the last one!</p><h4>4. Syncfusion</h4><p>This one was harder. The issue with syncfusion was that some parts of the package were included multiple times in our application bundle. When searching for references to @syncfusion/* in our code I saw that we used the @syncfusion/ej2-angular-querybuilder library directly and the @syncfusion/ej2-angular-calendars library through a shared corporate library.</p><p>At first, I thought the corporate library was incorrectly including the @syncfusion/ej2-angular-calendars package in their bundle. But this suspicion turned out to be wrong as it was only listed as a peerDependency and wasn’t part of the library’s bundle.</p><p>When looking at the overview from the webpack-bundle-analyzer again I noticed that the duplicate packages had slightly different names. There was ej2-base and ej2-base.es2015 , ej2-inputs and ej2-inputs.es2015 and so on. This time I thought I found the issue and had a look at the tsconfig of the corporate library to see if it used a different target than our app, so there were different versions of the same package. But this also turned out to be wrong.</p><p>Lastly I turned to our package.json and it hit me. We were using @syncfusion/ej2-angular-calendars in version 18.x.x and @syncfusion/ej2-angular-querybuilder in version 19.x.x . So i updated both to be on the same version, rebuilt the application and the duplicate packages were gone!</p><p>🎉 Final success! This saved another good chunk and our total bundle size decreased to just under <strong>5MB</strong>.</p><h3>Where are we now?</h3><p>After all the spring-cleaning action I reran the webpack-bundle-analyzer and looked at the result with a content smile. The bundle went from <strong>7.29MB</strong> to <strong>4.96MB</strong> with a few days of work. That’s almost a third of the whole bundle size gone!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g_vHTsidJPZLbXrsK6KyDA.png" /><figcaption>Bundle overview after</figcaption></figure><p>In the new overview you can see that the problematic packages either heavily decreased in size or nearly vanished. As an example the new dayjs library is so small it’s hardly noticeable in the overview.</p><p>All in all I have to say I am very happy with the results. The app feels more performant and snappier than before. In addition, the build time also decreased, so that’s another win.</p><h3>Is that all?</h3><p>Most definitely not. <strong>5MB</strong> is still a lot for the type of application we provide. So I will keep on working to further reduce our bundle size. There are some things on my list like evaluating alternatives to the @syncfusion components and properly integrating lazy-loading for rarely used routes or functionality of our app.</p><p>Keeping your bundle size low is an ongoing task that every developer should consider when working on new features. Usually it does not have to be a third-party library because it might take up more bundle size than you think. So keep an eye out when choosing your next library to not be overwhelmed by its sheer size. 😉</p><p>🎉 You scrolled to the end! Thank you for reading! When was the last time you had a look at your bundle size? And how do you keep your bundle size low?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae244cd78ee0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/holisticon-consultants/angular-bundle-size-reduction-ae244cd78ee0">Honey, I shrunk the bundle 📦— A story about Angular bundle size reduction</a> was originally published in <a href="https://medium.com/holisticon-consultants">Holisticon Consultants</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular i18n — Picking the right tool for the job]]></title>
            <link>https://medium.com/holisticon-consultants/angular-i18n-picking-the-right-tool-for-the-job-b543460801db?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/b543460801db</guid>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[translation]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[internationalization]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Fri, 09 Apr 2021 07:17:46 GMT</pubDate>
            <atom:updated>2021-04-09T10:46:10.897Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7D0rk3rb7iK3SJrB" /><figcaption>Photo by <a href="https://unsplash.com/@farzadme?utm_source=medium&amp;utm_medium=referral">Farzad Mohsenvand</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Internationalization is hard. And it gets even harder if you have to decide which approach to use when managing your translations. For Angular application you have two ways of using the translated texts:</p><ul><li><strong>Translations at runtime</strong></li><li><strong>Translations at compile-time</strong></li></ul><p>Let’s compare the two approaches and see which one’s the best 🤓:</p><h3>Translations at runtime 🏃‍♂️</h3><p>Managing your translations at runtime usually involves some kind of dictionary file that maps translation keys to the translated text. Popular libraries like <a href="https://github.com/ngx-translate/core"><strong>ngx-translate</strong></a> and <a href="https://ngneat.github.io/transloco/"><strong>transloco</strong></a><strong> </strong>follow this approach and normally use JSON files as dictionaries.</p><p>In your HTML template you then pass the translation key to a service or pipe which takes the corresponding translation text from the dictionary and renders the localized text.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db97bc7dab18788d04b8b0c8303240c3/href">https://medium.com/media/db97bc7dab18788d04b8b0c8303240c3/href</a></iframe><p>This approach also gives the flexibility of using translations inside your Typescript code using a service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f292307ea11877f17d3d19940c4fe324/href">https://medium.com/media/f292307ea11877f17d3d19940c4fe324/href</a></iframe><h4>How it works</h4><p>This approach is not very complicated. You have a dictionary file which maps translation keys to translated text. Inside your code you only use the translation keys and at runtime you resolve these keys from the dictionary file and render the text that corresponds to the given keys.</p><p>For every language you have one dictionary file. Each file includes all the translation keys used in your app and maps to the translated text of the chosen language.</p><p>As an example see this snippet for the dictionary of translations for a registration form in english. So this is plain JSON which allows for grouping and nesting the translated texts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/525a250fc07be6cd6fe8be29910652fd/href">https://medium.com/media/525a250fc07be6cd6fe8be29910652fd/href</a></iframe><h4>Why?</h4><p>Using this approach is pretty <strong>simple</strong>. Install a library, add a dictionary file, initialize it and you’re pretty much set. It’s very <strong>easy to use</strong> and gives the <strong>flexibility </strong>of using translations everywhere without much hassle.</p><p>Another advantage is the ability to <strong>change the language at runtime without reloading</strong> the whole application. Changing the language just replaces all the rendered texts with texts from a different dictionary file. Done 🧙‍♂️.</p><h4>Why not?</h4><p>One of my major gripes with this approach is the <strong>developer experience</strong> with handling translations. In your Typescript code and HTML templates you always use translation keys instead of the real text. So you have to jump between files to see what’s actually being rendered. VSCode plugins like <a href="https://marketplace.visualstudio.com/items?itemName=apk27.ngx-translate-lookup">ngx-translate-lookup</a> definitely help but it’s still a <strong>disconnect between your code and the translations</strong>.</p><p><strong>The JSON dictionary file is a non-standard file format for managing translations</strong> which means there is no translation-specific tool for editing this file. If you want a third-party service to do the translations for you, this will result in more work for the translator.</p><p>Another thing is <strong>performance and bundle size</strong>. Having another library adds to the bundle size. Loading translations at runtime negatively impacts the application’s performance. Even when including the dictionary files in the bundle to boost the performance it still adds to the total bundle size.</p><p>Lastly it’s hard to manage the dictionary file properly. <strong>Translation keys are basically magic strings</strong>. If you make a typo the translated text can not be found. If you forget to delete a translation in the dictionary no compiler or linter will complain about it. There are of course plugins or other libraries that do this for you but I still feel like to general approach is flawed.</p><h3>Translations at compile-time 👨‍💻</h3><p>The other approach of managing translations is resolving them at compile-time which is how the official <a href="https://angular.io/guide/i18n">Angular i18n library</a> handles them.</p><blockquote>You don’t need a special service or JSON translation dictionary anymore. Just use the real text in a language of your choice.</blockquote><p>Instead of using translation keys in development you use the actual localized text inside your code and <strong>mark the text for translation</strong>. Angular i18n uses the i18n HTML attribute to do this in templates.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/221da4301883972553f4f301ffffe038/href">https://medium.com/media/221da4301883972553f4f301ffffe038/href</a></iframe><p>In your Typescript code this is done by using the tagged template function $localize. This also marks the displayed text for translation just like the i18n attribute does.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/429c12fa090d5b857592b28a33c5a493/href">https://medium.com/media/429c12fa090d5b857592b28a33c5a493/href</a></iframe><h4>How it works</h4><p>Before compiling the application you run an <strong>extractor </strong>that scans all files for the marked translation texts and extracts them into a single file. Every entry is given a unique ID based on various parameters like file path, name, text etc. This is the <strong>main translation file</strong>.</p><p>For every language the Angular application should be served in, this <strong>translation file is then copied</strong> and the texts used in development are <strong>replaced with the proper translations</strong> for the chosen language.</p><p>At compile-time the application is compiled <strong>once</strong>. For this build all the texts used in development are replaced with the unique IDs from the <strong>main translation file</strong>. Then for <strong>every language</strong> the bundle is copied and all the IDs are <strong>replaced with the corresponding translation texts</strong>. So you end up with a <strong>bundle for every language</strong> the application should support.</p><h4>Why?</h4><p>So why should you use this approach if you have the extra overhead of extracting translations every time some translated text is changed or added and building separate applications for every language?</p><p>The answer pretty much boils down to: <strong>Performance</strong>.</p><p>Instead of having to load JSON dictionaries and replacing translation keys at runtime the translated texts are already <strong>baked into the application bundle</strong>. There is no need for an additional library at runtime. And the increased performance of the application ultimately leads to a <strong>better user experience</strong>.</p><p>In comparison to translations at runtime you don’t have dictionaries with translations keys so there is <strong>no disconnect between translations and code </strong>and also <strong>no need to manually maintain a dictionary file</strong> because the extractor handles this for you. This simplification allows for a better <strong>developer experience</strong>.</p><p>Lastly the generated translation files do not use JSON as file format but instead rely on <strong>XLIFF which is a XML-based standard file format for internationalization</strong>. This means there are various tools for working with translations and most third-party services will most likely support them too.</p><p>As an example see the snippet below for a XLIFF file generated by the Angular CLI. At the top of the file you see the source-language=&quot;en&quot; and target-language=&quot;de&quot; . This defines that we are translating from english to german.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16f0c127c4665b2aa13bb9e9057e09d6/href">https://medium.com/media/16f0c127c4665b2aa13bb9e9057e09d6/href</a></iframe><p>Every text that should be translated is a trans-unit which is uniquely identified by an <strong>id</strong>. This unit includes a source which is text extracted from the HTML template or TypeScript file. The target is the localized text, in this case german. The localized text is added manually by the person who translates this text so this will be empty initially. Everything else is automatically generated by the Angular CLI.</p><p>This file is then used at compile-time to generate an app bundle with german localization.</p><p><em>Note that you will most likely never add translation by directly editing this file but instead use proper localization tools that handle XLIFF files.</em></p><h4>Why not?</h4><p>There is definitely a <strong>learning curve</strong> accompanied with this approach. With translations at runtime you get the translated text as a string and just use it like every other string in Angular. Compile-time translations require you to learn about proper translation text marking and also the <a href="http://userguide.icu-project.org/formatparse/messages"><strong>ICU Message Format</strong></a>. This message format is mandatory if you want to use dynamic values or pluralization in translation texts with the official <a href="https://angular.io/guide/i18n">Angular i18n library</a>.</p><p>Multiple bundles mean <strong>no language switching without reloading</strong>.<strong> </strong>If the user changes the language of the application he will be forwarded to a different bundle which causes a whole reload of the application. Persisting state in between bundles is definitely possible with LocalStorage or ServiceWorkers but a lot more complicated than with translations at runtime.</p><h3>Conclusion 🤷‍♂️</h3><p>So which of the two approaches is the correct one?</p><p>There is no definitive answer to this question but after having worked in multiple enterprise projects with both of these approaches <strong>I very much prefer translations at compile-time </strong>with <a href="https://angular.io/guide/i18n">Angular i18n</a>.</p><p>The Angular team did a very good job with <a href="https://www.npmjs.com/package/@angular/localize">@angular/localize</a> and with Angular 10.1 the extraction of translation texts marked with the $localize tagged template function now works out of the box. In future projects I will definitely use this approach if internationalization is a requirement 👍</p><p><strong>Whats your opinion? Which approach would you choose and why?</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b543460801db" width="1" height="1" alt=""><hr><p><a href="https://medium.com/holisticon-consultants/angular-i18n-picking-the-right-tool-for-the-job-b543460801db">Angular i18n 🌐— Picking the right tool for the job</a> was originally published in <a href="https://medium.com/holisticon-consultants">Holisticon Consultants</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ Creating and publishing your Angular library with Travis CI ]]></title>
            <link>https://medium.com/ngxp/creating-and-publishing-your-angular-library-with-travis-ci-272829c422a0?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/272829c422a0</guid>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[travis]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Fri, 17 Aug 2018 14:36:49 GMT</pubDate>
            <atom:updated>2018-08-17T14:36:49.070Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial I will show you how to create an Angular library and automatically publish it to <a href="https://www.npmjs.com/">NPM</a> using <a href="https://travis-ci.org/">Travis CI</a>.</p><h3>Create an Angular CLI project ✨</h3><p>First you create an Angular project using the Angular CLI and then you generate the library project.</p><pre>ng new my-first-library-project<br>cd my-first-library-project<br>ng generate lib my-first-lib</pre><p>The <strong>my-first-library-project</strong> acts as a wrapper project you can use as demo application. The <strong>my-first-lib</strong> project will be created inside the <strong>projects</strong> folder.</p><h3>Add package.json metadata 🔧</h3><p>It is recommended to add additional metadata to the <strong>package.json</strong> of your library. This can contain information about the author, github repository as well as a description and some keywords. All this helps NPM display helpful information about your package. So be sure to add those fields.</p><p>Add the following fields to the <strong>package.json</strong> located in <strong>projects/my-first-lib</strong>. ⛔ Do not edit the <strong>package.json</strong> in the root project!</p><pre>&quot;name&quot;: &quot;my-first-lib&quot;,<br>&quot;author&quot;: {<br>    &quot;name&quot;: &quot;You&quot;,<br>    &quot;email&quot;: &quot;you@domain.com&quot;<br>},<br>&quot;publishConfig&quot;: {<br>    &quot;access&quot;: &quot;public&quot;<br>},<br>&quot;repository&quot;: {<br>    &quot;type&quot;: &quot;git&quot;,<br>    &quot;url&quot;: &quot;https://github.com/you/my-first-lib&quot;<br>},<br>&quot;description&quot;: &quot;My very first library. It&#39;s awesome!&quot;,<br>&quot;keywords&quot;: [<br>    &quot;my&quot;,<br>    &quot;very&quot;,<br>    &quot;first&quot;,<br>    &quot;library&quot;<br>],<br>&quot;homepage&quot;: &quot;https://github.com/you/my-first-lib#readme&quot;,<br>&quot;bugs&quot;: {<br>    &quot;url&quot;: &quot;https://github.com/you/my-first-lib/issues&quot;<br>}</pre><p>You can also prepend the name with a scope e.g. @mynpmusername/my-first-lib or @myorg/my-first-libto publish the library with a scope.</p><h3>Add Travis Integration 📦</h3><p>Before you can use the Travis CI you need to register on the <a href="https://travis-ci.org/">Travis CI website</a>. You can then connect your Github account and add your public repositories to the automated Travis integration pipeline. Each time changes are pushed to these repositories, Travis will execute.</p><p>To configure Travis you need to create a <strong>.travis.yml</strong> file inside your projects root folder.</p><p>The following config is very simple and will run the <strong>build-ci</strong> NPM script everytime changes are pushed to the repository. The <strong>build-ci</strong> script builds the library using Angular CLI.</p><pre>&quot;build-ci&quot;: &quot;ng build my-first-lib --prod&quot;</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccfae9ffe9124cdd34f0fb373960d540/href">https://medium.com/media/ccfae9ffe9124cdd34f0fb373960d540/href</a></iframe><h3>Executing tests 🔍</h3><p>Before publishing a library all tests for it should be executed so no faulty version is pushed to the NPM registry.</p><p>When using <strong>Karma + Jasmine</strong> you have to configure some more things. First you need to add chrome to the Travis config so the tests can run in a browser.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7f3fe2f1a5390f629ede9262d26b4ac6/href">https://medium.com/media/7f3fe2f1a5390f629ede9262d26b4ac6/href</a></iframe><p>Additionally you need to add a <strong>test-ci</strong> NPM script that executes the tests in the Travis environment.</p><pre>&quot;test-ci&quot;: &quot;ng test my-first-lib --watch=false --browsers=ChromeHeadless&quot;</pre><p>The parameters will execute the tests only once and make them run in Chrome Headless browser. For this to work you add the browser as <strong>customLauncher </strong>to the <strong>karma.conf.js</strong> inside the <strong>projects/my-first-lib</strong> folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a56605af95c9a96cb1d6963e49e3977/href">https://medium.com/media/8a56605af95c9a96cb1d6963e49e3977/href</a></iframe><h3>Publishing to NPM 🎉</h3><p>To publish your awesome library you need to install the <a href="https://github.com/travis-ci/travis.rb">Travis CLI</a>. When installed execute the following command in the folder containing the <strong>.travis.yml</strong>:</p><pre>travis setup npm</pre><p>The script will ask you for all relevant data as well as a NPM token. The Travis CLI automatically encrypts this token so it is save to use this generated config in a public repository.</p><p>Before publishing you need Travis to navigate to the directory the build artifacts of your library were created in. These were created by the <strong>build-ci</strong> script.</p><pre>before_deploy:  <br>- cd dist/my-first-lib</pre><p>And make sure Travis does not delete these files before publishing:</p><pre>skip_cleanup: true</pre><p>The finished Travis config now looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7f2f5de36a8d0953d7ce5e6647f4f16/href">https://medium.com/media/b7f2f5de36a8d0953d7ce5e6647f4f16/href</a></iframe><h3>Triggering a new deployment 🔨</h3><p>You now have everything in place for Travis to automatically publish your library to NPM. But Travis will only deploy new versions of the library if you push a tagged commit. See <strong>line 19</strong> in the Travis config.</p><p>Luckily NPM provides a tool <strong>(npm version) </strong>to automatically do that for you. There is just one minor quirk that you need to look out for.</p><p><strong>npm version</strong> does only work in directories that contain a <strong>.git</strong> directory. But the <strong>projects/my-first-lib</strong> folder does not contain a <strong>.git</strong> directory. So you need to create an empty <strong>.git </strong>directory there.</p><p>Then you can run <strong>npm version</strong> inside the <strong>projects/my-first-lib</strong> folder and the script will automatically bump the version in the <strong>package.json,</strong> add a commit and tag it with the new version.</p><pre>cd projects/my-first-lib<br>mkdir .git<br>npm version (major|minor|patch)</pre><p>Now you can push the commit to Github using</p><pre>git push --follow-tags</pre><p>And Travis will automatically test, build and publish your library 🎉🤘</p><p>Thanks for reading. Don’t forget to clap 👏 if this was helpful and be sure to check out my other articles 😊</p><ul><li><a href="https://medium.com/ngxp/optimize-angular-component-test-performance-ed1b261a2d97">⏰ Optimize Angular Component Test Performance</a></li><li><a href="https://medium.com/ngxp/automatically-unsubscribing-from-subscriptions-inside-angular-components-a327006b467b">Automatically unsubscribing from subscriptions inside Angular components</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=272829c422a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ngxp/creating-and-publishing-your-angular-library-with-travis-ci-272829c422a0">📖 Creating and publishing your Angular library with Travis CI 💡</a> was originally published in <a href="https://medium.com/ngxp">ngxp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[⏰ Optimize Angular Component Test Performance]]></title>
            <link>https://medium.com/ngxp/optimize-angular-component-test-performance-ed1b261a2d97?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed1b261a2d97</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[unittest]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[component-test]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Wed, 15 Aug 2018 10:50:42 GMT</pubDate>
            <atom:updated>2018-08-15T11:55:24.533Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_2ZfRJiHVTynr_Ld" /><figcaption>Waiting for Angular component tests to finish executing…</figcaption></figure><p><em>For a german version of this article visit the </em><a href="https://blog.holisticon.de/2018/08/angular-komponententests-zeit-und-nerven-sparen/"><em>Holisticon blog</em></a><em>.</em></p><p>Component Tests in Angular are expensive and take a lot of time. That’s why the testing module should include only what is necessary. From our experiences in multiple Angular projects we learned a lot of helpful tips and tricks regarding Angular component tests. This allowed us to save around 90% test execution time. I want to share these tips and tricks with you so you can not only save a lot of time but also a lot of hassle.</p><h3>Background</h3><p>In one of our projects the test execution of the Angular tests took around 8–9 minutes. The application had around 100 components and 1000 tests. Because of the long test execution time the automatic deployment via Jenkins and all subsequents processes were delayed. That meant a new build was deployed 15–20 minutes after the commit. This delay was the crucial factor why we decided to analyze our Angular tests. With the new gained knowledge from the analysis we were able to optimize all tests and reduce the test execution time to under 1 minute. As a result we save around 6 hours of time each week as well as a huge amount of developer hassle. ⏳😄</p><h3>💡 Tips</h3><p>Before I start with the actual tips, I want to define the general outline of this post. Let’s start by asking why a component should be tested. The goal is to test the functionality of the component. That means we want to create an isolated unit test. But in a lot of cases the testing module imports more than is needed for a unit test. Because of this the unit test becomes an unwanted integration test. That’s problematic as every <em>it</em>-Block inside the test instantiates the testing module, which is expensive. The more modules, components and services are imported in the testing module, the longer the component test takes. To reduce this time I want to give you some tips on how to create isolated Angular component tests and reduce the total test execution time ⏰.</p><h3>1. Declare components instead of importing modules 📦</h3><p>In every unit test of a component the testing module is instantiated. Instead of importingthe whole module a component is declared in, you should declare the component inside the testing module itself. Otherwise all other imported modules, declared components and services are also imported into the testing module, even though they are not releveant for the test.</p><pre>import { AppComponent } from &#39;app/app.component&#39;;<br>    ...<br>TestBed.configureTestingModule({<br>    ...<br>    declarations: [<br>        AppComponent<br>    ],<br>    ...<br>})</pre><p>Not every component test can simply be optimized this way. In a lot of cases the component has dependencies on other components, services, pipes or directives. If these are not included in the testing module, the test will fail. But importing them is not the solution as the isolated unit test will then become an integration test. Hence the following tips will give you an overview on how to test components that have additional dependencies.</p><h3>2. Use the NO_ERRORS_SCHEMA 💣</h3><p>To create an isolated unit test of a component only the functionality of the component and not its interaction with other child components should be tested. But if the child components are not declared in the testing module and the component references them in the template, the test will fail. This can be mitigated using the <strong>NO_ERRORS_SCHEMA</strong>. You simply include it inside the <strong>schemas</strong> array in the testing module. This way all referenced components and directives inside the template won’t give you errors if they are not included in the testing module.</p><pre>import { NO_ERRORS_SCHEMA } from &#39;@angular/core&#39;;<br>...<br>TestBed.configureTestingModule({<br>    ...<br>    schemas: [<br>        NO_ERRORS_SCHEMA<br>    ],<br>    ...<br>})</pre><h3>3. Mock Services 🔨</h3><p>If a component uses a service the service should be mocked. You don’t want to test the service and usually not every method of a service is used inside a component.</p><h4>Component</h4><pre>@Component({<br>    selector: &#39;app-root&#39;,<br>    templateUrl: &#39;./app.component.html&#39;,<br>    styleUrls: [&#39;./app.component.scss&#39;]<br>})<br>export class AppComponent {</pre><pre>    constructor(<br>        private authenticationService: AuthenticationService,<br>    ) {<br>        this.authenticationService.init()<br>    }<br>}</pre><h4>Test</h4><pre>const authenticationServiceMock: AuthenticationService = {<br>    init(): void {}<br>};</pre><pre>TestBed.configureTestingModule({<br>    declarations: [<br>        AppComponent<br>    ],<br>    providers: [<br>        {<br>            provide: AuthenticationService,<br>            useValue: authenticationServiceMock<br>        }<br>    ]<br>})</pre><p>Every <strong>@Injectable </strong>class<strong> </strong>can be mocked using this method. That means other class instances can also be mocked inside the testing module. More detailed information on <strong>providers</strong> can be found on the Angular website: <a href="https://angular.io/guide/dependency-injection#the-provide-object-literal">Dependency Injection</a>.</p><h3>4. Mock Pipes 📐</h3><p>In most cases mocking pipes is not incredibly beneficial to the test performance. If the pipe does simple text transformation a mocked pipe wouldn’t have a great impact on the test performance. In those cases the original pipe should be added to the testing module instead of a mocked one.</p><p>If the pipes are complex or heavy, mocking them can mean performance improvements. Examples of complex and heavy pipes are pipes that have dependencies to other services or pipes that perform asynchronous operations. To mock pipes you create a pipe inside your test that has the same <strong>name</strong> and include it in the declarations of the testing module</p><h4>Component</h4><pre>@Component({<br>    selector: &#39;user-birthday&#39;,<br>    template: `<br>        &lt;div&gt;{{ user.birthday | date }}&lt;/div&gt;<br>    `,<br>    styles: [&#39;&#39;]<br>})<br>export class UserBirthdayComponent {<br>    @Input()<br>    user: User;<br>}</pre><h4>Test</h4><pre>@Pipe({<br>    name: &#39;date&#39;<br>})<br>export class MockDatePipe implements PipeTransform {<br>    transform(value: any, ...args: any[]) {<br>        return value;<br>    }<br>}<br>...<br>TestBed.configureTestingModule({<br>    declarations: [<br>        UserBirthdayComponent,<br>        MockDatePipe<br>    ]<br>})</pre><h3>5. Mock Directives 🔧</h3><p>Directives are somewhat of a special case. If the directive is used as a attribute inside the template the <strong>NO_ERRORS_SCHEMA</strong> will catch it. So there is no need to mock it.</p><pre>&lt;input <br>  class=&quot;form-control&quot; <br>  ngbTooltip=&quot;This is a tooltip&quot;&gt;<br>&lt;/input&gt;</pre><p>If the directive is being exported and referenced in the template or the component, it has to be included in the testing module. Otherwise the reference is not defined and you will get errors.</p><pre>&lt;input <br>  class=&quot;form-control&quot; <br>  ngbTooltip=&quot;This is a tooltip&quot; <br>  #tooltip=&quot;ngbTooltip&quot;&gt; // Direktive wird exportiert<br>&lt;/input&gt;<br>&lt;button (click)=&quot;tooltip.close()&quot;&gt;Close&lt;/button&gt;</pre><p>If the directive is being exported as shown above, the same rule as for the Mock Pipes applies. Mocking simple and lightweight directives will not improve test performance. Only very complex and heavy directives affect the test performance and thus should be mocked. To do this you create a directive with the same <strong>selector</strong> and <strong>exportAs</strong> name as it is used in the template. You then add it to the declarations of the testing module.</p><h4>Component</h4><pre>@Component({<br>    selector: &#39;user-popover&#39;,<br>    template: `<br>        &lt;div #popover=&quot;ngbPopover&quot;&gt;{{ user.name }}&lt;/div&gt;<br>    `,<br>    styles: [&#39;&#39;]<br>})<br>export class UserPopoverComponent implements OnDestroy {<br>    @ViewChild(&#39;popover&#39;)<br>    popover: NgbPopover;</pre><pre>    ngOnDestroy() {<br>        this.popover.close();<br>    }<br>}</pre><h4>Test</h4><pre>@Directive({selector: &#39;[ngbPopover]&#39;, exportAs: &#39;ngbPopover&#39;})<br>export class NgbPopoverMockDirective {<br>    close(): void {}<br>}<br>...<br>TestBed.configureTestingModule({<br>    declarations: [<br>        UserPopoverComponent,<br>        NgbPopoverMockDirective<br>    ]<br>})</pre><p>Importing whole modules inside component unit tests generally imports a lot more than is needed. This means the unit test will become an integration test that takes a lot more time to execute. That’s why it’s beneficial to isolate the component inside the unit test and mock complex services, pipes or directives.</p><blockquote>Not everything that can be mocked, should be mocked</blockquote><p>Performance improvements by mocking are often marginal. That’s why you should choose carefully which services, pipes and directives to mock.</p><p>I hope I could help you with these tips and wish you: Happy Testing! 🎉🤘</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed1b261a2d97" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ngxp/optimize-angular-component-test-performance-ed1b261a2d97">⏰ Optimize Angular Component Test Performance</a> was originally published in <a href="https://medium.com/ngxp">ngxp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatically unsubscribing from subscriptions inside Angular components]]></title>
            <link>https://medium.com/ngxp/automatically-unsubscribing-from-subscriptions-inside-angular-components-a327006b467b?source=rss-e9ad51f61d83------2</link>
            <guid isPermaLink="false">https://medium.com/p/a327006b467b</guid>
            <category><![CDATA[components]]></category>
            <category><![CDATA[subscription]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[automatically]]></category>
            <category><![CDATA[unsubscribe]]></category>
            <dc:creator><![CDATA[Joost Zöllner]]></dc:creator>
            <pubDate>Wed, 18 Jul 2018 17:11:46 GMT</pubDate>
            <atom:updated>2018-08-14T13:51:23.710Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o4b_UpdNoHE_46Ep" /><figcaption>“A focused man working on a sticker-covered laptop in a coffee shop” by <a href="https://unsplash.com/@punttim?utm_source=medium&amp;utm_medium=referral">Tim Gouw</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In a project we were working on we had the issue of having to manually unsubscribe subscriptions we created inside our Angular components. We did not like the solution we came up with so we decided to invest some more time and research if it is possible to automatically unsubscribe from all subscriptions when the component is destroyed. In this article we want to share what we learned and came up with.</p><p>Asynchronous code inside components is always pretty hard to manage and increases complexity. That’s why we always try to avoid it and use helpful things like Higher-Order-Components together with the async pipe. But it’s not always that easy.</p><p>Look at the following component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/467902689f1f08122c9eb1c70c17d9b6/href">https://medium.com/media/467902689f1f08122c9eb1c70c17d9b6/href</a></iframe><p>The component uses the actions observable stream from ngrx . It’s not very pretty as we have to manage all the subscriptions by hand. For that we specify an array where we save all our subscriptions . In the ngOnDestroy method we can the iterate over all our subscriptions and unsubscribe from each one.</p><p>But we don’t want that. Optimally the subscriptions live as long as our component and are unsubscribed when our component is destroyed. Let’s find out how to do that 🤞</p><h3>Automatically unsubscribing</h3><p>We initially searched if anyone had already come up with a solution. We found <a href="https://github.com/w11k/ng2-rx-componentdestroyed">https://github.com/w11k/ng2-rx-componentdestroyed</a> and an article from Netanel Basal <a href="https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88">https://netbasal.com/automagically-unsubscribe-in-angular-4487e9853a88</a> .</p><p>Both approaches helped us immensely but they weren’t the ideal solution we were looking for. So we decided to create our very own solution that matched our needs.</p><p>To achieve our dream of automatically unsubscribing all subscriptions inside our component, we wrote the RxJS operator called takeWhileAlive .</p><p>Use it by installing the take-while-alivepackage from NPM:</p><pre>npm install take-while-alive</pre><p>And now you can add the operator to <strong>automagically </strong>unsubscribe from all the subscriptions inside your components😍</p><p>To do that you first have to add the @AutoUnsubscibe() decorator to the component class. This will ensure everything is set up properly. Then simply add the takeWhileAlive(this) operator to each subscription that should be unsubscribed when the component is destroyed💣</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de91c276a930babae5115d1a6a9b1ffa/href">https://medium.com/media/de91c276a930babae5115d1a6a9b1ffa/href</a></iframe><p>The component looks a lot cleaner and we don’t have to worry about any memory leaks because of subscriptions that weren’t properly unsubscribed. Yay!</p><h3>How it works</h3><p>The takeWhileAlive operator uses the existing RxJS takeWhile operator. The takeWhile operator takes a function as parameter that is evaluated each time the observable stream fires. If the function evaluates totrue the stream will continue as usual. But if it evaluates tofalse the subscription will be unsubscribed. Pretty cool! 👍</p><p>The takeWhileAlive operator creates a property on the component that gets evaluated inside the takeWhile operator. Initially this value will be true so the stream will continue. But when ngOnDestroy is called the property will be set to false and thus the subscription will be unsubscribed. It does all the work for you 💪</p><p>The @AutoUnsubscribe() decorator is needed to create the ngOnDestroy lifecycle method. We need the method to set our property to false . If the method already exists it will keep the old one. What’s not so pretty about this approach is that we have to alter the component’s prototype . Angular only calls the ngOnDestroy lifecycle hook if it exists, when the component is instantiated. So it has to be created on the prototype . But this way we don’t have to implement the OnDestroy interface in the component. 😜</p><p>That’s it for our custom operator. I hope you learned something new 😊 If you have any feedback or ideas don’t hesitate to comment here or on <a href="https://github.com/joostme/takeWhileAlive">the Github page</a> of the project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a327006b467b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ngxp/automatically-unsubscribing-from-subscriptions-inside-angular-components-a327006b467b">Automatically unsubscribing from subscriptions inside Angular components</a> was originally published in <a href="https://medium.com/ngxp">ngxp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>