angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

--deploy-url will have no effect on assets paths

Open ghost opened this issue 9 years ago • 44 comments

Please provide us with the following information:

Versions.

Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here: @angular/cli: 1.0.0-beta.30 node: 7.5.0 os: win32 x64 @angular/common: 2.4.6 @angular/compiler: 2.4.6 @angular/core: 2.4.6 @angular/forms: 2.4.6 @angular/http: 2.4.6 @angular/platform-browser: 2.4.6 @angular/platform-browser-dynamic: 2.4.6 @angular/router: 3.4.6 @angular/cli: 1.0.0-beta.30 @angular/compiler-cli: 2.4.6

Repro steps.

Was this an app that wasn't created using the CLI? What change did you do on your code? etc.

  • create app with the cli
  • use img src="assets/logo.png" in a template
  • ng build -d mysubfolder -bh mysubfolder copy the content of the dist to "htdocs/mysubfolder" on any webserver or use app.use('/mysubfolder', express.static(path.join(__dirname, angularDist))); on a node server.

The log given by the failure.

Normally this include a stack trace and some more information.

404 on all assets because it will ignore base href or deploy url and just use GET /assets/logo.png

Mention any other details that might be useful.

ghost avatar Feb 08 '17 10:02 ghost

Hello! I am running into the same issues.

I am deploying an app written by someone else. Components have hard-coded paths like /assets/img/etc (note the leading slash compared to the OP); the main application files (*.bundle.js and sourcemaps) end up in the root of the dist directory. This is okay for frontend development (using ng serve, /index.html can load /index.bundle.js and /assets/img/etc), but for the actual deployment I would ideally have everything under URLs looking like /static/app/ and /static/assets/ for example.

Ideally, we would have set up different paths and directories from the start if we had thought about the server considerations. Trying to minimize changes at this stage, I found the one-line documentation for base-href and deploy-url not very enlightening, but PR #2276 seemed like deploy-url was intended to solve my issue. I am still experimenting in CI to see if that works for the main js files, but this ticket tells me it won’t help with the assets URLs.

merwok avatar Feb 08 '17 22:02 merwok

I had this problem too. All my resources are served on a subdomain. Using the deployUrl option solved the issue for the scripts/styles being loaded from the subdomain inside the index.html. This guaranteed me to not worry about base href so that my routing will work properly, it remains as default /. Regarding other resources like images, try loading them from your styles using the background-image property. As the styles loads from the subdomain, it loads the necessary resources from the subdomain too, including fonts, etc. Now I have an issue with the favicon as it is an asset, the compiler does not put the deployUrl in the href attribute and I have to put it manually.

ghost avatar Feb 21 '17 17:02 ghost

Facing the same issue here, I've got some CSS like this:

label.custom-radio-default-label {
  margin: 0;
  clear: none;
  cursor: pointer;
  background: url(/assets/images/custom-radio-normal.svg) left center no-repeat;
  background-size: 20px;
  font-size: 16px;
  font-weight: normal;
}

and building with ng build --deploy-url https://test.com doesn't seem to have any effect on the output.

victormejia avatar Mar 03 '17 20:03 victormejia

Largely fixed by https://github.com/angular/angular-cli/pull/4803.

The ng build --deploy-url https://test.com (with http) scenario isn't catered for currently though.

filipesilva avatar Mar 09 '17 12:03 filipesilva

@filipesilva #5254 Here is a related issue that I've posted 3 days ago. Hope you will fix this soon.

ghost avatar Mar 09 '17 14:03 ghost

@filipesilva Does #4803 rewrite paths for assets that are images or javascript files, or only CSS files?

merwok avatar Mar 09 '17 17:03 merwok

@es-lab thank you, I will track the http bug in your issue instead. I'm closing this one then.

@merwok it rewrite paths only for assets in url() inside CSS. Those are the only ones that need path rewriting afaik.

filipesilva avatar Mar 10 '17 13:03 filipesilva

The example in the original message is an image. The source code contains img src="assets/logo.png" (in my case, src="/assets/logo.png") but the files will be served from /static/assets/.

merwok avatar Mar 10 '17 16:03 merwok

@merwok if you have url('/assets/logo.png') in your CSS, and use --deploy-url=static/, the CLI should build that path to url('/static/assets/logo.png'). Mind you that images under a certain size are just inlined.

Make sure you're using the latest RC though.

filipesilva avatar Mar 11 '17 22:03 filipesilva

Thank you for the reply! I’ll check with my team when we can upgrade and adapt our CI to benefit from the new behaviour.

merwok avatar Mar 12 '17 00:03 merwok

@filipesilva It affects urls stated in CSS files, but it doesn't those which are directly stated in markup like <img src="assets/logo.png">. So if I build using --deploy-url=static/ it won't be
<img src="/static/assets/logo.png">

milankr avatar Mar 13 '17 20:03 milankr

@milankr we do not process HTML, so that is expected behaviour.

filipesilva avatar Mar 13 '17 21:03 filipesilva

Ah! If only index.html is changed and not the other HTML files, I’ll have to keep my sed workaround.

Next project, we’ll get the paths right from day one!

merwok avatar Mar 14 '17 03:03 merwok

@filipesilva can you tell me what the reasoning was to only process asset urls in css, and not in the html templates?

tomasklingen avatar Apr 13 '17 13:04 tomasklingen

@filipesilva What is the strategy using src paths with a project using --deploy-url? @merwok comment points out this issue:

"The example in the original message is an image. The source code contains img src="assets/logo.png" (in my case, src="/assets/logo.png") but the files will be served from /static/assets/."

The request for the image resource will 404.

macrivello avatar Apr 21 '17 06:04 macrivello

Is there any workaround to make this work for <img src="[...]" />? (Or any other path in the html files)

bergben avatar Jun 10 '17 19:06 bergben

@bergben the only workaround that now reading the thread is to use Javascript binding so you configure the base path in your environment config files 👎

yordis avatar Jun 13 '17 16:06 yordis

@filipesilva any updates on this? I need HTML processing as well

yordis avatar Jun 13 '17 16:06 yordis

FWIW, I lived with renaming assets to static/assets and doing a mass search-and-replace. Source paths now match deployed paths.

merwok avatar Jun 13 '17 17:06 merwok

I need to put Index.html in root directory i.e. /dist folder and all script files in dist/scripts directory, can anyone please guide how do I achieve this? So that when I deploy it on server my index file resides in root of my domain (mydomain/index.html) and all scripts in scripts directory (e.g. mydomain/scripts/****.js)

naveedahmed1 avatar Sep 01 '17 16:09 naveedahmed1