How to make Symfony 2 asset compilation to product different filenames?

℡╲_俬逩灬. 提交于 2019-12-10 01:30:02

问题


I followed the guide on the very bottom of this article: http://symfony.com/doc/current/cookbook/assetic/asset_management.html

I have this code:

    {% javascripts
        ...

        output='js/dist/dist.js'
    %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}

Now if I run sf assetic:dump --env=prod it creates the compiled file properly. However, I would like to have it generating a random name (or timestamped) so that the client-side browser cache problem is avoided. Right now it always creates dist.js file which gets cached and when I update my code the users won't see the difference (or get errors).

Is there a way to make it like dist12345678.js?


回答1:


You have two options here: Either leave out the output file name (it will then be an autogenerated hash that changes) or use asset versions, as described in the Symfony docs: http://symfony.com/doc/current/reference/configuration/framework.html#ref-framework-assets-version




回答2:


I have been doing a lot of experimenting with Assetic for a project, and I stumbled across a way to do exactly what you're asking. If the "output" string contains "*", a derministically generated arbitrary string is substituted for it.

For example, I have the following in a template (PHP, we aren't using twig):

<?php foreach($view['assetic']->javascripts(array('@MyBundle/Resources/public/js/page.js'), array(), array('output' => 'js/compiled/page_*.js')) as $url): ?>
  <script type="text/javascript" src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>

<?php foreach($view['assetic']->stylesheets(array('@MyBundle/Resources/public/css/page.css'), array(), array('output' => 'css/compiled/page_*.css')) as $url): ?>
  <link rel="stylesheet" href="<?php echo $view->escape($url) ?>" />
<?php endforeach; ?>

When I run app/console assetic:dump, it generates /web/css/compiled/page_8e8fcb3.css and /web/js/compiled/page_241b4e5.js.




回答3:


Better approach is https://github.com/symfony/AsseticBundle/pull/119#issuecomment-28877145

You'll get :

app/console assetic:dump --env=prod --no-debug
Dumping all prod assets.
Debug mode is off.

[file+] /web/assets/static-fe1927d.css
[file+] /web/assets/static-6e92057.js


来源:https://stackoverflow.com/questions/8911072/how-to-make-symfony-2-asset-compilation-to-product-different-filenames

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!