How to make Symfony 2 asset compilation to product different filenames?
Asked Answered
P

3

15

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?

Passmore answered 18/1, 2012 at 13:39 Comment(0)
B
8

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

Barriebarrientos answered 18/1, 2012 at 14:1 Comment(3)
It does not seem to have any effect. I have cleared the cache and ran assetic:dump --env=prod and tried against the prod environment and I do not see any ?v0.0.1 there.Passmore
Leave out the output file name doesn't work, always generated the same name like "8662e4b.js". Asset version is work good.Jentoft
Ruuning php app/console cache:clear --env=prod before dumping the assets, as @faost mentionned, worksKishke
F
3

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.

Frye answered 4/4, 2012 at 7:34 Comment(7)
I tried placing an asterisk, but it seems as it gets removed and not replace by a random string. Is this part of Assetic or did you do something on your own?Passmore
I haven't done anything on my own, and my code is exactly as shown above except for the name of my bundle. Is it possible that something is being lost in compiling your twig template to php? I can't imagine why that would be the case, but nothing else occurs to me that would explain why the asterisk is being treated differently in my system than in yours.Frye
Might be a different version. What's yours Assetic version?Passmore
Sorry for the late reply - I've got Assetic v1.0.3, and AsseticBundle v1.0.1.Frye
An * (asterisk) in an asset output filename is substituted by a non-random string (and should not be used for cache-busting). See Assetic\Factory\AssetFactory::generateAssetName(). It is deterministic, based on the included static files, filters, and options (such as the specified filename for the generated file). @AmericanUmlaut, you should update your answer.Shaduf
Adam, thank you for your feedback. You're right, I was misusing the word "random", and I've updated my answer to be more precise.Frye
Right on. See also: groups.google.com/d/topic/symfony-devs/gjDxXa4I1dI/discussionShaduf
D
1

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
Diarrhea answered 21/12, 2014 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.