-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Update docs for conditional loading assets using render_callback #21838
Comments
@mtias did we explore how to mitigate this issue in the past? Can you think about any drawbacks of the proposed workaround? @aduth do you think we could optimize it on the |
I'm inclined to think it should be feasible to wait to enqueue scripts associated with a block's type at the point that the block is rendered. A few notes:
|
One point to consider would be infinite-scroll type of sites, where a block might not be present on the initial page load but in a subsequent fetch. |
Do they work with block scripts as it exists today? Maybe the current behavior of loading all scripts and styles for all blocks can be left intact for the blogroll page. I think it would be less of a problem to optimize this on screens which aren't expecting to load additional blocks after load, or where no blocks would ever be rendered anyways (e.g. single template). I think it's also worth pointing out that even if popular, this isn't a core behavior. Which is to say: I'd hate for it to impede any progress here, because the current behavior is pretty untenable. |
Yes, I agree 100%. If anything, it should be possible to disable this behaviour and any plugin implementing infinite-scroll like behaviours needs to handle it. |
💯
We also don't enqueue any JS code on the front so that's another reason why we didn't work on it so far. It could be still beneficial to apply this technique to styles though.
It looks like |
Two patterns I've seen: |
Because of that, plugins that have separate back end and front end builds may want to check |
A pull request exploring to make this the default behavior: #22754 |
I just saw this issue and it's great to see. However, I have a concern about FOUC. Has it been considered to not just enqueue the scripts and styles but rather print them instead? This will reduce the risk of a flash of unstyled content, as otherwise enqueued assets will get printed at I took this approach in the Syntax-highlighting Code Block plugin. It's definitely not as clean as just deferring to enqueue in #22754, but it did address FOUC. |
See also studiopress/atomic-blocks#294 & studiopress/atomic-blocks#297 which do this for Atomic Blocks, to conditionally print the Font Awesome stylesheet before the first dependent block in the page. |
There is currently a bug in core with loading CSS assets in the footer. See #30579. I think that this would need to be fixed before this goes into core. |
This is actually not a bug (anymore). See the comment I just posted on that issue. |
It interesting approach. What about all dependencies that scripts define, will they be printed at the same time when rendering script tags for the block? Could it create any issues this way? I'd be curious to explore it regardless. I don't know if it can have some impact on the front-end code. |
Yes, any dependencies will get printed as well (if they haven't been printed already). Once the assets and their dependencies are printed, then any subsequent blocks will print nothing (since the assets are then marked as |
I'm exploring the approach @westonruter suggested as a direct change in WordPress core. I made some progress but there is one edge case as noted in https://core.trac.wordpress.org/ticket/50328#comment:3. |
That edge case is actually accounted for (mostly) in the the second Atomic Blocks PR I linked to above: studiopress/atomic-blocks#297 And you've come up with the same solution! |
It looks like #22754 doesn’t work in some cases as expected and it needs to be reverted. I still don’t quite know why this technique might not wotk for CSS on the front-end. Maybe CSS is printed only once in the head for some themes? |
Issue: Installing numerous blocks that require client assets (JSS and/or CSS) on the front-end results in many files being loaded regardless of the block being used on the page or not.
Ideally, a block's assets only load on a page that uses that block. The ticket #5445 is an outstanding issue to automatically handling loading of assets, but it is a complicated issue without a clear solution in sight.
In the interim, it is possible for each plugin to use the
render_callback
function when registering their block to enqueue assets only if the block is used. I wrote a blog post explaining the issue, as well as a test block that illustrates it in use.I raised this in the Core Editor slack meeting but did not receive much feedback there, so raising as an issue for additional comment.
Question: Should we update our documentation stating placing asset enqueuing in the
render_callback
function is a best practice for conditionally loading assets?The text was updated successfully, but these errors were encountered: