Skip to content

Conversation

@nlemoine
Copy link
Contributor

@nlemoine nlemoine commented Nov 29, 2025

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes/features)
  • Docs have been added/updated (for bug fixes/features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Feature

What is the current behavior? (You can also link to an open issue here)

Preambule

This feature is not to be confused with:

  • dependency management: it's not a matter of an asset that depend of another
  • browser asset processing order: I'm not referring to async, defer, etc.

This must be considered all asset location/attributes equal.

Problem

WordPress provides no straightforward (besides hook priority) way to control asset output order. Further more, this library abstract enqueue related actions where hook priority isn't reachable.

When multiple scripts or styles are enqueued, their order in the HTML depends on registration order, which can be hard to manage when scattered across plugins, themes, etc.

Example:

// Plugin A
add_action(AssetManager::ACTION_SETUP, function(AssetManager $assetManager) {
    $assetManager->register($someScript);
});

// Plugin B 
add_action(AssetManager::ACTION_SETUP, function(AssetManager $assetManager) {
    $assetManager->register($someOtherScript);
});

// Theme
add_action(AssetManager::ACTION_SETUP, function(AssetManager $assetManager) {
    $assetManager->register($themeScript);
});

Although adding different priorities to AssetManager::ACTION_SETUP will work, it's not very convenient and will eventually fail or become unmaintainable when registering multiple assets with different priorities within the same hook.

Having priority defined per asset feels way more natural and maintainable approach.

What is the new behavior (if this is a feature change)?

This PR adds a priority property to assets (lower = earlier, WP style). The AssetCollection now sorts assets by priority before returning them, ensuring predictable output order regardless of registration timing.

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

No. I created a PrioritizedAsset interface to avoid changing the Asset interface. So this feature is completely backward compatible.

Other information

You can take a look at Capo.js for ideal head order as a reason for this feature.

WordPress provides no straightforward way to control asset output order. Further more, this library abstract enqueue related actions where priority isn't reachable.

When multiple scripts or styles are enqueued, their order in the HTML depends on registration order, which is hard to manage when scattered accross plugins, themes, etc.

This adds a priority property to assets (lower = earlier, WP style). The `AssetCollection` now sorts assets by priority before returning them, ensuring predictable output order regardless of registration timing.
@widoz
Copy link
Member

widoz commented Dec 20, 2025

Hi @nlemoine

I get your concerns and the implementation is not so complex in terms of maintanance but I do have some problems to the fact that the Priority implementation reach the BaseAssets in relation to the new ScriptModule.

Another concerns is the false assumption that scripts would load in that particular order due to the async and defer attributes, but also, some scripts requires other scripts and the dependencies will change place in the head. We also have to consider the footer case imo.

The order in case of elements of the same priority isn't guarantee in php version lower than 8.0.0.

In particular, related to the ScriptModule they cannot have a priority because they might not rendered like normal scripts as dependencies are moved into the importmap.

I'm not against the changes but I think make sense to document the fact that the order cannot be guaranteed due to the aforementioned characteristics of browsers, php and WordPress.

Other than that, if the changes are fine for @Chrico are also fine for me.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants