如何编程移除静态资源的Query String?Drupal模块无效求助
Hey there! I totally get it when modules don't live up to expectations—let's walk through how to programmatically strip query strings from static resources in Drupal. I've got a couple of solid approaches depending on your Drupal version:
For Drupal 9/10
Approach 1: Use Preprocess Hooks to Modify Attachments
You can tap into hook_preprocess_html() or hook_preprocess_page() to directly edit the CSS/JS assets attached to your pages. This is straightforward and great for targeted changes.
Add this code to your custom module's .module file:
/** * Implements hook_preprocess_html(). */ function YOUR_MODULE_preprocess_html(&$variables) { // Clear query strings from standalone CSS attachments if (!empty($variables['page']['#attached']['css'])) { foreach ($variables['page']['#attached']['css'] as &$css) { if (isset($css['query'])) { $css['query'] = []; } } } // Clear query strings from standalone JS attachments if (!empty($variables['page']['#attached']['js'])) { foreach ($variables['page']['#attached']['js'] as &$js) { if (isset($js['query'])) { $js['query'] = []; } } } }
Approach 2: Decorate the Asset Resolver Service (Global Solution)
If you want to strip query strings from all assets (including those loaded via libraries), a service decorator is the way to go—it intercepts the asset resolution process globally.
- First, define the decorator in your module's
services.yml:
services: custom_asset.asset_resolver_decorator: class: Drupal\custom_asset\Asset\AssetResolverDecorator decorates: asset.resolver arguments: ['@custom_asset.asset_resolver_decorator.inner']
- Create the decorator class in
src/Asset/AssetResolverDecorator.php:
<?php namespace Drupal\custom_asset\Asset; use Drupal\Core\Asset\AssetResolverInterface; use Drupal\Core\Language\LanguageInterface; class AssetResolverDecorator implements AssetResolverInterface { protected $innerService; public function __construct(AssetResolverInterface $inner_service) { $this->innerService = $inner_service; } public function getAssets($type, array $libraries, LanguageInterface $language) { $assets = $this->innerService->getAssets($type, $libraries, $language); $this->clearQueryStrings($assets); return $assets; } public function getJsAssetsOptimized(array $js_assets, $optimize) { $assets = $this->innerService->getJsAssetsOptimized($js_assets, $optimize); $this->clearQueryStrings($assets); return $assets; } public function getCssAssetsOptimized(array $css_assets, $optimize) { $assets = $this->innerService->getCssAssetsOptimized($css_assets, $optimize); $this->clearQueryStrings($assets); return $assets; } /** * Helper method to clear query strings from assets. */ private function clearQueryStrings(array &$assets) { foreach ($assets as &$asset) { if (isset($asset['query'])) { $asset['query'] = []; } } } // Delegate any other interface methods to the inner service if needed }
For Drupal 7
If you're still on Drupal 7, use the dedicated alter hooks for CSS and JS:
Add this to your custom module's .module file:
/** * Implements hook_css_alter(). */ function YOUR_MODULE_css_alter(&$css) { foreach ($css as &$file) { $file['query_string'] = ''; } } /** * Implements hook_js_alter(). */ function YOUR_MODULE_js_alter(&$js) { foreach ($js as &$file) { $file['query_string'] = ''; } }
Important Notes
- Replace
YOUR_MODULEorcustom_assetwith your actual custom module name. - Always clear Drupal's cache after adding this code—otherwise, the changes won't take effect.
- Keep in mind: Query strings on static assets often help browsers cache bust when files are updated. If you remove them, make sure you have another strategy (like file renaming with hashes) to ensure users get fresh assets when you update them.
- Test by viewing your page source to confirm the query strings are gone from CSS/JS URLs.
内容的提问来源于stack exchange,提问作者chuan wu

