ng build base-href issue (angular cli 8)
π Bug report
Command (mark with an x)
- [ ] new
- [x] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc
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)
npm install -g @angular/cli
ng new my-dream-app
-- 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
no exception
π Your Environment
Angular CLI: 8.0.0
Node: 10.16.0
OS: win32 x64
Angular: 8.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.800.0
@angular-devkit/build-angular 0.800.0
@angular-devkit/build-optimizer 0.800.0
@angular-devkit/build-webpack 0.800.0
@angular-devkit/core 8.0.0
@angular-devkit/schematics 8.0.0
@ngtools/webpack 8.0.0
@schematics/angular 8.0.0
@schematics/update 0.800.0
rxjs 6.4.0
typescript 3.4.5
webpack 4.30.0
Anything else relevant? no
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???
Why is the thread closed??? so many people experience this problem, and no valid solution was presented by the Angular team. 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 --rebase-root-relative-css-urls true option for prod build. Extracting the images from asset folder is imho bad for several different reasons :
- It does clutter the top-level folder on your server. Deployments should be performed by automatic tools, but you cannot always exclude a manual intervention on your server. If so, acting in a list of hundreds of files is risky.
- It does duplicate the assets. If you do not want duplication, you should move your CSS-relatively-referenced files to a folder which is not copy/pasted as-is into production build like "assets" folder (assuming "assets" is the default name when generating a project). But these files are eventually renamed using a hash, so you cannot use them in non-CSS contexts, for instance HTML templates.
- Due to the files being copy-pasted to the root output folder, multiple image files used by CSS can have conflicting names for local
ng serve. Just add two different images into two different folder of theassets. Use them both in CSS with relative paths. Only one of them will be available due to conflicting name. Workaround : create anng servebuild configuration which is using theoutputHash: 'media'similar to prod build.
We do this:
<script type="text/javascript">
var parts = document.location.pathname.split('/');
var baseHref;
for (var i=0; i<parts.length; i++) {
var part = parts[i];
if (part.length > 0) {
baseHref = part;
break;
}
}
document.write("<base href='" + baseHref ? "/" + baseHref + "/" : "/" + "' />");
</script> </body> </html>
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.