When does Yii recreate assets?

By a total accident I have figured out that my frontend and backend folder are huge (65M+). Quick inspection had proven that my app is using a set of only three assets, but for some reason Yii has generated (published) each of them three times:

As you can see, in some cases the same huge Bootstrap 5 asset has been published twice in the same date, hour and minute. So between 2-3 refreshes of the page. And between less than 60 seconds my assets folder has got extra 19 MB.

So, my question is:

  1. When or under exactly which conditions Yii 2 may decide to publish exactly the same asset (as it already have published) under new asset ID?

  2. Is there anything I can do to prevent this?

  3. Can I do something (and what) to make my both apps (backend and frontend) reusing the same assets folder (i.e. /common/web/assets?) instead of separate one for each of these apps? Is it worth extra code of it? Or maybe an answer to the next question is the problem’s solution?

  4. Can I delete extra assets at all? And, if yes, which one (all of them?) and when (some hours or days after publication)?

The last one seems most important. If I can consider some automated script that runs i.e. on daily basis and simply purges all or some folders out of assets folder?

filemtime changed.

Yes. You can customize hash function here: AssetManager, yii\web\AssetManager | API Documentation for Yii 2.0 | Yii PHP Framework

Better not to since these are two different apps.

Deleting all of them is OK. Yii will regenerate assets if these are missing.

1 Like

Care to expand this?

Nothing has change on my side (around assets):

  • No change in source code
  • No change in source files (i.e. the same out-of-the-box Bootstrap 5 deployment)
  • No change in version of any package (composer not touched at all)
  • No change in destination folder (nothing deleted, removed or renamed)

PHP guide says: “filemtime – Gets file modification time”. But what has been modified?

With the above assumptions, my assets folder should stick unmodified until the cold death of the universe, because nothing ever gets modified. And yet, I am getting Bootstrap 5 deployed after Bootstrap 5, sometimes twice per minute ← that’s the key of the problem and the key of this question.

Where should I start tracing on what changes what, to trigger the whole process? As per my knowledge nothing changes on either side.

For me this looks either as a bug or very, very complicated and confusing algorithm.

Again, I am trying to trace the roots of the problem to try to understand it. You have already provided me with the solution – some CRON task that will purge assets folder on regular basis.

But, I really would like to know, what exactly I am doing that I am causing Bootstrap 5 to be published to assets folder twice per minute and then there’s a “silence” of six days.

No idea but ls -l assets dir and subdirs may be of some help.

This is the code in question. Only filemtime() there is a variable. That’s why my assumption.

1 Like

OK, seems valid. I don’t think that we can then figure out what’s wrong. Thanks.

You can supply your own hash function that has no variables and doesn’t depend on filemtime.

1 Like