Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - Specify path for lazy loaded chunks

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
like image 425
Andurit Avatar asked Jun 05 '18 14:06

Andurit


People also ask

What is the new way of declaring lazy loaded routes?

Create a feature module with routinglink Because the new module is meant to be lazy-loaded, the command does not add a reference to it in the application's root module file, app.module.ts . Instead, it adds the declared route, customers to the routes array declared in the module provided as the --module option.

How do you use a lazy load module without routing?

define where we want to load our component in the template with the ng-template tag, define its view query through ViewChild decorator, which gives us access to the DOM and defines the container to which the component will be added, finally, dynamic import the component and add it to the container.

What are lazy chunk files in Angular?

The compiled source of the lazy-loaded module is written to a separate file, called a chunk. This essentially splits the codebase (or the JavaScript bundles) into separate chunks by their respective loadChildren router configuration. A lazy loaded module is included in a chunk.

What is Lazyload in Angular?

Lazy loading is an approach to limit the modules that are loaded to the ones that the user currently needs. This can improve your application's performance and reduce the initial bundle size. By default, Angular uses eager loading to load modules.


2 Answers

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?

like image 168
jwdvorak Avatar answered Oct 12 '22 08:10

jwdvorak


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/

like image 39
Zonaib Avatar answered Oct 12 '22 07:10

Zonaib