--deploy-url will have no effect on assets paths
Please provide us with the following information:
Versions.
Please run
ng --version. If there's nothing outputted, please run in a Terminal:node --versionand 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.
@tomasklingen it was attempted a couple of times but at the time we couldn't find a safe approach for it, especially since you can compose urls in html using component variables. So any change there was very likely to break some projects, which is a breaking change and thus outside of 1.x.
@filipesilva Can we get some sort of reserved string/variable/token that you can search in HTML and TS to replace all occurences?
@filipesilva ,
For those of us who are not in production yet. Should we think about some workaround or we can expect some fix in next releases(even not in 1.x)?
When you deploy to prod, extract <base "/"> of the index.html, just this!
I think there are two workarounds: either move your source files and do a mass search and replace so that the source paths match the deployed paths (what I mentioned in a previous comment), or use the ng eject command to get access to the raw webpack config and get out of ng cli.
I have a work around in the mean time.
I put the deploy path in my environment.ts files.
Then in the components that I want to refer to files in the assets folder, I set href/src to "{{deploypath}}/assets/...". You will have to create a deploypath component variable and assign the environment.deployPath in the constructor of the component.
@zsmorgan21 We do the same thing. When we deploy, we have a script that updates our package version and replace the value in our environment file. It works so far.
I'll reference this StackOverflow post https://stackoverflow.com/questions/47885451
As I've ran into the same issue, deployUrl manages the path in the index.html, but not across the application's assets/ files. The workaround is to have a CI/CD post-build script which pretty much does in-place replace of assets path in the built files using sed.
The proper CDN support for any client-side apps framework is quite important; I'm not deep into Angular/CLI to know all the ins and outs to propose a solution, but it is something we're definitely running into when planning CDN deployments.
Generally, setting the base-href is a cleaner option. It not only handles initial scripts, but also lazy scripts, processed resources, and most importantly the application's assets. The use of Angular's APP_BASE_HREF (documentation) in combination can provide a full solution for CDN use cases such as described here.
@clydin,
base-ref alone wont solve the issue with assets. However, I do think setting and referencing Angular's APP_BASE_HREF is the better option than using angular CLI's environment variables. You can set it like @clydin mentioned above. You can reference it like so...
https://stackoverflow.com/questions/39287444/angular2-how-to-get-app-base-href-programatically
Could you elaborate on why using the --base-href option will not solve the asset issue?
All relative fetches made by the browser will be prefixed by the provided base href. This would include all relative asset URLs within the application. This is also why it is generally a cleaner solution as opposed to the --deploy-url option which requires the supplied URL to be embedded throughout the application to function. The APP_BASE_HREF token (which is used as the Angular router base path) would generally be set / in this scenario; as the default value is actually the base href of index.html.
When you refer to base-href, are you referring to the command line
parameter or the base tag and href attribute? I think there is some
confusion there. Your initial post was missing the "--" from base-href.
Zachary S. Morgan Hampden-Sydney College Class of '14
On Tue, Jan 23, 2018 at 3:19 PM, clydin [email protected] wrote:
Could you elaborate on why using the --base-href option will not solve the asset issue?
All relative fetches made by the browser will be prefixed by the provided base href. This would include all relative asset URLs within the application. This is also why it is generally a cleaner solution as opposed to the --deploy-url option which requires the supplied URL to be embedded throughout the application to function. The APP_BASE_HREF token (which is used as the Angular router base path) would generally be set / in this scenario; as the default value is actually the base href of index.html.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/4517#issuecomment-359917240, or mute the thread https://github.com/notifications/unsubscribe-auth/AGtzhNWpbDTiBrLJ0YGFdOXHLsMPv-ydks5tNj7TgaJpZM4L6mvx .
Reading all this, and it still doesn't explain how to have assets mapped to a CDN, even with using this APP_BASE_HREF
This is the build command, which maps base-href to /subpath/ and deployUrl to CDN:
ng build --prod --aot --base-href "/subpath/" --deployUrl="//cdn.example.com/subpath/"
So, the app is served off of a webserver host's subpath (so, Angular routing must work with this subpath, as the link I shared explains), but angular files are stored on the CDN. With the above build command, built JS/CSS files are correctly referenced with CDN URL in the index.html, but then, inside the app, all the assets should be referenced from the CDN as well, which is not the case.
What is needed is the way to --deployUrl be applied to all the /assets/ during the build. With this APP_BASE_URL they'd be referenced with the value of the --base-href parameter, if I understand it correctly (correct me if I'm wrong; but that was the conclusion in the comments of the StackOverflow question - not supported).
Thank you
The purpose of the --base-href option is to set the value of the base element's href attribute within index.html. So either would be relevant to the discussion.
@zappan Something similar to the following command:
ng build --prod --base-href="//cdn.example.com/subpath/"
with APP_BASE_HREF set to /subpath/ in the entry module should work. Relative URL usage within the app is critical as the browser will only apply the HTML base href to relative URLs.
As an aside, --aot is enabled by default with --prod.
Can some one throw light on this, how to achieve context path other than / for my application. I had been reading whole night for this issue but could not find a way
@zappan did it ever work for you? I tried without any luck and am stuck with it. Could you please assist?
@ga-kumar wrote a script using sed to replace paths that I run after the build.
Haven't tried what clydin suggested (or maybe I have and it didn't work, can't remember any more)
thanks @zappan , I also ended up doing same.. Clydin's suggestion didn't work for me either or not sure if I understood correctly.. anyway.. thanks again..
Having the same issue. My first attempt was to use the --base-href but this will affect also the angular router so it is not a solution.
For now I am going to place a deployUrl to every src of html images.
@albanx Use --base-href for the applications assets and the APP_BASE_HREF token to control the Angular router.