-
Notifications
You must be signed in to change notification settings - Fork 51
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
Enhance script loading behaviour #109
Comments
@dainemawer I really like this idea. One thing that I will be very interested in is whether we can eventually make this work with assets that get registered via With WordPress moving more and more towards a block first approach more of the scripts and stylesheets will get automatically enqueued by WordPress through the These registrations in {
"name": "example/block",
"viewScript": {
"src": "file:./view.js",
"handle": "example-block-view-script",
"strategy": "lazyOnLoad"
}
} Just wanted to call this out as more and more of the registration of scripts is happening automatically abstracted away via the blocks. |
@dainemawer, I think this idea is excellent, and I love the thought of being able to link block scripts in with these loading strategies @fabiankaegy! I have a couple of thoughts. Rather than replicating the way core enqueues files: tenup_add_script( $handle = 'component', $filename = 'file-name', $ver = '1.0.0', $strategy = 'lazyOnLoad' ); I think we could future-proof it and have all the args after the handle and filename passed as an array. This would make it easy to extend in the future without us needing to keep adding additional arguments to the function. Something like: tenup_add_script(
$handle = 'component',
$filename = 'file-name',
$args = array(
version => '1.0.0',
strategy => 'lazyOnLoad',
)
); We'd also need to consider how this works with dependencies and whether we'd bypass them altogether. One of the powerful things we utilise in the scaffold currently is the For example, if we defer a script using the I'm super excited about this; I want to make sure we think through those edge cases as they'll surely come up in a build. |
While we are taking a look at the asset handling here I also want to call out this core API that is being used in blocks to make it possible to inline small assets: https://make.wordpress.org/core/2021/07/01/block-styles-loading-enhancements-in-wordpress-5-8/#inlining-small-assets By providing a May be something that is worth considering for more than just blocks. |
This is great feedback so far, thanks @fabiankaegy and @darylldoyle - I totally agree with both of your points!
@darylldoyle I like the idea of using an tenup_add_script(
$handle = 'component',
$filename = 'file-name',
$args = array(
version => '1.0.0',
strategy => 'lazyOnLoad',
preconnect => true
)
); That would also be a winner. What are our next steps here? |
I think object oriented programing is alien to the WordPress ecosystem. use TenUp\Utility\Script;
(new Script('url-of-javascript-file'))
->setHandle('component')
->setVersion('1.0.0')
->enableLazyLoading()
->enablePreconnect() |
Is your enhancement related to a problem? Please describe.
Script loading in WordPress really sucks. We have a few options available to us:
<head>
</body>
script_loader_tag
to appendasync
anddefer
onto scripts.None of the options above, considering the WP ecosystem really has a huge effect on performance.
I would like to suggest that we build our own function which sits on top of
wp_enqueue_script
, very much like NextJS handles theirnext/script
component: https://nextjs.org/docs/api-reference/next/scriptIn NextJS scripts can apply "strategies":
(from web.dev)
The strategy attribute can take three values.
beforeInteractive
: This option may be used for critical scripts that should execute before the page becomes interactive. Next.js ensures that such scripts are injected into the initial HTML on the server and executed before other self-bundled JavaScript. Consent management, bot detection scripts, or helper libraries required to render critical content are good candidates for this strategy.afterInteractive
: This is the default strategy applied and is equivalent to loading a script with the defer attribute. It should be used for scripts that the browser can run after the page is interactive—for example, analytics scripts. Next.js injects these scripts on the client-side, and they run after the page is hydrated. Thus, unless otherwise specified, all third-party scripts defined using the Script component are deferred by Next.js, thereby providing a strong default.lazyOnload
: This option may be used to lazy-load low-priority scripts when the browser is idle. The functionality provided by such scripts is not required immediately after the page becomes interactive—for example, chat or social media plug-ins.As WP is not prerendered,
beforeInteractive
is null and void.We can however leverage
afterInteractive
andlazyOnLoad
as described above,afterInteractive
is effectively the equivalent of usingdefer
. What I would like to see is an option forlazyOnLoad
which loads scripts when the browsers main thread is idle usingrequestIdleCallback
- MDN. For browsers that do no supportrequestIdleCallback
there is a shim available.There is currently limited support in Safari for this functionality, but otherwise great support across the web.
Id envision a function like:
I think a proof of concept could be worthwhile testing out to see how far we could get but would love to see something in action!
Designs
No response
Describe alternatives you've considered
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: