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

--deploy-url will have no effect on the HTML

Open yordis opened this issue 9 years ago • 42 comments

Bug Report or Feature Request (mark with an x)

- [ ] bug report -> please search issues before submitting
- [ x ] feature request

Versions.

@angular/cli: 1.0.1 node: 8.0.0 os: darwin x64 @angular/animations: 4.1.3 @angular/common: 4.1.3 @angular/compiler: 4.1.3 @angular/core: 4.1.3 @angular/forms: 4.1.3 @angular/http: 4.1.3 @angular/material: 2.0.0-beta.6 @angular/platform-browser: 4.1.3 @angular/platform-browser-dynamic: 4.1.3 @angular/router: 4.1.3 @angular/cli: 1.0.1 @angular/compiler-cli: 4.1.3

Repro steps.

Related to https://github.com/angular/angular-cli/issues/4517#issuecomment-286252628

The log given by the failure.

Desired functionality.

The CLI transform the HTML as well

Mention any other details that might be useful.

I am using S3 on AWS this is kind of required for me unless I used CloudFront, I think.

yordis avatar Jun 13 '17 16:06 yordis

@clydin I want to serve images from a CDN and the rest of the app should point to /. Will your solution help me achieve that?

I tried setting APP_BASE_HREF to cdn.example.com and now my app automatically redirects to localhost:4200/cdn.example.com. Is this the expected behaviour?

s3rot0n1n avatar Sep 11 '19 16:09 s3rot0n1n

No real work arounds for hosting index.html on domain A and all the assets/JS on Domain B?

Even doing search/replace won't work as some of the asset paths are inside component source files.

Really, if Angular CLI could just expose the value of deploy-url and replace it at build time would work.

Our Angular app is 40+ meg and I don't really want to go through all the URLs in our source code and manually update them...

fishgills avatar Sep 13 '19 18:09 fishgills

This need to be fixed urgently. Don't you developer guys use CDN? Angular is really a outdated framework. @filipesilva

raisin8010 avatar Dec 08 '19 12:12 raisin8010

In my case I had to change my src from <mdb-card-img src="../../assets/404.png"></mdb-card-img> (which was correct from src/app/component_dir) to <mdb-card-img src="assets/404.png"></mdb-card-img> and ng serve properly used the path of http://localhost:4200/{deployUrl}/assets/404.png

xinthose avatar Dec 09 '19 17:12 xinthose

As a workaround, we change

<!-- component.html -->
<img src="assets/arrow-left.png" />

to something like this

// component.ts
const arrowLeft = require('assets/arrow-left.png');

export class MyComponent implements OnInit {
  readonly arrowLeft = arrowLeft;
}
<!-- component.html -->
<img [src]="arrowLeft" />

By requiring the image in ts, the file can be correctly loaded by file-loader, and so both file hash and deploy-url work. This pattern is also very common in React world.

xiaody avatar Dec 13 '19 08:12 xiaody

A potential workaround that worked for us, can be found here, too: https://stackoverflow.com/a/46493276/1902598 Basically set APP_BASE_HREF with the help of the PlatformLocation.getBaseHrefFromDOM

timbru31 avatar Dec 13 '19 09:12 timbru31

I recently also ran into the same situation because of https://github.com/angular/angular-cli/issues/15295#issuecomment-520098529. After trying a couple different ways, a solution I found that suits me well is using different assets in angular.json for production: https://angular.io/guide/workspace-config#asset-config (the "asset specification object" part)

neo avatar Dec 17 '19 21:12 neo

@clydin is it possible to configure the APP_BASE_HREF from the angular.json file? I would like to configure the two different baseHref and appBaseHref in the same place and from configuration.

JonathanWylie avatar Feb 19 '20 07:02 JonathanWylie

Is there any way to decouple APP_BASE_HREF provider from --base-href somehow?

That way for the browser, it could respect the HTML tag for all relative static assets, lazy-loaded chunks, etc

<base href="http://www.somecdn.com/"/>

But for Angular router, configuring the provider to have it respect a different "root" so routing doesn't break, based on what is in the user's URL bar ex:

providers: [
  {
      provide: APP_BASE_HREF, useValue: `/` // or even `${window.location.origin}/`
  }
]

In this way, we can use a CDN for all our lazy chunks assets, but not breaking routing, which would be URL / hostname based.


So just curious to know from the maintainer's point of view, if there is a way forward through some sort of enhancement to Angular and / or the CLI (whether it's this idea or something completely different)?

If so, I would be happy to take a look into implementing such a feature, opening an issue for discussion, and contributing. Just want to make sure there is a path forward and would then be happy to put some time into working on it.

Thanks!

