Hiding the version number of jQuery when loading a Drupal 9,10 website

By toswebdev, 14 May, 2023
Hiding the version number of jQuery when loading a Drupal 9,10 website

To remove the version query string from all JavaScript files, you can modify the MYMODULE_js_alter() implementation to loop through all the JavaScript files in the $javascript array and set the version to NULL for each file. Here's an example implementation:


/**
* Implements hook_js_alter().
*/
function MYMODULE_js_alter(&$javascript, Drupal\Core\Asset\AttachedAssetsInterface $assets) {
 // Loop through all the JavaScript files.
 foreach ($javascript as &$file) {
   // Check if the file has a version query string.
   if (isset($file['version'])) {
     // Remove the version query string.
     $file['version'] = NULL;
   }
 }
}

In this implementation, we loop through all the JavaScript files in the $javascript array and check if each file has a version query string. If a file has a version query string, we remove it by setting the version to NULL.

Note :- After implementing this hook, clear the cache of your Drupal site for the changes to take effect. Note that disabling the version query string can affect caching behavior, so it's important to test thoroughly after making this change to ensure that your site's assets are properly cached and refreshed when necessary.

Comments

All comments go through moderation, so your comment won't display immediately.