<?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: Andy Tu Hoang</title>
    <description>The latest articles on DEV Community by Andy Tu Hoang (@andyt2503).</description>
    <link>https://dev.to/andyt2503</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%2F1057647%2F86fd57a0-afae-455b-b0b5-c76b2e4c906b.jpeg</url>
      <title>DEV Community: Andy Tu Hoang</title>
      <link>https://dev.to/andyt2503</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andyt2503"/>
    <language>en</language>
    <item>
      <title>Angular: Fix 404 error after reloading Github Pages with Custom Angular CLI Builder</title>
      <dc:creator>Andy Tu Hoang</dc:creator>
      <pubDate>Mon, 03 Apr 2023 04:54:41 +0000</pubDate>
      <link>https://dev.to/andyt2503/angular-fix-404-error-after-reloading-github-pages-with-custom-angular-cli-builder-1270</link>
      <guid>https://dev.to/andyt2503/angular-fix-404-error-after-reloading-github-pages-with-custom-angular-cli-builder-1270</guid>
      <description>&lt;p&gt;Back to the first time I deployed my Angular project to Github Pages, I just run &lt;code&gt;ng run build&lt;/code&gt; and deployed the build artifacts to Github. Everything seem working ok. But then I got the issue that whenever I refresh my application, it return 404 Page. Obviously, I had to searching in the Google how to fix this issue. Fortunately, I found the way to fix this issue from &lt;code&gt;angular.io&lt;/code&gt;. I just need to build the application and then add a 404 page, copy index.html into 404.html. Everything works perfectly. But now, when I want update my page, I have to build and copy again and again. I wondered if I can automatically generate 404.html file after run &lt;code&gt;ng run build&lt;/code&gt;. And I found a solution that is using &lt;strong&gt;Custom Angular CLI Builders&lt;/strong&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Angular CLI builders?
&lt;/h2&gt;

&lt;p&gt;When working with Angular, we usually use some CLI commands like &lt;code&gt;ng serve&lt;/code&gt;, &lt;code&gt;ng build&lt;/code&gt;, &lt;code&gt;ng run&lt;/code&gt;. These commands use an internal tool called Architect to run CLI builders, which apply another tool to accomplish the wanted task. From Angular version 8, Angular developers can use CLI Builder API to customize the Angular CLI by adding or modifying commands. For example, you could supply a builder to perform an entirely new task, or to change which third-party tool is used by an existing command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create our custom CLI builders
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Update angular.json file
&lt;/h3&gt;

&lt;p&gt;We have to update the &lt;code&gt;angular.json&lt;/code&gt; file to add a target for this builder to the "architect" section of our new project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  // …
  "projects": {
    // …
    "builder-test": {
      // …
      "architect": {
        // …
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            // … more options…
            "outputPath": "dist/builder-test",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json"
          },
          "configurations": {
            "production": {
              // … more options…
              "optimization": true,
              "aot": true,
              "buildOptimizer": true
            }
          }
        },
        // other target. E.g: lint, server...
        "build-generate-404-page": {
          "builder": "./builders:build-generate-404-page"
        }
      }
    }
  }
  // …
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run our builder, use following CLI command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ng run [project name]:build-generate-404-page
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, if you run this command right now, you will get the error because we have not implemented any logic for our custom builder. Back to &lt;code&gt;angular.json&lt;/code&gt;, you can see in &lt;code&gt;build-generate-404-page&lt;/code&gt; object, we added &lt;code&gt;builder&lt;/code&gt; property. This property will instruct Angular which method to invoke when this target is called. Value of &lt;code&gt;builder&lt;/code&gt; is a string with 2 sections that separated by the colon:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first section is the path to folder which contain our custom builder. It can be a relative path if you create custom builder project in same workspace our project (like we will do in the example). Or it can be a name of &lt;code&gt;npm library&lt;/code&gt; if you publish our builder as a library, it will look like: &lt;code&gt;@builder-example&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The second path is the command that we want our builder executed. In the example, it is &lt;code&gt;build-generate-404-page&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create Builder Project
&lt;/h3&gt;

&lt;p&gt;Our builder project structure will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── builders
    ├─ src
    │   └─ build-generate-404-page
    |      ├─ index.ts
    |      └─ schema.json
    ├─ builders.json
    ├─ package.json
    └─ tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;package.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "@andyt/custom-builder",
  "version": "1.0.0",
  "description": "Angular CLI custom builder",
  "builders": "builders.json",
  "scripts": {
    "build": "rimraf dist &amp;amp;&amp;amp; tsc",
    "postbuild": "copyfiles --up 1 ./src/**/*.json ./dist",
    "build:watch": "tsc-watch --onSuccess 'npm run postbuild'"
  },
  "author": "Andy Tu Hoang",
  "license": "ISC",
  "devDependencies": {
    "@angular-devkit/architect": "^0.1402.8",
    "@angular-devkit/core": "^14.2.8",
    "@types/node": "^18.11.12",
    "copyfiles": "^2.4.1",
    "rimraf": "^3.0.2",
    "tsc-watch": "^6.0.0",
    "typescript": "~4.6.2"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like other Node.js projects, &lt;code&gt;package.json&lt;/code&gt; describes dependencies of this project. Beside, in Angular CLI Builder project, we have to define &lt;code&gt;builders&lt;/code&gt; property in &lt;code&gt;package.json&lt;/code&gt;. When Angular run CLI Builder, it goes to &lt;code&gt;package.json&lt;/code&gt; firstly and find &lt;code&gt;builders&lt;/code&gt; property, this property points to &lt;em&gt;builder definition&lt;/em&gt; file, in the example, it is &lt;code&gt;builder.json&lt;/code&gt; (but you can name it anything). Then Angular goes to &lt;code&gt;builder.json&lt;/code&gt; and base on it to find the script that need to be executed.&lt;br&gt;&lt;br&gt;
In our &lt;code&gt;package.json&lt;/code&gt;, we also describes some scripts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;build&lt;/strong&gt; - build typescript script to javascript script&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;postbuild&lt;/strong&gt; - this script execute immediately after &lt;code&gt;build&lt;/code&gt;. We use it to copy &lt;code&gt;schema.json&lt;/code&gt; file to &lt;code&gt;dist&lt;/code&gt; folder&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;build:watch&lt;/strong&gt; - use &lt;strong&gt;watch mode&lt;/strong&gt; to debug builder script&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;builders.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "builders": {
    "build-generate-404-page": {
      "description": "It builds application and then auto generate 404.html",
      "implementation": "./dist/build-generate-404-page",
      "schema": "./dist/build-generate-404-page/schema.json"
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file describes all available builders that exist in our builder projects. Each key under the &lt;code&gt;builders&lt;/code&gt; property is the name of the builder, in this case, it is &lt;code&gt;build-generate-404-page&lt;/code&gt;. Every builder will have some following property:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;description&lt;/strong&gt; – This field contains a message that describes what this builder does and it appears when you run e.g ng build --help command.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;implementation&lt;/strong&gt; – This field points to the NodeJS script that will be executed when Angular CLI call to corresponding builder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;schema&lt;/strong&gt; – This field points to the JSON file that describes a list of &lt;em&gt;options&lt;/em&gt; that can be provided for the builder function in the builder script. You can also describe type as well as default value and many other thing of the option. &lt;strong&gt;Note&lt;/strong&gt;: If your builder script doesn't have any options like the example, you still have to define this property and create corresponding &lt;code&gt;schema.json&lt;/code&gt; file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;tsconfig.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": "./",
    "target": "ESNext",
    "outDir": "./dist",
    "rootDir": "./src",
    "module": "CommonJS"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;schema.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "$schema": "http://json-schema.org/schema",
  "type": "object",
  "properties": {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our builder, we don't use any options, so &lt;code&gt;properties&lt;/code&gt; is empty. But let explore how &lt;code&gt;@angular-devkit&lt;/code&gt; describes &lt;code&gt;schema.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyn2sa5i15m895hnwomxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyn2sa5i15m895hnwomxk.png" alt="schema.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see &lt;code&gt;assets&lt;/code&gt; property is described and type of it is an array. And this is exactly the property that you define in your angular.json file in the options property of the corresponding build architect target.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy24d17s3dkbjfaw84do9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy24d17s3dkbjfaw84do9.png" alt="angular.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;build-generate-404-page/index.ts&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Now, we create a builder that copies &lt;code&gt;index.html&lt;/code&gt; to &lt;code&gt;404.html&lt;/code&gt; after build project. To create a builder, use the createBuilder() CLI Builder function, and return a Promise object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createBuilder } from '@angular-devkit/architect';
import { readFileSync, writeFileSync } from 'fs';

export default createBuilder(async (options, ctx) =&amp;gt; {
  //put logic hear
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, we have to invoke target &lt;code&gt;build&lt;/code&gt; in &lt;code&gt;angular.json&lt;/code&gt; file to build our Angular application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default createBuilder(async (options, ctx) =&amp;gt; {
  try {
    const build = await ctx.scheduleTarget({
      target: 'build',
      project: ctx.target!.project,
      configuration: ctx.target!.configuration!,
    });

  } catch (error) {
    return {
      success: false,
    };
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we get the &lt;code&gt;outputPath&lt;/code&gt; and use Node.js API &lt;code&gt;readFileSync&lt;/code&gt; and &lt;code&gt;writeFileSync&lt;/code&gt; to copy content from &lt;code&gt;index.html&lt;/code&gt; to &lt;code&gt;404.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default createBuilder(async (options, ctx) =&amp;gt; {
  ctx.logger.info('Builder has been started...');
  try {
    const build = await ctx.scheduleTarget({
      target: 'build',
      project: ctx.target!.project,
      configuration: ctx.target!.configuration!,
    });
    const result = await build.result;
    const success = result.success;
    const outputPath = result.outputPath as string;
    if (success) {
      const pathOfIndexPage = `${outputPath}/index.html`;
      const contentOfIndexPage = readFileSync(pathOfIndexPage, 'utf-8');
      const pathOfNotFoundPage = `${outputPath}/404.html`;
      writeFileSync(pathOfNotFoundPage, contentOfIndexPage);
      ctx.logger.info('Builder has been completed!!!');
      return { success };
    } else {
      return {
        success: false,
      };
    }
  } catch (error) {
    return {
      success: false,
    };
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our builder logic is done. we run &lt;code&gt;npm run build&lt;/code&gt; to compile it to Node.js script. And then go to our Angular application, run &lt;code&gt;ng run [project name]:build-generate-404-page&lt;/code&gt; and push all to Github Repository. Your application will be run ok on Github Page without &lt;strong&gt;404 redirect issue&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;We already explored how to create a custom &lt;strong&gt;Angular CLI Builder&lt;/strong&gt; to fix &lt;strong&gt;404 redirect issue&lt;/strong&gt; on Github Page. &lt;strong&gt;CLI Builder API&lt;/strong&gt; is an intensive tool which you can do a lot of thing with this. If you want to learn more about &lt;strong&gt;CLI Builder&lt;/strong&gt;, you can visit &lt;a href="https://angular.io/guide/cli-builder" rel="noopener noreferrer"&gt;Angular.io&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
Full code of the example is &lt;a href="https://github.com/AndyT2503/AndyT2503.github.io/tree/v2" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>tutorial</category>
      <category>typescript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
