-
Notifications
You must be signed in to change notification settings - Fork 12k
ng build base-href issue (angular cli 8) #14587
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
Comments
This is actually expected behavior. The HTML base HREF is intended to be a runtime value used by the web browser and not a build time constant. The The release notes provide more detail regarding this change:
|
I have the same problem. |
Add |
So how should we create url paths to images that we want to keep in assets folder and use custom --base-href in the long run (Angular 8+)? From now on all images should be kept along with components? In my case in scss files:
leads to CssSyntaxError - can't compile
doesn't work after build because the resources are referenced to the root path of the url, not the one given in base-href or deploy-url
works, however all images are copied from assets/images folder to the root folder of the build output, so I end up having two copies of each image on the server |
also interested in the answer to the above question. |
But seeing at that is not even possible, who's bright idea was it to think that breaking the only viable solution with only a flag to restore it; that will be soon depricated post v8 was the best course of action.... So was the plan to just break it and have no solution? |
I solved the issue in angular 8 by adding following code in AppModule |
Is not working for urls in less for me |
Is working only in component styles and not working in main less file for me |
With all due respect @clydin, how is this issue solved when the only thing that seems to work ( |
as @DzmVasileusky has pointed out this does not work for LESS. @clydin What is our solution for this closed ticket. |
A newly generated project contains the following configuration option:
This tells the build system to copy all files verbatim that are within the By using a relative path for a resource file within a stylesheet, the build system is able to process and manage the resource. Since the build system is now managing the resource, it will process the file as needed and add it to the output of the application. File name hashing for long-term caching functionality being the only current processing. This general behavior is common among typical standalone Webpack configurations, Vue.js, and Create React App. In fact, Create React App strongly recommends the use of relative URLs for files used within the application for the following reasons:
|
That's an issue with Sass not rebasing URLs based on imports and not an issue with others such as LESS or Stylus since that functionality is provided. The typical method of addressing the issue is to use Sass variables to point to a resource location. This is used effectively in packages such as Font Awesome. The Sass issue tracking the problem: sass/sass#2535 |
@clydin What is the recommended way to import assets from LESS component styles, then? I used to be able to just do Edit: After playing around with it it seems that going relative pathing from the |
I have been scratching my head for days after trying to make assets work on my site deploy with a custom The relative approach sounds a bit messy if you ask me, because deeper components would have to go back several folders to find a single asset unless used on the same level, just saying. EDIT: The relative approach also create copies of assets on my root folder. |
Unless I'm missing something, the relative urls don't seem to pay any attention to the assets in the angular.json, which means as far as I can tell there is no way to provide a url for an asset in my library project. We were using absolute paths, as a hack workaround, but if rebaseRootRelativeCssUrls is removed, we wont even have that. |
Rather than the rebaseRootRelativeCssUrls you might try programmatically setting the base href: That should give you a deprecation-tolerant work-around. For some reason the environment.baseHref settings for angular 8 were not populating the document's base href. So relative image paths (in a src="" attribute) were breaking which had worked previously, I imagine relative url paths in styles would have the same problem. The solution above worked at least in chrome. I think that the sass/less behavior being reported is possibly symptomatic of the issue of the baseHref not being honored in the output HTML. You shouldn't have to rebase anything if a configuration/environment's baseHref setting is properly being honored. |
@filipesilva & @clydin is this maybe something that should be added to update.angular.io? It's likely breaking a lot of people and I think it would be helpful to have proper documentation for it. What do you think about it? |
My previous comment may not be true for earlier versions of some browsers. And is problematic if some resources load before programmatic setting of baseHref. The right solution is one that sets baseHref in index.html at compile time. Doing a little further experimentation I've realized that the baseHref defined in the environment.ts (or replaced environment.ts) is not the one being used for the document when it loads. If I specify baseHref in the angular.json file for a configuration then it appears as the href for the base tag in the compiled index.html file. I don't know if I am misremembering or if something changed but I thought that putting things in the environment.ts files (for each config) meant that the config options would be grabbed from the environment file (after replacement) such that in my environment.dev.ts file if I set baseHref there and then set the replacement section in the "dev" configuration in angular.json to replace environment.ts with environment.dev.ts then my environment specific config values would be used from environment.dev.ts. The behavior is more like the environment file is just for properties you want to make available for the specific deployment environment and the config section is completely independent so you have to put the same baseHref value in both places if you want to refer to environment.baseHref in the app and have the baseHref set to the right initial value when index.html is compiled. My personal preference would be to be able to add config properties (other than replacements ) to environment files and have the config properties inherit from the replaced environment file so that I can set config properties I want to make available to the app in environment.x.ts and config properties only relevant to compilation available in angular.json and not have to replicate any values to both. However, if in fact the environment files are going to be independent of compilation configuration that needs to be more clearly pointed out so that baseHref is set in angular.json for configurations for compiling and set in environment.x.ts for availability to the application. |
@clydin Is there any estimate when rebase-root-relative-css-urls will be removed? v9? v10? |
Why is the thread closed??? so many people experience this problem, and no valid solution was presented yet. Did anyone manged to solve this issue, any workarounds??? |
We are still waiting for another solution since the provided one doesn't match the expectations. |
Wating for a better solution too. Currently I'm using the already-deprecated
|
We do this: |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 Bug report
Command (mark with an
x
)Is this a regression?
Yes, works with angular: 7.2.0
Description
The base-href flag works differently in angular 8. I hope you can help.
In angular 7:
ng build --prod app1 --base-href /app1/
after the command all my relative path in the scss are changed to the proper one.
source:
.example { background: url('/assets/img/picture.png') no-repeat center center; }
deployed:
.example { background: url('/app1/assets/img/picture.png') no-repeat center center; }
With angular 8 nothing happen:
source:
.example { background: url('/assets/img/picture.png') no-repeat center center; }
deployed:
.example { background: url('/assets/img/picture.png') no-repeat center center; }
🔬 Minimal Reproduction
-- get angular-cli 8.0.0 and create a new app (with scss)
-- add a new class to the css (src/style.scss)
.example { background: url('/assets/img/picture.png') no-repeat center center; }
-- add a new class to a html file (src/app/app.compontent.html)
<div class="example" style="text-align:center">
-- run the build
ng build --prod --base-href /app1/
🔥 Exception or Error
🌍 Your Environment
Anything else relevant?
no
The text was updated successfully, but these errors were encountered: