2

I am using Angular CLI to build my Angular 4 project. I have a requirement to run the application

  1. as a standalone application
  2. as an application embedded in a portal page

The application runs well as a standalone app.

When embedded in a portal whose static content is served from a CDN, the resources that are loaded using relative URLs do not get loaded as they are nested under a different root directory under the CDN.

e.g. In my standalone app, I have a background image that I refer in my scss file as background: url('/assets/images/bg.jpg'). However, when embedded in portal, the relative URL is /cdn/app/assets/images/bg.jpg.

Is there a way to change the relative URLs used in SCSS and HTMLs as part of ng build ? Can I define a variable that holds my CDN path and Angular-CLI replaces the variable with the actual value at build time?

e.g.

background: url('$cdnAppRoot/assets/images/bg.jpg');

replaced with

background: url('/cdn/app/assets/images/bg.jpg')

1 Answer 1

1

When you build for production you can add the deploy-url param to change where the assets should be read from.

ng build -d /cdn/app

I suggest adding a new npm script called prod to make this easier

{
   "scripts: {
     // Other npm scripts
     "prod": "ng build -d /cdn/app"
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect solution. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.