angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

How to set 'publicPath' in webpack after 'deployUrl' option is deprecated in version 15

Open feng-xiao opened this issue 5 years ago • 54 comments

https://github.com/angular/angular-cli/blob/5402f99f8ad20e0a57456a416a992415fc6332bd/packages/angular_devkit/build_angular/src/webpack/configs/common.ts#L335

feng-xiao avatar Nov 07 '21 22:11 feng-xiao

As a work around for the WebPack publicPath value you can set the global __webpack_public_path__ variable in your angular entry point. I have the following in my main.ts file.

declare var  __webpack_public_path__: string;
__webpack_public_path__ = 'valueFormerlyAssignedUsing_deployUrl';

I'm on Version 13

same problem here: 'test' was not added to the build html paths

declare var __webpack_public_path__: string;
__webpack_public_path__ = 'test';

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import 'hammerjs';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));
    "@angular-devkit/build-angular": "~13.2.5",
`    "@angular/cli": "~13.2.5",
    "@angular/compiler-cli": "~13.2.4",

robvaneck avatar Feb 26 '22 16:02 robvaneck

It seems that so many use cases are in favor of keeping the deployUrl option. I don't know if it would be possible for the Angular team , to enable/disable to use of deployUrl in configuration so that developpers who want it can opt-in for build performance gains and developpers who need the current behavior can keep using it ?

rekna1 avatar Feb 26 '22 17:02 rekna1

@alan-agius4 Any hope? 🙏 😅

Blackbaud-SteveBrush avatar Mar 14 '22 15:03 Blackbaud-SteveBrush

I also need deploy URL, otherwise the chunks are not loaded correctly.
APP_BASE_HREF is not the solution.

lulzzz avatar Mar 26 '22 15:03 lulzzz

This causes problems with our setup too. We use Spring Boot to serve the Angular app. The app is being built under resources/static/dist. Spring by default serves content from the resources/static folder, we use a subfolder so that we can restrict access to resources but whitelist the Angular app. We set the deployUrl to dist/ to make this setup work. The index.html is served as a Thymeleaf template with the base href set to the context path of the server, we do not use the index.html generated during build.

E.g. suppose the server runs on http://my-domain.com/foo then we set the base href to /foo/, the app resources are available at http://my-domain.com/foo/dist.

With the deprecation of deployUrl we need to set the base href to /foo/dist/ for the assets to be loaded but this results in API requests going to /foo/dist/api whereas we need them to go to /foo/api.

Obviously there are ways around it (the best one currently being using deployUrl while we still can), but I would like to know what an elegant solution to this problem could be.

icguy avatar Mar 31 '22 07:03 icguy

We also have problems with the deprecation of deployUrl.

Our server serves two Angular applications from the same path depending on whether the user is authenticated or not, with the help of deployUrl, we could set each application's assets paths to separate them on the server. In this case if user load http://example.foo/ and they are not authenticated, the server can serve lets say LoginApplication which has deployUrl set to assets/login/, once they are authenticated the server serves lets say DashboardApplication which has deployUrl set to assets/dashboard/.

APP_BASE_HREF does not help us because it changes the browser URL to assets/login/ or assets/dashboard/.

Any idea how this can be handled without deployUrl?

Luladjiev avatar Mar 31 '22 08:03 Luladjiev

How to use deployUrl correctly and what problems it is good / not good for has been a difficult area for the community. We've done our best to assist folks where possible, but this is a big enough issue that our attempts to do so haven't really scaled effectively. After some discussion, we think the best path forward here is to update some of our docs to be more clear about various aspects and also write a blog post which discusses some common use cases where devs typically reach for deployUrl and how our official recommendation for how to handle those requirements. That seems like the most scalable approach addressing the confusion around deployUrl.

I'm closing this issue for now as we don't see anything directly actionable here, but hopefully better documentation and a future blog post will help clarify how best to meet your requirements.

dgp1130 avatar Apr 14 '22 22:04 dgp1130

@dgp1130 Could you clearly describe what will happen to deployUrl? Will that property still pending on removal or Angular will keep that functionality.

People who posted here, or at least for me need an clear path of whether deployUrl will be removed or not. Documentation and recommendation is good, but if the functionality will be removed, we need to plan ahead.

Nness avatar Apr 14 '22 22:04 Nness

We are still planning to remove deployUrl eventually largely because of all the confusion it causes. The exact version it gets dropped is still TBH, but I can confidently say it will not be removed in v14, so this shouldn't be an immediate problem for anyone. The blog post in particular should hopefully help address common use cases which have used deployUrl in the past and how we recommend moving forward for each of them. We're not expecting any existing use cases to suddenly become unsupported, just that the right approach for you will probably vary by your particular requirements.

dgp1130 avatar Apr 14 '22 23:04 dgp1130

Had this same issue but found a solution that worked for me - https://stackoverflow.com/questions/71695674/what-is-best-way-to-go-about-replacing-deployurl-in-angular-json-for-v13/71913980#71913980

ThomasDevens avatar Apr 18 '22 15:04 ThomasDevens

I also need deploy URL, otherwise the chunks are not loaded correctly. APP_BASE_HREF is not the solution.

did you ever managed to work things out with omit deployUrl and using APP_BASE_HREF?

AGuyCoding avatar May 13 '22 09:05 AGuyCoding

@benjaminadrom tey a solution posted by @ThomasDevens above. It worked for me.

miluoshi avatar May 15 '22 09:05 miluoshi

@benjaminadrom tey a solution posted by @ThomasDevens above. It worked for me. thanks, but APP_BASE_HREF' useValue did not change the base href inside the generated index, so his solution as I understood was to replace those links that manually, which feels like an unnecessary hack somehow

AGuyCoding avatar May 16 '22 05:05 AGuyCoding

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.