Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By gpantikidis on
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
Hello,
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.
Hello kirtivaland, thank you
Hello kirtivaland, thank you for your response. I tried placing your code in template.php and my site does not work at all.
Replace
do you replace "MYTHEME" with your theme name?
Is this still valid answer?
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?
Hi,
Hi,
I also used this code but is not working for me .
Try this
/**
* 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);
}
Nice! Where do I place this?
Nice! Where do I place this? I'm looking to get a high score on my PageSpeed. :)
Template.php
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!
Hi,after adding the patch I
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?
solution for Drupal 8
can anyone provide the solution for D8 ?
This look promising: https:/
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
Cachebuster
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
Only in development
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
Removing query strings from CSS, JS and Image Files in Drupal 8
https://www.drupal.org/project/remove_querystring_from_static_resource
This worked for me in Drupal 8
Increased my Page Score.