yii2: Assets not updating
I am experiencing a very odd issue regarding publishing assets (.js, .css). For some reason, Yii has stopped publishing updated .js files to the web/assets directory. I have to manually remove everything from web/assets for the updated version to show up. The reason this issue is odd is because the asset manager was working perfectly until last couple of days.
On my system I can see that my .js file was modified on a certain time, and the version of the same file in the assets directory has an earlier modification time; so I don’t think this is a browser caching issue. It’s an issue that updated assets are not being published.
I don’t have a clue where to start to finding out what is causing this. Does anyone have any idea on how to fix this?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 6
- Comments: 36 (27 by maintainers)
I believe reason is the default hash function of AssetManager, It looks like below:
The value of $path is $sourcePath of an AssetBundle. When $sourcePath is a directory who has sub directory ,after some files are modified in the sub directory, the last modified time of the sub directory is changed ,but the directory 's last modified time is not change. The hash function only contain the directory’s last modified time, so publish js skipped.
I tried this for local development with good results:
I think this is definitely a problem. I’m having the same issue with another Yii2 project. Is nobody else having this issue?
@rubenjleon I did exactly the same, but problem remained. I learned there is another solution:
forceCopy forces files to be published in assets folder each time.
Same issue here.
My Assets bundle:
As you can see I tried to add the
publishOptionto force copy assets, which works but slows down the website about 10 times.It seems its not picking up the change only when you use
sourcePathAre you testing this by calling
getPublishedUrl()in a single loop? Do you callclearstatcache()after each cycle?We are able to reliably reproduce this whenever a nested file has a modification date that is greater than the parent directory – especially easy to reproduce when the file is in a subdirectory of the asset bundle’s root path.
For Craft CMS, we’ve fixed this by overriding
AssetManager::hash(), replacing thefilemtime()call with a call to ourcraft\helpers\FileHelper::lastModifiedTime()method, which recursively loops through all sub-files/directories and returns the true latest modified time.Performance impact is negligible. For an asset bundle with 10 nested directories and 128 nested files, I’m seeing about an 0.2-second performance hit on average when calling
getPublishedUrl()1,000,000 times.@cebe @samdark @qiangxue Let me know if you’d like me to submit these changes in a PR.
Yes, the issue is there: http://stackoverflow.com/questions/3620684/directory-last-modified-date
same here. adding this to my config file made it work :