Hello,

just recently I uploaded a website (www.leorealestate.gr) and tested it here: http://tools.pingdom.com/fpt/
In terms of Page Speed Performance, it looks like I need to remove query strings from static resources.
I have searched a lot and found these:
- http://www.lightrains.com/blog/remove-query-strings-static-resources-drupal
- https://gist.github.com/flashvnn/5b036bed25c15647d622
I did try the solutions recommended, but unfortunately with no results at all.

Can someone help me with this problem?

Comments

kirtivaland’s picture

Hello,
I am working on the same issue and find below solution to remove query string from CSS.

/**
* Implements template_process_html().
*/

// Remove Query Strings from CSS filenames (CacheBuster)
function MYTHEME_process_html(&$variables) {
$variables['styles'] = preg_replace('/\.css\?.*"/','.css"', $variables['styles']);
}

Hope this helps.

gpantikidis’s picture

Hello kirtivaland, thank you for your response. I tried placing your code in template.php and my site does not work at all.

alireza.13’s picture

do you replace "MYTHEME" with your theme name?

Onnia’s picture

Im just curios to know. My google searches found this topic as well: https://www.drupal.org/node/827490.
The answer was 6 years ago: "Ignore it. Bad advice from the app."

Is still valid answer? That Drupal cache is better the googles recommendation?

nehapandya55’s picture

Hi,

I also used this code but is not working for me .

Bes2theFort’s picture

/**
* Implements template_process_html().
* Remove Query Strings from CSS & JS filenames
*/
function themename_process_html(&$variables) {
$variables['styles'] = preg_replace('/\.css\?[^"]+/','.css', $variables['styles']);
$variables ['scripts']= preg_replace('/\.js\?[^"]+/','.js', $variables['scripts']);
}

/**
* Implement hook_image_style
* Override theme image style to remove query string.
* @param $variables
*/
function themename_image_style($variables) {
// Determine the dimensions of the styled image.
$dimensions = array(
'width' => $variables['width'],
'height' => $variables['height'],
);
image_style_transform_dimensions($variables['style_name'], $dimensions);
$variables['width'] = $dimensions['width'];
$variables['height'] = $dimensions['height'];
// Determine the URL for the styled image.
$variables['path'] = image_style_url($variables['style_name'], $variables['path']);
// Remove query string for image.
$variables['path'] = preg_replace('/\?.*/', '', $variables['path']);
return theme('image', $variables);
}

bdparker’s picture

Nice! Where do I place this? I'm looking to get a high score on my PageSpeed. :)

Tonkers’s picture

It deals with template code, so under sites/all/themes, navigate to your theme's folder, and then add these lines in template.php, making sure you replace themename with the actual theme name, and that the functions don't already exist in that file!

nellngun’s picture

Hi,after adding the patch I can't see newly loaded images to my website in the views section, even after clearing all cache. 

Any ideas?

Gaurav_drupal’s picture

can anyone provide the solution for D8 ?

bserem’s picture

This look promising: https://www.drupal.org/project/remove_querystring_from_static_resource
but it didn't work for me

--
http://srm.gr - Drupal Implementor

bserem’s picture

I made this: https://drupal.org/project/cachebuster
it works for CSS files, if you want it for other files it might be possible.

--
http://srm.gr - Drupal Implementor

jimconte’s picture

Nice!
You could pass an environment variable per environment, and it would only run in dev. You can also pass a new cache buster to disable browser cache

function mymodule_css_alter(&$css, AttachedAssetsInterface $assets) {
    if (getenv('DEPLOYMENT_ENVIRONMENT') == 'development') {
        foreach ($css as &$file) {
            if ($file['type'] != 'external') {
                $file = [
                    'type' => 'external',
                    'data' => '/' . $file['data'] . '?' . time()
                ] + $file;
            }
        }
    }
}
srijeeta’s picture

https://www.drupal.org/project/remove_querystring_from_static_resource
This worked for me in Drupal 8
Increased my Page Score.