Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling app with --base-href does not prefix image src attribute value with --base-href value #9347

Closed
reloaded opened this issue Jan 24, 2018 · 4 comments

Comments

@reloaded
Copy link

Versions

<!--
Output from: `ng --version`.
If nothing, output from: `node --version` and `npm --version`.
  Windows (7/8/10). Linux (incl. distribution). macOS (El Capitan? Sierra?)
-->

ng --version
@angular/cli: 1.4.3
node: 8.9.1
os: win32 x64

npm --version
5.5.1

Repro steps

  • Create a new Angular CLI project.
  • Configure the base URL to be <base href="/" /> in the index.html
  • Create a new component transition-indicator at app/widgets/ (the resulting directory tree should be app/widgets/transition-indicator/transition-indicator.component.{ts,html,scss,spec.ts}.
  • Update transition-indicator.component.html and use an <img /> where the src is relative and stored in assets/. Mine is <img src="../../../assets/images/loading/fading-lines-loader-32x32.gif" alt="Loading..." />.
  • Create an image (i'm using a loading GIF from https://icons8.com/preloaders/) at app/assets/images/loading/fading-lines-loader-32x32.gif
  • Update app.module so it declares TransitionIndicatorComponent.
  • Create another component called home (the resulting directory tree should be app/home/home.component.{ts,html,scss,spec.ts}).
  • Reference <app-transition-indicator></app-transition-indicator> from HomeComponent's view template.
  • Create a route in app.route that loads HomeComponent.
  • Build the application with ng build --environment=dev --base-href /angular-app-2/

Let's assume your application is at C:\angular-app-2.

Configure your local IIS / Nginx / Apache to host a new virtual website at http://localhost/angular-app-2.

  • Point this new virtual host document root to C:\angular-app-2\dist
  • Restart IIS / Nginx / Apache.
  • Load http://localhost/angular-app-2 in the browser. Notice all assets loaded correctly in the browser developer tools (network tab), except the image that was referenced by the transition-indicator component.

image

Here's a ZIP with the project that can reproduce this. https://www.dropbox.com/s/euoyxria5lzfbrk/angular-app-2%28angular%20cli%20bug%29.zip?dl=0

Observed behavior

<!-- Normally this includes a stack trace and some more information. -->

The Angular CLI compiler is not updating the transition-indicator component's <img/> tag src attribute to request the loading GIF from /angular-app-2/assets/images/loading/fading-lines-loader-32x32.gif and instead the src value is /assets/images/loading/fading-lines-loader-32x32.gif.

This results in a HTTP request to http://localhost/assets/images/loading/fading-lines-loader-32x32.gif instead of http://localhost/angular-app-2/assets/images/loading/fading-lines-loader-32x32.gif.

Desired behavior

I would expect the loading image to be loaded from /angular-app-2/assets/* because the Angular CLI build command sets the <base /> element href to /angular-app-2/.

Mention any other details that might be useful (optional)

The purpose of this build style is to host multiple apps under the same sub-domain (one sub domain for each environment like sprint staging, release staging, production staging, etc), but each app using their own unique sub-path. In this case my sub-domain is for all applications that are in the current Agile Sprint iteration (sprint-stage.mydomain.com).

I have two Angular applications:

  • sprint-stage.mydomain.com/ <-- this is the first Angular app and it's hosted at the root path.
  • sprint-stage.mydomain.com/angular-app-2/ <-- this is the second Angular app. My CI/CD build server executes ng build --environment=dev --base-href /angular-app-2/ so the app will run on the sub-path /angular-app-2/.
  • sprint-stage.mydomain.com/api/v1/microservice-1
  • sprint-stage.mydomain.com/api/v1/microservice-2
  • sprint-stage.mydomain.com/api/v1/microservice-3

I'm using a custom environment file for my stage-sprint environment so normally the command is ng build --environment=stage-sprint --base-href /angular-app-2/; but for simplicity of this issue I replaced it with --environment=dev.

@reloaded reloaded changed the title Compiling the app with --base-href does not update relative image src attribute values Compiling app with --base-href does not prefix image src attribute value with --base-href value Jan 24, 2018
@filipesilva
Copy link
Contributor

Heya, thanks for the detailed issue and reproduction! This is a known limitation where assets in HTML are not processed. You can read more about it #6666. Workaround for now is to load that image in a CSS file.

@clydin
Copy link
Member

clydin commented Jan 25, 2018

Relative asset URL's present within a component's HTML template will be affected by the base HREF (this is in direct contrast to the deployUrl option). However, the asset's URL must be relative in the context of the deployed location not the project's source location.

@joelswanson123
Copy link

joelswanson123 commented Apr 24, 2019

This is wrong, I think.

<img src="../../../assets/images/loading/fading-lines-loader-32x32.gif" alt="Loading..." />

Folder assets will be in the root of your deployed location. Therefore the path should be:

< img src="./assets/images/loading/fading-lines-loader-32x32.gif" alt="Loading..." />

At least that worked for me.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants