WebDevStudios / wds-bt

A starter block theme from WebDevStudios.
https://wdsbt.wdslab.com/
GNU General Public License v3.0
31 stars 4 forks source link

Theme stylesheet is being enqueued with the wrong asset version #37

Open Cheffheid opened 1 week ago

Cheffheid commented 1 week ago

This was a fun one to try and figure out.

Currently, we're enqueueing both the theme script file and the theme stylesheet with the same version from index.asset.php if that exists.

However, this means that if the JavaScript isn't updated but the CSS is then the version that lives inside of index.asset.php doesn't update as a result of the build. Which, in turn, means that the stylesheet enqueue doesn't properly update the version to bust CDN caches and will continue to load older cached versions of that stylesheet if those exist.

This is confirmed by the rsync action inside of Buddy with the files it considers "updated" and uploads:

image

And so I would propose splitting the current scripts() function inside of inc/setup/scripts.php and grab the version from the appropriate asset.php file (/build/js/index.asset.php for the script, /build/js/css/style.asset.php for the stylesheet) when enqueueing.

Alternatively, we can use filmtime() or something of that nature instead or find a way to get it to update the version in index.asset.php if only the CSS is updated, since the theme stylesheet likely won't have any dependencies in its asset file ever so including that file just to get the version might be a little overkill.

itsamoreh commented 1 week ago

Prior to #33, this code was not working as expected, and the version would always be 0.1.0 regardless of whether or not changes were made to js or css assets: https://github.com/WebDevStudios/wds-bt/blob/708fb6f89b30c7c4c9ff243c0f2ba3cef2ea0b04/inc/setup/scripts.php#L18-L25

After #33 was merged, there are still problems. I did a fresh clone of wds-bt's main branch.

After building assets, and checking the network tab, this is what I see for index.js and style.css: 2024-09-11-18 00 20 2024-09-11-18 12 46

This matches the version in /build/js/index.asset.php: 2024-09-11-18 02 40

So things look good. However, after changing some JS and rebuilding, the version in index.asset.php does not change. Even after I delete the build folder and do a rebuild, the version is still the same.