Setting different asset paths in config for different environments


#1

I have Kirby set up using the suggested Multi-environment configs from the docs.

Development environment config:
/site/config/config.dev.mydomain.com.php

Production environment config:
/site/config/config.mydomain.com.php

I’m looking to have different asset path for each environment.

Development environment asset path:
/assets/dev/

Production environment asset path:
/assets/prod/

What’s the best way to set the different asset paths inside each config?


#2

I think the easiest way to do that, is via a config setting:

E.g. in /site/config/config.dev.mydomain.com.php

c::set('assets.path', 'dev');

In your header

<?= css('assets/' . c::get('assets.path', 'prod') . '/app.min.css') ?>

Note that I’ve used prod as a fallback value here, in case the assets path is not set. Instead of only setting the subfolder, you can of course set the path to assets/dev' etc. instead and modify thecss()`helper accordingly.


#3

This is great! Thank you! I didn’t think it would be as easy as that.

Are there any downsides to using this technique as you start to use it for more and more assets? - say I’m using this for 50 assets on a page?


#4

I don’t see any downsides; the only downside is loading 50 assets per page.


#5

My hint:
Use less or sass to avoid so many css-files! Google is your friend for details…

[Added:]
May be you want to use different snippet files header.php, one for each environment.


#6

Yes, 50 assets would be a bit extreme :smile: I was more concerned if there were any speed issues on Kirby’s side as this method isn’t “the norm” I guess.

FYI, I’m using Gulp to compile assets for both environments. When I compile for production these assets are minified and uglified before deployed.

Anyway, thank you for all your help :slight_smile:


#7

No, speed-wise, I think the call to the config setting is the last thing you need to be concerned about. If you use it multiple times in a template, I’d store the value in a variable, anyway.