Angular 5 - Dynamic base reference is causing duplicate loading of bundles|chunks

末鹿安然 提交于 2019-12-03 12:31:40
Karan

The problem lies during the ng build arguments.

Earlier it was

ng build --prod -e=dev --base-href=/Client1

After i added the ending / to the ng build statement, it worked fine.

ng build --prod -e=dev --base-href=/Client1/

The duplicate angular chunks gone.

Instead of using the HTML attribute, use the APP_BASE_HREF provider. You can use a dynamic factory to have a value that changes over time.

You can use ng build --base-href /myUrl/.

Or add "baseHref" : "MY_APP_BASE_HREF_2" in your angular.json.

Related post with more info: Angular 2 / 4 / 5 - Set base href dynamically

Add this to head section in index.html

<base href="/">
 <script>
 (function() {
  window['_app_base'] = '/' + window.location.pathname.split('/')[1];
 })();
</script>

Then in the app.module.ts file, add { provide: APP_BASE_HREF, useValue: window['_app_base'] || '/' } to the list of providers, like so:

import { NgModule, enableProdMode, provide } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { APP_BASE_HREF, Location } from '@angular/common';
import { AppComponent, routing, appRoutingProviders, environment } from './';
 if (environment.production) {
 enableProdMode();
}

  @NgModule({
  declarations: [AppComponent],
   imports: [
   BrowserModule,
   HttpModule,
   routing],
   bootstrap: [AppComponent],
   providers: [
   appRoutingProviders,
   { provide: APP_BASE_HREF, useValue: window['_app_base'] || '/' },
 ]
})
export class AppModule { }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!