How to add version of assets in css and js?

I want assetic to output compressed js and css something like this: v2.3.1/css/whatever.css

I am currently dumping my css and js for production: $ php app/console assetic:dump --env=prod --no-debug . But they are reset in css / and js / without version.

I read this one , but it seems to only apply to images, not CSS / js.

An important reason for this is a broken or invalidated cache.

+2
source share
3 answers

Yes, a known problem ... In our production process, we ended up with such a block in the bin/vendors script:

 if (in_array('--env=dev', $argv)) { system(sprintf('%s %s assets:install --symlink %s', $interpreter, escapeshellarg($rootDir . '/app/console'), escapeshellarg($rootDir . '/web/'))); system(sprintf('%s %s assetic:dump --env=dev', $interpreter, escapeshellarg($rootDir . '/app/console'))); system(sprintf('%s %s myVendor:assets:install --symlink ', $interpreter, escapeshellarg($rootDir . '/app/console'))); } else { system(sprintf('%s %s assets:install %s', $interpreter, escapeshellarg($rootDir . '/app/console'), escapeshellarg($rootDir . '/web/'))); system(sprintf('%s %s assetic:dump --env=prod --no-debug', $interpreter, escapeshellarg($rootDir . '/app/console'))); system(sprintf('%s %s myVendor:assets:install ', $interpreter, escapeshellarg($rootDir . '/app/console'))); } 

As you can see, we have defined our console command, which installs assets into a web folder after installing and resetting Symfony assets. In the MyVendorCommand script, we do something like this:

 $version = $this->getContainer()->getParameter('your_version_parameter'); $assetsInstallCommand = $this->getApplication()->find('assets:install'); $commandOptions = $input->getOptions(); $assetsInstallArguments = array( 'command' => 'assets:install', 'target' => 'web/version-' . $version, '--symlink' => $commandOptions['symlink'] ); $assetsInstallInput = new ArrayInput($assetsInstallArguments); $returnCode = $assetsInstallCommand->run($assetsInstallInput, $output); 
+3
source

Ho, this is a big Symfony2 mistake! I'm not sure anyone reported this!

My solution was to add an alias to the Nginx configuration, but that is your question. cleaner and better.

+1
source

My workaround is to make a rewriteRule in the .htaccess file to serve the real file, but accepting the full URL with the version number. Something like that...

application /Config/config.yml

 [...] engines: ['twig'] assets_version: 20140523 # numeric version assets_version_format: "assets-%%2$s/%%1$s" [...] 

Web / .htaccess

 [...] <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^assets-([0-9]*)/(.*)$ ./$2 [L] [...] 
0
source

All Articles