How to make Angular cli serve package files from absolute location and not base-href

I am struggling with deploying an Angular 4 project built with the cli for a Spring Boot Server.

In .angular-cli.json, I added an outdir property that points to a custom folder inside the Spring webapp folder that is accessible directly on the server (webapp / main).

The cli build command I'm using is:

ng build --base-href /main/ -a main

      

Creates a main folder in webapp with index.html containing the following script tags:

<base href="/main/">
...
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>
<script type="text/javascript" src="vendor.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script></body>

      

which means that these files will be requested with a / main prefix like localhost: 8080 / main / inline.bundle.js

Which is fine for a basic application, but becomes a problem when adding routes to an Angular application (in Spring, I serve index.html for all paths from / main to allow Angular to work correctly, which triggers a redirect loop for the packages as they start with / main as well as .

How do I force the generated script tags to use a custom location? or at least specify an absolute location and not a relay on the base href (so I can change the outDir property to main-app and the problem is solved).

The desired solution would be something like this:

<script type="text/javascript" src="/main-app/main.bundle.js"></script>

      

I thought maybe add a script to be executed after ng build that will change the script tags, but this is a messy solution and also won't work with ng build --watch , which is critical for development ...

For reference, here is a Spring controller function that renders / main requests:

@RequestMapping("main/**")
public String mainRoute(HttpServletRequest request) {
       return "/main/index.html";
}

      

Thank!

+2


source to share


1 answer


Here is the ng build

wiki: https://github.com/angular/angular-cli/wiki/build

use --deploy-url="/main-app/"

variant. this will generate:



<script type="text/javascript" src="/main-app/inline.bundle.js"></script>
<script type="text/javascript" src="/main-app/polyfills.bundle.js"></script>
<script type="text/javascript" src="/main-app/scripts.bundle.js"></script>
<script type="text/javascript" src="/main-app/styles.bundle.js"></script>
<script type="text/javascript" src="/main-app/vendor.bundle.js"></script>
<script type="text/javascript" src="/main-app/main.bundle.js"></script>

      

+5


source







All Articles