Matheus Lima
Matheus Lima

Reputation: 2143

AngularJS: Absolute vs Relative Paths

I'm starting an AngularJS application and I need to call my static files with an absolute path in production mode and with a relative path in development.

If for example I need to access a JS static folder, in production I'll need to call: https://statics.domain.com/js and in development I'll have a regular assets/js folder.

Is there a best approach for that?

Thanks!

Edit: An important note is that this app is separated from the backend.

Upvotes: 1

Views: 2126

Answers (2)

Eterm
Eterm

Reputation: 1808

You can use the Base Tag (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) to set the base path for all relative URLs.

So you could have in development and change that base href to static.domain.com in production.

However, the base tag is a bit obscure and I wouldn't recommend it for serious work.

Other ideas:

Could you use your hosts file to redirect static.testdomain.com to your local environment.

Could you use an environment variable or other configuration setting to alter the javascript references.

Upvotes: 2

Tibos
Tibos

Reputation: 27823

The best approach is to have your server serve different index.html files depending on the environment (production versus development). If you don't use a server at the moment you could consider a lightweight NodeJS one.

You could even use a templating engine (ex Express + Jade) and have the same index.html file be constructed differently based in the environment.

Upvotes: 0

Related Questions