Skip to content

Commit

Permalink
Use WP version as the revision when precaching admin files.
Browse files Browse the repository at this point in the history
  • Loading branch information
miina committed Aug 7, 2018
1 parent bbf383d commit 024eab3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 50 deletions.
116 changes: 67 additions & 49 deletions wp-includes/class-wp-service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ public function init() {
array()
);

if ( SCRIPT_DEBUG || is_preview() ) {
$this->register_cached_route( '/(wp-admin|wp-includes)/.*\.(?:js|css)/', self::STRATEGY_NETWORK_ONLY );
$this->register(
'caching-utils-sw',
PWA_PLUGIN_URL . '/wp-includes/js/service-worker.js',
array( 'workbox-sw' )
);

if ( SCRIPT_DEBUG ) {
$this->register_cached_route( '/(wp-admin|wp-includes)/.*\.(?:js|css|gif|png|svg)/', self::STRATEGY_NETWORK_ONLY, array(), true );
} else {
$this->precache_admin_assets();
}
Expand Down Expand Up @@ -169,13 +175,6 @@ public function get_workbox_script() {
$script .= "/* Navigation preload disabled. */\n";
}

$script .= '
if ( ! self.wp ) {
self.wp = {};
}
wp.serviceWorker = workbox;';

return $script;
}

Expand All @@ -185,27 +184,44 @@ public function get_workbox_script() {
protected function precache_admin_assets() {

$admin_dir = ABSPATH . 'wp-admin/';
$admin_files = array_merge(
list_files( $admin_dir . 'css/' ),
list_files( $admin_dir . 'js/' ),
list_files( $admin_dir . 'images/' )
);
$inc_files = array_merge(
list_files( ABSPATH . WPINC . '/js/' ),
list_files( ABSPATH . WPINC . '/css/' ),
list_files( ABSPATH . WPINC . '/images/' )
);
$admin_images = list_files( $admin_dir . 'images/' );
$inc_images = list_files( ABSPATH . WPINC . '/images/' );

$routes = array_merge(
$this->get_routes_from_file_list( $admin_files, 'wp-admin' ),
$this->get_routes_from_file_list( $inc_files, 'wp-includes' )
$this->get_routes_from_file_list( $admin_images, 'wp-admin' ),
$this->get_routes_from_file_list( $inc_images, 'wp-includes' ),
$this->get_admin_routes_from_dependency_list( wp_scripts()->registered ),
$this->get_admin_routes_from_dependency_list( wp_styles()->registered )
);

if ( empty( $routes ) ) {
return;
}

$this->register_cached_route( $routes, self::STRATEGY_PRECACHE );
$this->register_precached_routes( $routes );
}

/**
* Get routes from dependency list.
*
* @param array $dependencies Array of _WP_Dependency objects.
* @return array Array of routes.
*/
protected function get_admin_routes_from_dependency_list( $dependencies ) {
$routes = array();
foreach ( $dependencies as $handle => $params ) {

// Only precache scripts from wp-admin and wp-includes.
if ( false === strpos( $params->src, 'wp-admin' ) && false === strpos( $params->src, 'wp-includes' ) ) {
continue;
}
$revision = false === $params->ver ? get_bloginfo( 'version' ) : $params->ver;
$routes[] = array(
'url' => $params->src,
'revision' => $revision,
);
}
return $routes;
}

/**
Expand All @@ -219,22 +235,14 @@ protected function get_routes_from_file_list( $list, $folder ) {
$routes = array();
foreach ( $list as $filename ) {
$ext = pathinfo( $filename, PATHINFO_EXTENSION );
if ( ! in_array( $ext, array( 'js', 'css', 'png', 'gif', 'svg' ), true ) ) {
continue;
}

// Only precache minified files in case there is one.
if ( 'css' === $ext && '.min.css' !== substr( $filename, -strlen( '.min.css' ) ) ) {
if ( ! in_array( $ext, array( 'png', 'gif', 'svg' ), true ) ) {
continue;
} elseif ( 'js' === $ext && '.min.js' !== substr( $filename, -strlen( '.min.js' ) ) ) {

// If there exists a minified file in the array, then skip this.
if ( in_array( str_replace( '.js', '.min.js', $filename ), $list, true ) ) {
continue;
}
}

$routes[] = strstr( $filename, '/' . $folder );
$routes[] = array(
'url' => strstr( $filename, '/' . $folder ),
'revision' => get_bloginfo( 'version' ),
);
}

return $routes;
Expand Down Expand Up @@ -309,11 +317,16 @@ public function register_cached_route( $route, $strategy, $strategy_args = array
/**
* Register routes / files for precaching.
*
* @param array $routes Array of routes, each route must be a string literal.
* @param array $routes {
* Array of routes.
*
* @type string $url URL of the route.
* @type string $revision Revision (optional).
* }
*/
public function register_precached_routes( $routes ) {
if ( ! is_array( $routes ) || empty( $routes ) ) {
_doing_it_wrong( __METHOD__, esc_html__( 'Routes must be an array consisting of string literals.', 'pwa' ), '0.2' );
_doing_it_wrong( __METHOD__, esc_html__( 'Routes must be an array.', 'pwa' ), '0.2' );
return;
}
$this->registered_precaching_routes = array_merge(
Expand All @@ -332,27 +345,33 @@ protected function register_precaching_for_routes( $routes ) {

$routes_list = array();

foreach ( $routes as $route ) {
$validated_path = $this->get_validated_file_path( $route, false );
if ( ! is_wp_error( $validated_path ) ) {
foreach ( $routes as $route => $params ) {
if ( ! isset( $params['url'] ) ) {
continue;
}
$validated_path = $this->get_validated_file_path( $params['url'], false );
if ( is_wp_error( $validated_path ) ) {
continue;
}

if ( ! isset( $params['revision'] ) ) {
$file_content = @file_get_contents( $validated_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_read_file_get_contents
if ( ! $file_content ) {
continue;
}

$hash = md5( $file_content );
$routes_list[] = array(
'url' => $route,
'revision' => $hash,
);
$params['revision'] = md5( $file_content );
}
$routes_list[] = array(
'url' => $params['url'],
'revision' => $params['revision'],
);
}

if ( empty( $routes_list ) ) {
return '';
}

return sprintf( "wp.serviceWorker.precaching.precacheAndRoute( %s );\n{
return sprintf( "wp.serviceWorker.precaching.precacheAndRoute( %s,\n{
ignoreUrlParametersMatching: [/.*/]
}
);\n", wp_json_encode( $routes_list ) );
Expand Down Expand Up @@ -486,8 +505,7 @@ public function serve_request( $scope ) {
* Add logic for precaching to the request output.
*/
protected function do_precaching_routes() {
$routes_to_precache = array_unique( $this->registered_precaching_routes );
$this->output .= $this->register_precaching_for_routes( array_unique( $routes_to_precache ) );
$this->output .= $this->register_precaching_for_routes( $this->registered_precaching_routes );
}

/**
Expand Down
7 changes: 6 additions & 1 deletion wp-includes/service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ function wp_register_route_caching_strategy( $route, $strategy = WP_Service_Work
*
* @since 0.2
*
* @param array $routes Array of routes, each route must be a string literal.
* @param array $routes {
* Array of routes.
*
* @type string $url URL of the route.
* @type string $revision Revision (optional).
* }
*/
function wp_register_routes_precaching( $routes ) {
return wp_service_workers()->register_precached_routes( $routes );
Expand Down

0 comments on commit 024eab3

Please sign in to comment.