X-Cart 4.5 and a CDN / Cookieless Static Domain


Written by

So today I was working on a site based on xcart 4.5 with an aim to speed up page load times. I had previously migrated all the background images into CSS base64 encoded data to save on the number of requests and file sizes etc.

Part of x-carts SEO tools and optimisation is that it will combine all CSS files into one but while it does that, it craftily rewrites specific parts mainly so URLs are correct which plays havoc with base64 encoded CSS images as it attempts to rewrite it all, so this file needs to be separate and defined manually in the template via the <link /> tag and not with the load_defer() function.

Anyway in regards to using a cookie-less Static domain or a Content Delivery Network for CSS, Javascripts, Images etc then you need to do some minor rewriting of x-cart.

Firstly, you need to setup your domain and relevant services to have the correct resources and the domain to work. I was using apache and included the following lines in the vhosts.

RequestHeader unset Cookie    
Header unset Set-Cookie

Which removes any cookies on that domain.

Once the domain is all setup and ready then we have to edit x-cart a little, I’ll try and write this as simply as possible.

Edit /smarty.php

approx. line 92
from:

$smarty->assign('AltImagesDir', $alt_skin_info['web_path'].'/images');
$smarty->assign('AltSkinDir', $alt_skin_info['web_path']);

to:

if ($_SERVER['HTTPS'] != 'on') {
    $smarty->assign('AltImagesDir', "http://cdn.domain.tld".$alt_skin_info['web_path'].'/images');
    $smarty->assign('AltSkinDir', "http://cdn.domain.tld".$alt_skin_info['web_path']);
} else {
    $smarty->assign('AltImagesDir', $alt_skin_info['web_path'].'/images');
    $smarty->assign('AltSkinDir', $alt_skin_info['web_path']);
}

and approx. line 107
from:

$smarty->assign('ImagesDir', $xcart_web_dir.$smarty_skin_dir.'/images');
$smarty->assign('SkinDir', $xcart_web_dir.$smarty_skin_dir);

to:

if ($_SERVER['HTTPS'] != 'on') {
    $smarty->assign('ImagesDir', "http://cdn.domain.tld".$smarty_skin_dir.'/images');
    $smarty->assign('SkinDir', "http://cdn.domain.tld".$smarty_skin_dir);
} else {
    $smarty->assign('ImagesDir', $xcart_web_dir.$smarty_skin_dir.'/images');
    $smarty->assign('SkinDir', $xcart_web_dir.$smarty_skin_dir);
}

then edit: /include/template/plugins/function.load_defer_code.php

at approx. line 111
from:

$cacheWebFile = $var_dirs_web['cache'].'/'.$label.'.'.$md5Suffix.'.'.$type;

to:

if ($_SERVER['HTTPS'] != 'on') {
    $cacheWebFile = "http://cdn.domain.tld/var/cache".'/' .$label.'.'.$md5Suffix.'.'.$type;
} else {
    $cacheWebFile = $var_dirs_web['cache'].'/'.$label.'.'.$md5Suffix.'.'.$type;
}

We have to check for SSL so we don’t get certificate errors and alerts about unencrypted content. There isn’t much information in regards to x-cart 4.5 and CDN’s but there’s a snippet on the wiki about it in older versions.