Assets not found in scss/css when using base-href

See original GitHub issue

Please provide us with the following information:

OS?

Windows 10 x64

Versions.

@angular/cli: 1.0.0-beta.32.3 [1.0.0-beta.28.3] node: 7.2.0 os: win32 x64 @angular/common: 2.4.7 @angular/compiler: 2.4.7 @angular/core: 2.4.7 @angular/forms: 2.4.7 @angular/http: 2.4.7 @angular/platform-browser: 2.4.7 @angular/platform-browser-dynamic: 2.4.7 @angular/router: 3.4.7 @angular/cli: 1.0.0-beta.32.3 @angular/compiler-cli: 2.4.7

Repro steps.

create an app with ng-cli. create a build for a server (not ng serve) with base-href set to xxx. use url’s in scss (like background-image: url(‘/assets/img/myimg’). the imagepath for the url will be set to server/assets/img/myimg instead of server/xxx/assets/img/myimg. If I try to use a relative path in the scss (ie url(‘./assets/img/myimg’) ) i get a compileerror, that this is not resolveable. If i try to use url(‘assets/img/myimg’) this is also not resolveable for the compiler. my .angular-cli.json has this section: “assets”: [ “assets”, “favicon.ico” ],

The log given by the failure.

There is only a compileerror that the path is not resolveable or a runtime 404 when the resource is not found without using the base-ref in the url.

Mention any other details that might be useful.

There is a workaround for this issue: i set the style in my component directly and inject it like this: <span … [style.background-image]=“getLogo()”></span>

getLogo() { return this._domSanitizer.bypassSecurityTrustStyle(url(${this.logosrc}); }

where logosrc = “./assets/img/myimg”


Thanks! We’ll be in touch soon.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:11
  • Comments:26 (4 by maintainers)

github_iconTop GitHub Comments

20reactions
Piyush247commented, Dec 13, 2017

fhbgjtgyjhuyhj

14reactions
maxpfandlcommented, Feb 17, 2017

@chriseenberg this actually works fine for me: simply use

<img src="./assets/img/myimg">

and make sure your assets folder is set in .angular-cli.json in the assets section. I’ve used this pattern also in the scss-files, but the newest version broke this and i get a compile-error that those path’s cannot be resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular: Using base-href in combination with scss urls
I am using Angular 11. I am using the filter rule but it of course works with bacground-url or any other css rule...
Read more >
Angular --deploy-url and --base-href - The art of simplicity
This parameter will update the generated url's for our assets(scripts, css) inside the index.html. To make your assets available at /angularapp/ ...
Read more >
Static Asset Handling - Vite
Assets that are not included in the internal list or in assetsInclude , can be explicitly imported as a URL using the ?url...
Read more >
Assets & caching - Infinum
To understand how base href affects assets loading, we will take a look at an example of image loading via the <img> element....
Read more >
Using Angular's Base HREF in Paths - Shawn Wildermuth
No Comments. I was recently working with a client and they were having an odd problem with Angular. They'd build their Angular ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found