1

I've tried it with https://www.drupal.org/project/remove_querystring_from_static_resource

But it doesn't work well for me .

How can I achieve that programmatically?

The following is the test result: enter image description here

0

2 Answers 2

1

This trouble is usually encountered when static resources (eg. images, css & javascript files) are accessed using a query string.

Eg: http://example.com/image.png?something=test

Those query strings are used for avoiding browser caching. Their values are changed, so the browser should do a new request instead of getting cached resource.

You should remove those query strings (?something=test in my example) and use some suitable Cache-Control headers.

Edit: Try this code.

Replace THEMENAME with your theme name.

/**
 * 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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I inserted "<meta http-equiv = 'cache-control' content = 'no-cache'>" to header but query strings are still existing . which directive should I use?
That line doesn't help you. Remove the highlighted parts: prntscr.com/i90msq
Yeah.but how to remove the highlighted parts that's what exactly I want to know. I've tried to remove it by js. but it's useless
Removing them using JS does not make sense. Resources are already loaded. See my edited answer.
I used that code before using js code .it doesn't work ....., i think i'm going to give up.but thank you very much .
1

Finally I solved the issue with this code:

    use Drupal\Core\Asset\AttachedAssetsInterface;

    /**
    * Implements hook_css_alter().
    */

    function bootstrap_css_alter(&$css, AttachedAssetsInterface $assets){
        foreach ($css as &$file) {
            if ($file['type'] != 'external') {
            $file['type'] = 'external';
            $file['data'] = '/' . $file['data'];
            }
        }

     }


     /**
     * Implements hook_js_alter().
     */

    function bootstrap_js_alter(&$javascript, AttachedAssetsInterface $assets){

        foreach ($javascript as &$file) {
            if ($file['type'] != 'external') {
            $file['type'] = 'external';
            $file['data'] = '/' . $file['data'];
            }
        }
    }

Put this code to your yourthemename.theme file.
it works perfect on drupal 8
Hope this help you guys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.