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

Add asset priority & use AssetMaker for global backend assets #1022

Merged
merged 12 commits into from
Jul 14, 2024

Conversation

bennothommo
Copy link
Member

@bennothommo bennothommo commented Dec 21, 2023

This PR changes the global backend assets that were originally specified directly in the "head" template for the Backend, and uses the AssetMaker to load them instead. This should allow people the ability to manipulate the assets if need be through events.

To ensure that the global assets are loaded first before all others, I have also introduced a priority order value for assets. It may be specified by providing the second argument for addJs(), addCss(), etc. as an array and populating it with the following:

$this->addCss('my.css', [
    'build' => 'core',
    'order' => 50, // must be an integer
]);

The default order for all assets is 500.

CSS and JS assets can also be set to be preloaded by adding a preload value set to true. This will ensure that the appropriate preload tag is included with the asset.

@bennothommo bennothommo changed the title Use AssetMaker for global backend assets, introduce asset priority Use AssetMaker for global backend assets, introduce asset priority and preloading Dec 21, 2023
@bennothommo bennothommo marked this pull request as ready for review December 21, 2023 03:32
@LukeTowers
Copy link
Member

The "priority" property should probably be called "order" and have a default value of 500, as that is what the backend menu and permissions registration already does.

The current implementation makes these global assets a responsibility of the skin, so I'm not sure how I feel about them being included in the base controller class; thereby forcing them to be included in every single backend skin.

@bennothommo
Copy link
Member Author

@LukeTowers we can add a parameter to the Skin class that defines whether to load the global assets or not - thereby making it the responsibility of the skin to load all necessary assets?

Alternatively, the skin itself could be the class that provides the global assets, although this might take some refactoring for the base skin.

@LukeTowers
Copy link
Member

Hmm, I think I have a better idea, let's just add in those assets dynamically in the layout file itself since that's where they're used anyways. Different layouts have different global assets, so I don't really like having it be defined in the controller or the skin.

@bennothommo
Copy link
Member Author

So basically just can this PR because that's what was done originally?

@LukeTowers
Copy link
Member

The point of this PR would be to pass those assets through the AssetMaker trait (instead of manually rendering them as script / style tags) so that their attributes can be controlled through the event system (thus removing the cfasync attribute from the core as you wanted and moving it into the cloudflare specific plugin).

@LukeTowers LukeTowers modified the milestones: v1.2.4, 1.2.5 Dec 27, 2023
@mjauvin mjauvin modified the milestones: 1.2.5, 1.2.6 Feb 18, 2024
@LukeTowers
Copy link
Member

@bennothommo idea was along the lines of

$this->addAssets([...])

in the layout files instead of directly rendering the asset tags; thus forcing them to go through the AssetMaker trait and associated events. Any thoughts?

@LukeTowers LukeTowers modified the milestones: 1.2.6, 1.2.7 Apr 25, 2024
@bennothommo
Copy link
Member Author

@LukeTowers are we still going to implement ordering / prioritising with that? I can't quite recall the reason I did this PR (might've had something to do with the backend skinning or the widgets). I imagine it was to enforce some scripts / styles loading before other global scripts / styles, but it might be misleading if the assets are placed directly in the layouts or partials.

I think we'll have difficulty using the addAssets/Jss/Css methods directly in the layout too, because the assets would need to be prepared and collated before hitting the layout in order to be placed in the proper position in the layout. We'd effectively need to run two passes on the layout - first to get all the assets requested, and the second to place them in the head.

@LukeTowers
Copy link
Member

Yes, I'm fine with the ordering / prioritization changes staying. I don't think we'll have to do any extra passes on the layout because we can dynamically add them exactly where they're being added right now, which is right before the call to makeAssets(), so I don't see any reason why it wouldn't work.

The preload definition has changed somewhat since this was last looked at (see https://web.dev/articles/fetch-priority#history; ex. fetchpriority instead of priority).

Additionally, the MDN article on rel=preload (https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload#the_basics) makes a good point that adding preloads for resources immediately before actually loading them doesn't actually help ("the browser probably discovers the <link rel="stylesheet"> and <script> elements in the same chunk of HTML as the preloads"), but should instead be used for resources loaded much later on in the page lifecycle:

> the benefits can be seen much more clearly the later resources are discovered and the larger they are. For example:
> Resources that are pointed to from inside CSS, like fonts or images.
> Resources that JavaScript can request, like imported scripts.

For this reason we'll remove the initial stuff we have related to fetchpriority and preloading. If we would like to implement it in the future, I would recommend that we take more of an approach of having a separate categorization for preloads, i.e. more along the lines of addPreload(). Potentially at that point we could do things like advance scanning of the assets added by addCss / addJs to dynamically generate some useful preloads, but for now this is not required by any active projects so we'll table it for now.
Theoretically we could have some helper methods / a standard interface in the base Backend Skin class to handle this, which might also simplify the TailwindUI skin, but for now we just can't have them being defined in the controller.

Also fixed a small oversight on the type hints for addCss / addJs / addRss
@LukeTowers LukeTowers changed the title Use AssetMaker for global backend assets, introduce asset priority and preloading Use AssetMaker for global backend assets, introduce asset priority Jul 14, 2024
LukeTowers added a commit to wintercms/docs that referenced this pull request Jul 14, 2024
@LukeTowers LukeTowers changed the title Use AssetMaker for global backend assets, introduce asset priority Add asset priority & use AssetMaker for global backend assets Jul 14, 2024
@LukeTowers LukeTowers merged commit cb51ce9 into develop Jul 14, 2024
11 checks passed
@LukeTowers LukeTowers deleted the wip/global-assets branch July 14, 2024 06:54
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.

3 participants