thescientist13 avatar Mar 06 '20 21:03 thescientist13

Here's my workaround:

declare const __webpack_public_path__: string;
export function deployUrl(url: string): string {
  let publicPath = __webpack_public_path__;
  if (!publicPath.endsWith('/')) {
    publicPath += '/';
  }
  if (url.startsWith('/')) {
    url = url.slice(1);
  }

  return `${publicPath}${url}`;
}

In component:

@Component({...})
export class MyComponent {
  deployUrl = deployurl;
}

In template:

<img [src]="deployUrl('assets/image.png')"/>

vultix avatar Apr 20 '20 18:04 vultix

As there is an awful lot of confusion around --deploy-url I think this is a good candidate to review and update documentation. Specifically this could include:

  1. What use cases --deploy-url is intended for (and what it is not).
  2. For several common use cases (such as CDN's), what configuration recipe (whether using --deploy-url or other means) makes the most sense and does what an application wants.

/cc @aikidave for visibility.

dgp1130 avatar Jun 11 '20 18:06 dgp1130

Copied from my STACKOVERFLOW answer. Its a very annoying problem: Here is how I solve it:

When your app is for e.g. hosted under www.domain.de/hehe.

  1. in angular.json add "deployUrl": "./",
  2. in src/index.html inside <head> add/update <base href="./">
  3. throughout your app you will use relative paths for assets and whatnot: <img src="./assets/img/pictureOfMyCrazyWife.png">
  4. just run ng build or ng build --prod conveniently.

Hope this helps and saves some grey hair, couse it only looks good on George Clooney.

andreElrico avatar Jun 17 '20 14:06 andreElrico

@andreElrico that does not seem to work if you are using routing If you try to navigate from the base path, let's say /base-path/ to /base-path/router-path then try to reload the page or copy the link to another tab, the application will fail to load since it tries to use /base-path/router-path as base path

CaselIT avatar Jul 15 '20 07:07 CaselIT

@CaselIT have not tried it with routing.

andreElrico avatar Jul 15 '20 22:07 andreElrico

The problem of not being to add cache-busting hash to images used in Angular templates is a production issue many are dealing with.

I understand that this is out of scope for the CLI team at the moment, but I have seen some workarounds. I will try to summarize what i've seen so far.

If you want image cache-busting / fingerprinting you have these options:

  1. Only load images from css using relative path - This works fine, but some cases you want an <img> tag in template.
  2. This comment in this thread. Basically require the image in your component through Webpack and inject into the template. There are other, perhaps more elegant solutions like this one but same concept. However, these were sort-of recommended against by @filipesilva in this comment.
  3. Do some string search-and-replace hack post build. I don't think anybody considers this option...
  4. I've seen a comment by @mgechev about using ngx-build-plus for this purpose. Not sure how.

I think it will be very helpful for the community if we had a "blessed" workaround for this from the cli team and get their view on this. Ideally added to the official cookbook/recipe/docs

Thank you

mderazon avatar Aug 27 '20 10:08 mderazon

@filipesilva I want to second @mderazon idea about having a "blessed" solution for this. It seems like the community is wasting a lot of time on something that is pretty common. I'd be willing to help with a solution, but I think we are looking to you guys for a little direction as to what you think should be done or info if you are already working on something.

oliverdavidt avatar Sep 16 '20 14:09 oliverdavidt

I don't know if it is the same issue, because I use command --base-href of Angular. But if it helps someone, I'm happy.

I used the \ before the assets like this: <img [src]="'\assets/images/image.png'"/>

And this worked perfectly for me.

HDMuller avatar Oct 16 '20 13:10 HDMuller

This is really annoying. The --base-href parameter is available by injecting APP_BASE_HREF. I don't understand why --deploy-url can't be accessed the same way.

jorgeso avatar Apr 28 '21 18:04 jorgeso

Also guys, someone tried this with angular-universal? If I use: --deploy-url /myPath/ I'm receiving these erros in node...

Unable to locate stylesheet: /var/www/html/dist/browser/**myPath**/styles.16d89a3cd69123f2ba9c.css

Also the url set to the assets seems to be wrong..

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

BruneXX avatar Jun 15 '21 18:06 BruneXX

According to https://github.com/angular/angular-cli/issues/6666#issuecomment-595974291 in this thread. I tried building the app using:

ng build --c=qa --base-href https://your-assets-url/

and then in the app module to change the router base href:

providers: [ { provide: APP_BASE_HREF, useValue: '${window.location.origin}/' (replace single quotes ) } ]

This works fine for my scenario where I am serving the index.html in a different url and the assets from a cdn. Correct me if I am doing anything wrong.

Rishanthakumar avatar Jun 29 '21 15:06 Rishanthakumar