[wp-trac] [WordPress Trac] #36995: Support for Service Workers

WordPress Trac noreply at wordpress.org
Wed Jul 11 04:09:10 UTC 2018


#36995: Support for Service Workers
-------------------------+-------------------------------------------------
 Reporter:  bhubbard     |       Owner:  westonruter
     Type:  feature      |      Status:  accepted
  request                |
 Priority:  normal       |   Milestone:  Future Release
Component:  General      |     Version:  trunk
 Severity:  normal       |  Resolution:
 Keywords:  needs-patch  |     Focuses:  javascript, administration,
                         |  performance
-------------------------+-------------------------------------------------

Comment (by westonruter):

 That [https://github.com/xwp/pwa-wp/pull/27 PR] has been merged. The three
 different scopes are indicated by constants (integers).

 Example usage:

 {{{#!php
 <?php
 // Register script to only run on the frontend, with dependency on some
 other app-shell SW script.
 wp_register_service_worker(
     'foo', // Handle.
     plugin_dir_url( __FILE__ ) . 'foo.js', // Source.
     array( 'app-shell' ), // Dependency.
     WP_Service_Workers::SCOPE_FRONT // Scope.
 );

 // Register script (here via render callback instead of URL) to only run
 only in the admin.
 wp_register_service_worker(
     'bar',
     function() { return 'console.info("Hello admin!")'; },
     array(), // No deps.
     WP_Service_Workers::SCOPE_ADMIN
 );

 // Register script with to run in both the frontend and wp-admin.
 wp_register_service_worker( 'baz', plugin_dir_url( __FILE__ ) . 'baz.js',
 array(), WP_Service_Workers::SCOPE_ALL );

 // The default values for $deps and $scope are array() and
 WP_Service_Workers::SCOPE_ALL  respectively, so this is same as previous.
 wp_register_service_worker( 'baz', plugin_dir_url( __FILE__ ) . 'baz.js'
 );
 }}}

 I think the next thing to do is to look at higher-level abstractions than
 this low-level service worker API. While I think we'll always need to have
 this ability to write scripts directly into the service worker, in
 practice I think we should target a PHP API that abstracts away the most
 common code that gets included in a service worker, namely the various
 caching strategies for `fetch` events. For that we're
 [https://github.com/xwp/pwa-wp/issues/5 looking into using Workbox]. By
 having such an abstraction, we should be able to provide better detection
 for when two plugins/themes attempt to handle `fetch` events for the same
 routes.

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


More information about the wp-trac mailing list