Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

canEnqueue for 3rd party registered assets? #19

Open
lkraav opened this issue Mar 10, 2021 · 7 comments
Open

canEnqueue for 3rd party registered assets? #19

lkraav opened this issue Mar 10, 2021 · 7 comments
Labels
question Further information is requested

Comments

@lkraav
Copy link

lkraav commented Mar 10, 2021

Is your feature request related to a problem? Please describe.
There are assets that need to be only conditionally loaded in specific contexts, such as payment gateway scripts not necessary anywhere outside of WooCommerce is_checkout()

Describe the solution you'd like
New OutputFilter could accept an array of callbacks, or a boolean result to determine whether the asset should be enqueued at all.

This should also work for controlling non-Inpsyde\Asset dependencies. We should be able to initialize any externally enqueued handle to an Inpsyde\Asset object, then apply any OutputFilters as usual.

Describe alternatives you've considered
Not sure, are there any?

Additional context
I'm looking to better control my own and various 3rd party assets enqueued to only specific contexts, to optimized asset payloads for increased page load speed.

@lkraav
Copy link
Author

lkraav commented Mar 10, 2021

Can the team get back to me on whether you think this is a suitable addition to this package's functionality? I think I could file a PR then if it seems useful.

cc @gmazzap

@Chrico
Copy link
Member

Chrico commented Mar 10, 2021

Howdy,

you can already enqueue Assets conditionally like following:

<?php
$script = new Script('foo', 'foo.js');
$script->canEnqueue('is_checkout');
// or
$script->canEnqueue(function(): bool {
    return function_exists('is_checkout') && is_checkout();
});

Same goes for Style. This method allows you to define a callback which returns either true (do enqueue) or false (only register). :)

@Chrico Chrico added the question Further information is requested label Mar 10, 2021
@lkraav
Copy link
Author

lkraav commented Mar 10, 2021

you can already enqueue Assets conditionally like following:

Excellent! Is it me, or is this missing from docs https://github.com/inpsyde/assets/search?q=canEnqueue

I posted this issue after reading docs, but not source.

Remaining question: controlling 3rd party assets - can I take control of them with just $script = new Script('registered-handle')?

@lkraav lkraav changed the title New OutputFilter: Gatekeeper / ContextManager canEnqueue for 3rd party registered assets? Mar 10, 2021
@lkraav
Copy link
Author

lkraav commented Mar 10, 2021

After some source digging, it seems like https://github.com/inpsyde/assets/blob/master/src/BaseAsset.php could be extended to something like class 3rdPartyAsset, where constructor requires only registered handle, and fills object data from existing WP asset object global database.

@Chrico
Copy link
Member

Chrico commented Mar 11, 2021

Ah now i unterstand. Currently we do not connect to anything which is "outside" of the AssetManager. It's probably hard to keep that synced, because you can at nearly every point register/enqueue a new Script/Style.

@lkraav
Copy link
Author

lkraav commented Mar 11, 2021

It's probably hard to keep that synced, because you can at nearly every point register/enqueue a new Script/Style.

True, but I'll explore some and see if I can come up with something sensible - as I could use a systemic solution to this anyway, and inpsyde/assets seems to be the highest quality asset mgmt solution currently available in the ecosystem.

@lkraav
Copy link
Author

lkraav commented Mar 12, 2021

Currently we do not connect to anything which is "outside" of the AssetManager. It's probably hard to keep that synced, because you can at nearly every point register/enqueue a new Script/Style.

We would only take responsibility for things registered by the time of overtake operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants