Skip to content

Conversation

@HLeithner
Copy link
Member

Summary of Changes

Add the magic value "nocache" similar to "auto" to the joomla.asset.json renderer for scripts and styles.
This allows to add a js and css file already to the asset file while developing without struggling with all the possible cache layers.
Additional, it's possible that you need a cache buster for resources which are not under your control and changes frequently or are dynamically generated with a broken cache header.

Testing Instructions

Modify the version attribute in your joomla.asset.json to "nocache".

Actual result BEFORE applying this Pull Request

  • request url: media/com_xyz/js/your.js?nocache

Expected result AFTER applying this Pull Request

  • request url: media/com_xyz/js/your.js?nocache-<RANDOM NUMBER BETWEEN 0 AND 1.000.000>

Link to documentations

Please select:

  • Documentation link for docs.joomla.org:

  • No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org: Will be added when excepted

  • No documentation changes for manual.joomla.org needed

@laoneo
Copy link
Member

laoneo commented Oct 30, 2025

Just wondering if this is not already solved by passing a random string as version option?

@HLeithner
Copy link
Member Author

Just wondering if this is not already solved by passing a random string as version option?

That's not possible in a json file.

@laoneo
Copy link
Member

laoneo commented Oct 30, 2025

Sure, but I would then probably use the existing version attribute and support there a "nocache" value instead of adding a new flag. But this is just personal preference.

@Fedik
Copy link
Member

Fedik commented Oct 30, 2025

We already have this feature. Enable Debug and you will get version refresh for every auto item.
I do not think we need another magic string here.

It is also possible just call $doc->setMediaVersion(time()) in the layout (will work for every auto item without enabling debug).
Or we could fix $wa->getAsset(....)->setOption('version', time()).

@HLeithner
Copy link
Member Author

Sure, but I would then probably use the existing version attribute and support there a "nocache" value instead of adding a new flag. But this is just personal preference.

I'm not sure to understand you? I set the existing "version" attribute to "nocache" similar to "auto" and not adda new attribute?

HLeithner and others added 2 commits October 30, 2025 10:35
Co-authored-by: Richard Fath <richard67@users.noreply.github.com>
Co-authored-by: Richard Fath <richard67@users.noreply.github.com>
@HLeithner
Copy link
Member Author

We already have this feature. Enable Debug and you will get version refresh for every auto item. I do not think we need another magic string here.

It is also possible just call $doc->setMediaVersion(time()) in the layout (will work for every auto item without enabling debug). Or we could fix $wa->getAsset(....)->setOption('version', time()).

I want to do something different, I don't want to set it manually on each layout I'm using it and and want to have this persistent if I need to by path cache for what ever reason. (3rd party services for example).

setMediaVersion() would effect all script and styles which has a negative performance impact, setting it to the option would mean I have to add this to each usage which is error prone.

@Fedik
Copy link
Member

Fedik commented Oct 30, 2025

Hm, I not very understand your use case.
The auto is doing the same (when debug On), that probably why we have it.
This makes nocache "duplicate feature" basically (to me).

If you want to have some "custom generated" version only for specific asset and do not want to use setOption() in each layout then can also create a custom class:

{
    "name": "myasset",
    "type": "script",
    "class": "MyAssetItem",
    "namespace": "Foo\\Bar"
    "uri": "....myasset.js"
},
class MyAssetItem extends WebAssetItem 
{
  public function __construct(
        string $name,
        ?string $uri = null,
        array $options = [],
        array $attributes = [],
        array $dependencies = []
  ) {
      parent::__construct($name, $uri, $options, $attributes, $dependencies);

      $this->version = ComponentHelper::getParams('com_mycomponent')->get('myMediaVersion', '');
   }
}

Similar to core

{
"name": "core",
"type": "script",
"class": "CoreAssetItem",
"uri": "system/core.min.js"
},

@Fedik
Copy link
Member

Fedik commented Oct 30, 2025

I am not against it, if people want it then we can add.
I just don't see a reason for this.

@HLeithner
Copy link
Member Author

Hm, I not very understand your use case. The auto is doing the same (when debug On), that probably why we have it. This makes nocache "duplicate feature" basically (to me).

I want to make developer life easier, I don't want to create a new WebAsset if I just don't want no caching for exactly one script for a temporary time.
I want to make developer life easier, I don't want to create a new WebAsset if I need a cache buster for a (remote) resource I include.

Of course we can add an additional webassetitem in core that does this, but that sounds a bit overengineered compared to the the 2 lines of code change.

Co-authored-by: Richard Fath <richard67@users.noreply.github.com>
@Fedik
Copy link
Member

Fedik commented Oct 30, 2025

When I need it I usually just enable debug and disable debug plugin to hide the debug bar.

With nocache you getting a risk to forget to remove it, and push it to production.

@bembelimen
Copy link
Contributor

I have tested this item ✅ successfully on 53614f1

I see this a sensible addition for developers. It saves me the time to update on every JS/CSS change the version attribute and especially when I have to check something on a live site, I don't want to enable debug mode at all. I appreciate, there is a risk of forgetting the attribute, but same could happen with debug mode etc.

So I'm in favour of this change.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/46379.

@brianteeman
Copy link
Contributor

obviously this needs documentation please

@HLeithner
Copy link
Member Author

Of course, we will see if I can do it or someone else needs to create it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants