Search code examples
angularangular-cliangular-cli-v7

How to point Angular static assets to WWW URL


When I run ng build I get this index.html file:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ApiApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>

<script type="text/javascript">
  const appData = JSON.parse('<%=json%>');
</script>

<app-root></app-root>
<script type="text/javascript" src="runtime.js"></script>
<script type="text/javascript" src="polyfills.js"></script>
<script type="text/javascript" src="styles.js"></script>
<script type="text/javascript" src="vendor.js"></script>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

Using a script after a build I will push the static assets to some CDN, so I actually want to generate an .html file that looks like this:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>ApiApp</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>

<script type="text/javascript">
  const appData = JSON.parse('<%=json%>');
</script>

<app-root></app-root>
<script type="text/javascript" src="http://cdn.github.com/runtime.js"></script>
<script type="text/javascript" src="http://cdn.github.com/polyfills.js"></script>
<script type="text/javascript" src="http://cdn.github.com/styles.js"></script>
<script type="text/javascript" src="http://cdn.github.com/vendor.js"></script>
<script type="text/javascript" src="http://cdn.github.com/main.js"></script>
</body>
</html>

(I made up the domain cdn.github.com). So I could manually generate the urls for these files, but I am wondering if Angular allows us to configure it somehow?


Solution

  • You can configure it in angular.json file in your angular project.

    {
      "projects": {
        "<Your Angular Project Name>": {
            "architect": {
                "build": {
                    "options": {
                        "baseHref": "/testapp/",
                        "deployUrl": "/testapp/",
                        ...
                    }
                }
            }
        }
      }
    }
    

    deployUrl is basically what you are looking for.

    For available build options and its description, you can refer to the below link: https://github.com/angular/angular-cli/wiki/build