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

+1 for this

vukasin-nikodijevic avatar Jun 16 '17 14:06 vukasin-nikodijevic

@Brocco could we do something like i18n for the URL on the HTML, because I can see the big issues on it (no everything have to be href or src) but we could use the same strategy in the worse scenario.

yordis avatar Jun 16 '17 14:06 yordis

This is something I'd like to have if we can find a safe way to do it. For reference, there's some more context in https://github.com/angular/angular-cli/issues/4517#issuecomment-326970581.

filipesilva avatar Oct 02 '17 11:10 filipesilva

Any update here?

rajdeep26 avatar Oct 24 '17 17:10 rajdeep26

how to fix it?

lqclester avatar Mar 20 '18 07:03 lqclester

+1 fix it please!

i have an app with different base href and the sass is compiling correctly the routes but not in html link, src, etc

RSginer avatar Mar 27 '18 11:03 RSginer

it's not only inconvenient, the code won't work.

leichenau avatar Mar 29 '18 00:03 leichenau

I'm using ng5 now. My workaround is to add a prefix {{baseHref}} of each src in HTML template.

<img src="{{baseHref}}/assets/sample.png">

@RSginer

snow-swallow avatar Apr 28 '18 01:04 snow-swallow

@timelyxyz thank you so much! this is a possible solution but you need to create a local variable in each component or it is a global variable?

RSginer avatar Apr 30 '18 06:04 RSginer

@timelyxyz this does not work for AOT :(

timbru31 avatar May 17 '18 13:05 timbru31

This is horrible.

christophedemey avatar Jun 10 '18 13:06 christophedemey

pipe this plz, 我的手都快加累死了。

100cm avatar Sep 17 '18 10:09 100cm

Best solution for now is to use the background-image css property. The cli will add the base href attribute to this.

Also you could define the url in your environments.ts file and import it into your components.

davidmcdougs avatar Mar 22 '19 14:03 davidmcdougs

Workaround until Angular takes the effort to fix, is to add a property to your environment variable, like deployUrl: '/xyz/', then for all 'assets' objects, you can add the following logic in html:

<img src="{{getEnv().deployUrl}}assets/img/logo.svg">

For i18n, I used a custom loader.

gazsiadam avatar Apr 08 '19 06:04 gazsiadam

I went through the code of the Ivy rendering engine, compilers and CLI.

The main problem is that the url(...) replacement in the css files are done by webpack. The Html template is parsed by Angular compiler.

I think an AssetUrlVisitor could solve this problem. It should add the route as string concat. That would make img[src] attribute work without any binding. If it has binding the url should be added before the interpolation runs.

The visitor should look for img with src and make the changes to it. Probably there are more scenarios for example src-set for the future. Extension with the visitor would be easy. (Ivy is really great!)

The new url config should have a new name, so it could be introduced without breaking changes. E. g. assetUrl. Then in later versions could be merged with deployUrl.

The policy how to change the links could be the same as the algorithm made by webpack, for the best consistency.

csutorasr avatar Apr 13 '19 01:04 csutorasr

Hi,

I have a hybrid app. Not just angular hybrid, but a lot of stuff. So my deployUrl is virtual directory in IIS. I can not, or couldn't figure out how to, change baseHref, because for the page url '/' is just the base i need.

I have pretty much the same setup on dev machine as on server. So I could fix the assets paths thing by adding deployUrl to my src attributes, which is... yes on of those "fixes".

However now, I'm using storybook and it doesn't have to know about deployUrl, so all my assets are 404.

Is there way to make it work without touching baseHref?

anvlkv avatar Jul 11 '19 13:07 anvlkv

One quick note - if, after build, the assets are located in the base folder of index.html, you can use relative paths to access them by prefixing with ".": <img src="./assets/img/logo.svg">

This seems to be working with the baseHref build flag, since the path is relative.

s4m0r4m4 avatar Aug 13 '19 14:08 s4m0r4m4

My workaround is to use nginx router rewrite.

nginx.conf:

        location /assets/ {
            rewrite ^/assets/(.*)$ http://cdn.example.com/path/assets/$1 redirect;
        }

fengjihua avatar Sep 08 '19 12:09 fengjihua

Has someone discovered any workaround to this?

I don't want to append baseHref to all my images in HTML templates. I am playing around with custom webpack configuration, but nothing seems to be working as of now.

Has anyone seen any progress with a custom Webpack configuration or anything for that matter?

s3rot0n1n avatar Sep 11 '19 14:09 s3rot0n1n

A web browser will use the configured HTML base HREF value with all relative URLs. As long as relative paths are used with a properly configured base HREF value, no custom configuration should be required.

The deploy URL option is entirely different and is best avoided where possible as it hard codes the path within the application (potentially in multiple locations). An alternative for deploy URL that covers many situations is to use a combination of an HTML base HREF (with potentially not having one) and the Angular APP_BASE_HREF token (docs). The later of which controls the base URL for the Angular router which will otherwise default to the HTML base HREF.

clydin avatar Sep 11 '19 14:09 clydin