Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compress/optimise css for simplecache (Trac #1962) #1962

Closed
elgg-gitbot opened this issue Feb 16, 2013 · 3 comments
Closed

Compress/optimise css for simplecache (Trac #1962) #1962

elgg-gitbot opened this issue Feb 16, 2013 · 3 comments

Comments

@elgg-gitbot
Copy link

Original ticket http://trac.elgg.org/ticket/1962 on 40188001-05-03 by trac user milan, assigned to unknown.

Elgg version: 1.7

I've included a css compression method that runs just before the css view gets saves into simplecache:

Elgg 1.7 (with all modules enabled), no compression:
98KB

Elgg 1.7 (with all modules enabled), with compression:
82KB

Which saves (in this case) 16KB per hit, on the site i'm developing with more modules it saves a larger 25KB per page hit, probably more for more complex sites.

I modified the elgg_view_regenerate_simplecache() to do this following:

/**
 * Regenerates the simple cache.
 *
 * see elgg_view_register_simplecache
 *
 */
function elgg_view_regenerate_simplecache() {
    global $CONFIG;

    // todo elgg_view() checks if the page set is done (isset($CONFIG->pagesetupdone)) and
    // triggers an event if it's not. Calling elgg_view() here breaks submenus
    // (at least) because the page setup hook is called before any
    // contexts can be correctly set (since this is called before page_handler()).
    // To avoid this, lie about $CONFIG->pagehandlerdone to force
    // the trigger correctly when the first view is actually being output.
    $CONFIG->pagesetupdone = TRUE;

    if (isset($CONFIG->views->simplecache)) {
        if (!file_exists($CONFIG->dataroot . 'views_simplecache')) {
            mkdir($CONFIG->dataroot . 'views_simplecache');
        }

        if (!empty($CONFIG->views->simplecache) && is_array($CONFIG->views->simplecache)) {
            foreach($CONFIG->views->simplecache as $view) {
                $viewcontents = elgg_view($view);
                if ($view == 'css') {
                    $viewcontents = compress_css($viewcontents);
                }
                $viewname = md5(elgg_get_viewtype() . $view);
                if ($handle = fopen($CONFIG->dataroot . 'views_simplecache/' . $viewname, 'w')) {
                    fwrite($handle, $viewcontents);
                    fclose($handle);
                }
            }
        }

        datalist_set('simplecache_lastupdate', 0);
    }

    unset($CONFIG->pagesetupdone);
}

/* compress css content - see http://websitetips.com/articles/optimization/css/crunch/ */
function compress_css($buffer) {
    /* remove comments */
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, new lines, etc. */        
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    /* remove unnecessary spaces */        
        $buffer = str_replace('{ ', '{', $buffer);
        $buffer = str_replace(' }', '}', $buffer);
        $buffer = str_replace('; ', ';', $buffer);
        $buffer = str_replace(', ', ',', $buffer);
        $buffer = str_replace(' {', '{', $buffer);
        $buffer = str_replace('} ', '}', $buffer);
        $buffer = str_replace(': ', ':', $buffer);
        $buffer = str_replace(' ,', ',', $buffer);
        $buffer = str_replace(' ;', ';', $buffer);

    return $buffer;
}   

see this link as reference:
http://websitetips.com/articles/optimization/css/crunch/

@elgg-gitbot
Copy link
Author

trac user milan wrote on 40195460-07-31

After some further testing, we've found that this modified version of compress_css works happier with older versions of IE.

/* compress css content - see http://websitetips.com/articles/optimization/css/crunch/ */
function compress_css($buffer) {
    /* remove comments */
        $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, new lines, etc. */        
        $buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);
        $buffer = str_replace(array('  ', '   ', '    '), ' ', $buffer);
    /* remove unnecessary spaces */        
        $buffer = str_replace('{ ', '{', $buffer);
        $buffer = str_replace(' }', '}', $buffer);
        $buffer = str_replace('; ', ';', $buffer);
        $buffer = str_replace(', ', ',', $buffer);
        $buffer = str_replace(' {', '{', $buffer);
        $buffer = str_replace('} ', '}', $buffer);
        $buffer = str_replace(': ', ':', $buffer);
        $buffer = str_replace(' ,', ',', $buffer);
        $buffer = str_replace(' ;', ';', $buffer);

    return $buffer;
}   

@elgg-gitbot
Copy link
Author

Milestone changed to Elgg 1.9 by brettp on 40768991-05-09

@elgg-gitbot
Copy link
Author

brettp wrote on 40769573-11-20

Closing in favor of 2110 (more discussion).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant