[wp-trac] [WordPress Trac] #48654: Consider a solution/endpoint to lazy-load scripts and styles

WordPress Trac noreply at wordpress.org
Sun Mar 29 23:27:56 UTC 2020


#48654: Consider a solution/endpoint to lazy-load scripts and styles
-------------------------------------------------+-------------------------
 Reporter:  youknowriad                          |       Owner:  (none)
     Type:  feature request                      |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  REST API                             |     Version:  4.7
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch dev-feedback needs-unit-   |     Focuses:
  tests                                          |  javascript, rest-api
-------------------------------------------------+-------------------------

Comment (by azaozz):

 The "hard part" here is this:

 > Load these dependencies in the client without duplication.

 This is not possible to do from PHP (script-loader). The loaded scripts
 will have to be tracked in js and excluded from loading again if listed as
 dependencies. However not sure if it's a good idea to duplicate the
 script-loader functionality in js.

 Seems it would be better to use script-loader to "calculate" only the
 missing dependencies. This can be done by keeping a list of the already
 loaded scripts (handles) in js, and include it on all async requests to
 load more scripts.

 Use case: Let's assume there are 100 registered scripts in script-loader
 (PHP).
 1. The Edit Post screen is loaded (including the block editor). This loads
 50 scripts (enqueued plus dependencies). The loaded scripts "handles" are
 stored in a list in js.
 2. From the Edit Post screen the user installs a new plugin. The plugin
 registers 3 new scripts that have 5 dependencies each.
 3. In the install confirmation response the three new script handles are
 passed to the block editor.
 4. The block editor makes a request to get the URLs to load the 3 new
 scripts. With this request it also sends the list of already loaded
 scripts.
 5. In PHP script-loader now has 103 registered scripts 50 of which are
 already loaded. It "calculates" the dependencies of the 3 new scripts,
 flattens them, then removes the already loaded scripts, and returns the
 URLs to the scripts that need to be loaded (in the proper load order).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/48654#comment:12>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list