<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Charles Barnes</title>
    <description>The latest articles on DEV Community by Charles Barnes (@charlesabarnes).</description>
    <link>https://dev.to/charlesabarnes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F192137%2Fba4da784-e8e5-4161-868f-2e6cb8367fb6.png</url>
      <title>DEV Community: Charles Barnes</title>
      <link>https://dev.to/charlesabarnes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charlesabarnes"/>
    <language>en</language>
    <item>
      <title>Tips for Building Frontend AOT Angular Apps for Distribution</title>
      <dc:creator>Charles Barnes</dc:creator>
      <pubDate>Sat, 04 Jan 2020 07:04:45 +0000</pubDate>
      <link>https://dev.to/charlesabarnes/tips-for-building-frontend-aot-angular-apps-for-distribution-33lp</link>
      <guid>https://dev.to/charlesabarnes/tips-for-building-frontend-aot-angular-apps-for-distribution-33lp</guid>
      <description>&lt;p&gt;When working on the latest version of &lt;a href="https://github.com/bullhorn/career-portal"&gt;Bullhorn's Career Portal&lt;/a&gt; I ran into some issues that needed to be sorted.  The app is built once but uploaded to many different hosts by different users and companies. Because of this, I had to work around several issues that may arise when building for this audience. &lt;/p&gt;

&lt;p&gt;For background, the app is simple and contains 2 routes, the homepage, and the job page. There are several different configuration options available, and it works with any static web host. &lt;/p&gt;

&lt;p&gt;This post is my 5 tips for anyone attempting to go down a similar path, and hopefully, open up a discussion for other Angular tips and tricks. &lt;/p&gt;

&lt;h1&gt;
  
  
  1. Base Href
&lt;/h1&gt;

&lt;p&gt;The HTML5 &lt;code&gt;&amp;lt;base&amp;gt;&lt;/code&gt; tag is used to specify a base URL for relative links in the application.  The default configuration found in your index.html file would look like the following &lt;code&gt;&amp;lt;base href="/"&amp;gt;&lt;/code&gt;.  This value sets relative links to resolve from the root of your domain. For example, if you are at the page &lt;code&gt;chardeemacdennis.com/bird&lt;/code&gt; and it requests a resource at &lt;code&gt;data.json&lt;/code&gt;, the full URL for the JSON file would be &lt;code&gt;chardeemacdennis.com/data.json&lt;/code&gt;.  Since the app could be uploaded to any directory, this would be a problem.   &lt;/p&gt;

&lt;p&gt;In this instance, the proper configuration would be &lt;code&gt;&amp;lt;base href="."&amp;gt;&lt;/code&gt;.  This configuration would make any directory the files are placed in, be the root directory for the files.  In the &lt;code&gt;chardeemacdennis.com/bird&lt;/code&gt; example the &lt;code&gt;data.json&lt;/code&gt; file will resolve to &lt;code&gt;chardeemacdennis.com/bird/data.json&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Hash Location Strategy
&lt;/h1&gt;

&lt;p&gt;When using routing in your Angular application, it is suggested to set up your web server with a URL rewrite rule that allows Angular to fully handle routing.  Since the user potentially may not have access to edit these rules on shared hosting, it is easier to use &lt;a href="https://angular.io/api/common/HashLocationStrategy"&gt;HashLocationStrategy&lt;/a&gt;. When the user goes to the directory that your Angular application is in, it will put the Angular route after the hash symbol in the URL (ex. example.com#/foo).&lt;/p&gt;

&lt;p&gt;To use HashLocationStrategy, you can configure that in your application's &lt;code&gt;app.module&lt;/code&gt; file. The following is an example of how to do so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RouterModule.forRoot(appRoutes, { enableTracing: false, useHash: true},),
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  3. Simple Config file
&lt;/h1&gt;

&lt;p&gt;If an application is being used by a large number of people, there is a potential for a large number of settings to be added. It is probably easiest to use a JSON file for config, but ensure you are not utilizing too many settings or overly complex data structures.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Use AppInitializer
&lt;/h1&gt;

&lt;p&gt;To ensure the config is loaded properly when the application is first loaded, you can set your application settings by utilizing &lt;a href="https://angular.io/api/core/APP_INITIALIZER"&gt;APP_INITIALIZER&lt;/a&gt;.  For information about utilizing the App Initializer I suggest &lt;a href="https://dev.to/prestonjlamb/loading-app-configuration-in-the-appinitializer-304h"&gt;this article I found on the topic.&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Provide an Easy way to Update Translations
&lt;/h1&gt;

&lt;p&gt;To ensure your project reaches the largest audience possible, you will want to ensure your users can easily add translations to your project.  I will be writing more about this in the future, but for my project, to make it simple, I used a library called &lt;a href="https://github.com/bullhorn/chomsky"&gt;Chomsky&lt;/a&gt;.  If the config is correct, it will check the available translations to use, which can be useful for multi-lingual end-users. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>opensource</category>
      <category>discuss</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
