Welcome! Log In Create A New Profile


http-concat module to aggregate java-script/css files into a bundle on-the-fly

Posted by locojohn 
http-concat module to aggregate java-script/css files into a bundle on-the-fly
February 07, 2012 07:49AM
Dear friends,

I tried to submit this request to nginx development, but obviously I do not have permission to do this.

There's a wonderful little module written by Joshua Zhu as part of his tengine port of nginx. The module is called "http-concat". It allows aggregating multiple files of the same mime type into a single bundle, which dramatically reduces request time needed to fetch all files separately. We use it for aggregating Javascript and CSS files. This module is now distributed via the git repository: https://github.com/perusio/nginx-http-concat and it's possible to use it with nginx 1.1.14, which I just verified.

In short, this module can process URLs like: "/jquery/??jquery.asmselect.css,ui.imageuploader.css,jquery.linkselect.css" where all of the files followed after the "??" character sequence in the URL are concatenated and output to the client. It is currently very efficient and small module and does it's little job just fine.

However, there are several issues:

Issue #1: at this moment http-concat cannot take files from locations, where "alias" or "root" directives are used. Example:

location /jquery/ {
try_files $uri $uri/ @common;

location @common {
# handle common requests for all sites: /jquery/...., etc. if local version of the requested file is not available
root /opt/www/_common;


If we attempt to aggregate files from location /jquery, the request results in 404.

I would very much appreciate it if Maxim Dounin or Igor Sysoev or someone knowledgable in nginx internal structures could have a look at the source of http-concat module (URL: https://github.com/perusio/nginx-http-concat/blob/master/ngx_http_concat_module.c) and let me know what is needed to handle locations defined by "root" or "alias", so files could be concatenated in these locations. This is currently a major issue and a show-stopper for us. I contacted http-concat author, and he told me he currently does not know how to figure out path to files within locations that have "root" or "alias" definitions.

Second issue is that certain CSS files might use the standard "url" tag to point to external images. Unfortunately, when these links are relative, they become relative to the current page where the aggregation happens, but not relative to the CSS file where "url" tags are present. For example: "/jquery/jquery-ui-1.8.17.custom.css" of the standard jquery library contains 8 links to images/.... resolving to /jquery/images/...., and of course these links don't work when we attempt to aggregate jquery-ui-1.8.17.custom.css with other CSS files using http-concat in another folder. Before we used PHP to concatenate CSS/Javascript files, and we wrote the following code to automatically substitute relative urls to images with absolute urls, because absolute urls are known by the script for each CSS file in question:

$uri_path = dirname($css_file['uri']);
// example: url(relative/path/to/file) becomes: url(/absolute/and/not/relative/path/to/file) within a CSS file
$buf = preg_replace('/(:?\s*url\s*\()[\'"\s]*([^\/\'"].*)[\'"\s]*\)/isU', '$1' . ($uri_path == '/' ? '/' : $uri_path . '/') . '$2)', $buf);

It works like a charm, but it's in PHP. Could this be possibly ported to C, provided that nginx already uses PCRE library and so I assume it is somehow possible to implement within http-concat module.

Any patches or suggestions from knowledgable nginx/c experts are highly appreciated. I am sure this module would be beneficial for many. Questions like "Why do you need this module?" or statements like "It is better to concatenate all files in advance and serve them off as a static file" are not welcome.

Anxiously waiting for any hints and best wishes to everybody!


Edited 1 time(s). Last edit at 02/07/2012 07:51AM by locojohn.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 304
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready