How to set 'publicPath' in webpack after 'deployUrl' option is deprecated in version 15
https://github.com/angular/angular-cli/blob/5402f99f8ad20e0a57456a416a992415fc6332bd/packages/angular_devkit/build_angular/src/webpack/configs/common.ts#L335
That's also an issue for us. We have a widgets application that contains multiples lazy loaded custom elements. These elements are used in other applications. When some element want to load an image it should be loaded from the widgets domain, not from the current one that belongs to main application. Currently this can be achieved by using deploy-url during widgets app build. How to do it without this option?
@feng-xiao, can you please elaborate on why you need the deployUrl option and the specific use-case?
@artaommahe, is the image located in component stylesheet or external stylesheet?
@alan-agius4 in component's scss files
@artaommahe, in that case there is no alternate option for that. One thing to point out is that this aligns the behaviour of images defined in HTML and CSS, which results in images URI HTML to always be relative to HTML.
What comes to mind is to have an external CSS file (styles.css) that you defined the image in, since images are loaded relative to the stylesheet origin. Though this might be a bit cumbersome.
@alan-agius4 using global css file breaks lazy loading of such stuff, locality of code (images of some component from one of the widgets lib somehow are placed in global styles file).
This looks strange due to availability of official @angular/elements and breaks their usage for micro-frontend cases.
If the assets can be referenced from the component templates instead, another option is to generate the full asset URL at runtime via the use of import.meta.url. This is similar in functionality to the deprecated deploy URL option but has the advantage of not needing to hardcode anything other than the relative path of the asset. The JavaScript and asset files as a whole could then potentially be served from locations other than a predefined deploy URL and still function.
This method is unfortunately not possible with stylesheets since they are fixed at build time. However, if stylesheets are required in this context, they could potentially be external but dynamically loaded to retain lazy loading semantics.
@feng-xiao, can you please elaborate on why you need the deployUrl option and the specific use-case?
@artaommahe, is the image located in component stylesheet or external stylesheet?
Hi @alan-agius4 , I've integrated Angular app into an ASP.NET MVC application, i.e. the Angular app is initialized by a MVC razor view. Since the Angular resources are generated and hosted in different port number in 'ng serve' mode than MVC app, e.g. ASP.NET MVC app is loaded in localhost:8444, Angular app would be loaded in different port number like localhost:8445, I need to make Angular resources loaded using absolute URL, otherwise, they'll be loaded in wrong URL, the one MVC is using (i.e. localhost:8444).
@feng-xiao, can you please elaborate on why you need the deployUrl option and the specific use-case? @artaommahe, is the image located in component stylesheet or external stylesheet?
Hi @alan-agius4 , I've integrated Angular app into an ASP.NET MVC application, i.e. the Angular app is initialized by a MVC razor view. Since the Angular resources are generated and hosted in different port number in 'ng serve' mode than MVC app, e.g. ASP.NET MVC app is loaded in localhost:8444, Angular app would be loaded in different port number like localhost:8445, I need to make Angular resources loaded using absolute URL, otherwise, they'll be loaded in wrong URL, the one MVC is using (i.e. localhost:8444).
Hi @alan-agius4 , based on webpack's source code, I don't see any overhead by setting 'publicPath' to webpack config. I don't know why Angular Doc says it could increase build time.
@feng-xiao, the deployUrl, is used not just to set the publicPath, using deployUrl will cause additional steps including processing all CSS templates and files to append the deployUrl to the assets urls.
Another disadvantage of using deployUrl, is that the string value will be included all over the bundle multiple times, which causes an increase in bundle size and make promotion of bundles from one environment to another rather tricky.
In your cases, the recommended approach would be to use a combination of both --base-href and APP_BASE_HREF.
Razor View
...
<base href="http://localhost:8445/">
...
app.module.ts
...
{ provide: APP_BASE_HREF, useValue: 'http://localhost:8444' }
...
@alan-agius4 We heavily use too the deployUrl parameter. For us, it's because we have a reverse proxy that route all requests under (base-href)/static (if the load is too high) to a dedicated apache server, because we serve angular through a tomcat server most of the time (embedded jar for a Spring Boot application).
With the deployUrl we currently have
<!DOCTYPE html><html lang="fr">
<head>
<base href="/context/path/">
<meta charset="utf-8">
<title>My application...</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="static/favicon.png">
<link rel="stylesheet" href="static/styles.8277a10e7c2be6c58f14.css" media="print" onload="this.media='all'">
</head>
<body>
<app-root>loading...</app-root>
<script src="static/runtime-es2017.5a110097badef840dfda.js" type="module" crossorigin="use-credentials"></script>
<script src="static/runtime-es5.5a110097badef840dfda.js" nomodule defer></script>
<script src="static/polyfills-es5.1f50460b6153455d79c6.js" nomodule defer></script>
<script src="static/polyfills-es2017.acf5917270495d93417a.js" type="module" crossorigin="use-credentials"></script>
<script src="static/main-es2017.f3526c48e4006bc6f2e3.js" type="module" crossorigin="use-credentials"></script>
<script src="static/main-es5.f3526c48e4006bc6f2e3.js" nomodule defer></script>
</body>
</html>
If this parameter is removed, we're just screwed :/
Also, the context path, in tomcat, must match base-href for angular to work correctly. And last, but not least, requests served to /context/path/ are protected by an SSO platform (OpenAM), whereas, those under /context/path/static are not, because considered safe (not enforced policy) by convention. So the SSO agent is free from checking if requests behind static are legitimate or not, what drastically reduces load. Hope it's understandable.
TLDR : we'd love to keep the option to have both base-href and deployUrl (or whatever it's named) configurable.
A full example project is here : https://github.com/mpalourdio/SpringBootAngularHTML5
Our dev build relies on deployUrl to properly set resource URLs for ng serve, since we're not using a proxy (and don't want to), we simply link files from https://localhost:4200 from our .cshtml file. Removing the option from ng serve settings made me create an additional builder definition, where I still able to set this to https://localhost:4200 and everything works. Removing the option from builder settings will ruin our set up.
Same for me , in asp.net loaded from index.cshtml like mpalourdio... would break everything... ( application is deployed on premise so the url is not known before deployment ) Now it tries to load scripts and assets from the root web folder while it should load from a sub folder (so security can be set on the application to allow only downloading scripts and assets from subfolder).
I have mentioned my use-case in https://github.com/angular/angular-cli/issues/22208
Currently I am using the following option for building the application,
"scripts": {
...
"build": "node --max_old_space_size=5048 ./node_modules/@angular/cli/bin/ng build --base-href=/portal/ --deploy-url=/portal/public/ --configuration production"
...
}
In my case, static assets needs to be load from /portal/public/<asset_path> where as routing or http request needs to fire with base-href which is /portal/.
In our case, we are doing white-listing the security for the path having public in it as it has only static assets.
What would be your recommendation in this case?
I tried with steps mentioned in https://github.com/angular/angular-cli/issues/22208#issuecomment-975312544 but the issue didnt got resolved. Please see https://github.com/angular/angular-cli/issues/22208#issuecomment-975409891
deprecation of deployUrl is now been handled in my environment with https://github.com/angular/angular-cli/issues/22208#issuecomment-975570531.
Note: In my case, i am using relative path
Can someone explain how I can avoid having all the garbage ("main.js", "runtime.js") etc in the root folder with base href="/" when deployUrl is removed? I don't want to write a script that rewrites urls. I just want to run "ng build --watch" and get the correct result from an already running webserver when I change files.
I get this with --base-url="/" and --deploy-url="/app".
(I don't have performance issues, so… why create problems for no good reason?)
@alan-agius4 similar case as above - we're adopting module federation for some of our projects to split huge apps into smaller pieces to reduce development and deployment time cost. We have to use deploy-url to pin remote apps' css images to their domain, that is different to host's one.
Curious, what's the Angular Team's argument for removing deployUrl from ng serve targets? We are heavily relying on this feature with our custom dev-server builder and is preventing us from migrating to Angular 13.
@alan-agius4 Is there any hope of Angular reverting the change to deployUrl for serve targets, or should we invest in trying to find another solution?
@Blackbaud-SteveBrush, in your case you can set the deployUrl in the browser builder which will also effect ng serve.
However, this issue is related to removed deployUrl completely.
What is your use-case of using this option?
We have a custom dev-server builder that calls into the "vanilla" Angular CLI dev-server builder. Before we make the call to the dev-server builder, we create a deployUrl value that points to a remote URL that also includes metadata in a parameter that's different depending on the SPA running the builder. Since the value is dynamic, we can't bake it into the angular.json file. For example, our deployUrl looks like:
https://host.blackbaud.com/my-spa?config=<names-of-chunks,assets,localhost-url,etc.>
The URL above (i.e. deployUrl) is where we house the SPA's index.html file, but we read the assets/chunks from localhost (during development). Using the deployUrl allows us to use this functionality (during a serve) because that value is getting passed directly to webpack's publicPath.
If there's an alternative way to generate a dynamic publicPath during a serve, I'd be happy to hear it. 👍 Thanks!