Angular 6 - Specify path for lazy loaded chunks
Asked Answered
L

2

7

I have a main module with submodules, originally I wanted to make that modules eager loaded, so what I did:

1st Attempt - Eager loading

import {DashboardModule} from './module/dashboard/dashboard.module';
export const _dashboardModule = () => DashboardModule;
export const routes: Routes = [
    {
        path: '',
        children: [
            {
                path: 'dashboard',
                loadChildren: _dashboardModule
            }
        ]
    }                
];

It worked just fine for me until I start building prod build with aot compilation. In that moment I get into issue which is mentioned here: https://github.com/angular/angular-cli/issues/4192

2nd Attempt - Lazy loading

Based on ticket I linked above I start using string paths for modules e.g.

loadChildren: './path/to/module/dashboard.module#DashboardModule'

which generate separate bundle for each module. For build I used angular-cli command

ng build --output-path=../my/ouput/path --prod --aot --output-hashing none --vendor-chunk

Because my application is part of huge existing application I can't use generated index.html but I use .ftl file where I specify from where all scripts should be loaded like this:

<body>
    <my-app></my-app>

    <script src="<@versionedUri src="${base}/path/to/file/runtime.js"/>"></script>
    <script src="<@versionedUri src="${base}/path/to/file/polyfills.js"/>"></script>
    <script src="<@versionedUri src="${base}/path/to/file/styles.js"/>"></script>
    <script src="<@versionedUri src="${base}/path/to/file/vendor.js"/>"></script>
    <script src="<@versionedUri src="${base}/path/to/file/main.js"/>"></script>
</body>

And that's an issue if you try to lazy load because output files of my build is in different folder then my .ftl file which I use instead of index.html problem is that when you open application in browser it try to load chunks from same folder where .ftl is located and they doesn't exist there.

What my questions are:

  • Is there a way to specify path for chunks files?
  • Or is there some other way to eager load module without importing child routes and use them like: children: dashboardRoutes
Labourer answered 5/6, 2018 at 14:36 Comment(1)
When you use lazy load is important not import the loaded module, its better load children throught path stringDissimulate
Z
5

The path where lazy-loaded chunks (and all other .js scripts) are generated is designated in the angular.json file, @ (projects: <projectName>: architect: options:) outputPath: <your-output-path> ...

I'm having trouble myself, getting the Angular app to look for the chunk at this location, it seems to be looking for it at the root (baseUrl).

I'm in a similar situation as OP, embedded the Angular app in a .NET MVC5 application, so rather than using the Angular-generated index.html, the file "serving" the app is the MVC Home/Index view. My Index.cshtml file looks very similar to OP's .ftl file... my quandary is how to tell the Angular app to look for the chunk at the outputPath location?

Zygospore answered 31/8, 2018 at 17:30 Comment(2)
I'm also facing the same issue. Did you able to find out the solution?Usanis
Adding deployUrl in angular.json file resolved the issue.Usanis
T
5

I have a similar situation, and what helped was adding deploy url to my angular.json. The comment by Hermant Jain helped. I am just adding this answer to specify where to add this setting in angular.json.

Follow this path in angular.json:

Projects -> Your Project -> Architect -> Build -> Options-> deployUrl

The other way around without specifying this is to pass this as a parameter as follows:

    ng build --watch --deploy-url /dist/

This way, my chunks are loading from myWeb/dist/

Telltale answered 24/4, 2019 at 8:45 Comment(1)
Using deployUrl fix it in case the deployment URL has no virtual directory/folder aka somesite.xyz/theApp if the folder theApp isn't used it will work. I need to cover both cases and I can't find a way to get to work. If I use base href It will fix the folder case but break others!Vaclava

© 2022 - 2024 — McMap. All rights reserved.