Santhosh
Santhosh

Reputation: 20426

how to load minify css in production envirn

I have been loading so many JS and CSS in my project. To improve my site performance, I started with YUICompression integrated with Ant build. So each time when I build the project it creates minified file with appending"-min.js"

Example: myscript.js after build, new file "myscript-min.js".

Now I have changing all the files to load myscript-min.js in my pages.

Is there any automation or simpler way to load the minify file.

Thanks in Advance!!!

Upvotes: 6

Views: 1781

Answers (3)

Jorre
Jorre

Reputation: 17581

If you're using PHP, just do the following:

Edit the apache config file on your production machine and add this line to httpd.conf (restart apache afterwards). On a shared hosting you should try .htaccess if you don't have access to httpd.conf.

SetEnv ENVIRONMENT production

This simply adds a variable to apache telling your that you're running in production mode. On your development machine, change the value "production" to "development" or whatever makes sense to you.

Then in your PHP file, you can switch between loading the full JS files and the minified one, like so:

if(isset($_SERVER['ENVIRONMENT']) && $_SERVER['ENVIRONMENT'] == "production")
    {
        ... production minified JS here
    }
    else
    {
        ... development unminified JS here
    }

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272106

If you (can) use PHP in your project then have a look at the minify project. It takes care of most of the chores. You are free to use uncompressed versions of your CSS and JS files, minify will compress them on-demand when these files are requested over HTTP.

Upvotes: 1

Peter Kruithof
Peter Kruithof

Reputation: 10764

In your code, try to determine the environment (production or development) from where you're loading the page. For instance, when developing on a local machine, you can check your IP address, a server environment variable (using Apache SetEnv), script path, etc. Using that data, either load the minified script (in production environment), or the separate scripts (in your development environment).

I am assuming that you're using a server side scripting language, like PHP. If you're serving static HTML files, it gets a bit more tricky (I'm thinking dynamic javascript loading or something).

Upvotes: 2

Related Questions