Last active
August 28, 2019 23:48
-
-
Save westonruter/caa6e08b8d1f70cd3ddab1064d7d1bc2 to your computer and use it in GitHub Desktop.
PWA extension plugin to facilitate precaching styles, as an alternative to using WP_Service_Worker_Styles_Integration. See https://wordpress.org/support/topic/precaching-best-practices/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PWA Precache Styles | |
* | |
* @package PWA_Precache_Styles | |
* @author Weston Ruter, Google | |
* @license GPL-2.0-or-later | |
* @copyright 2019 Google Inc. | |
* | |
* @wordpress-plugin | |
* Plugin Name: PWA Precache Styles | |
* Description: Add styles with the 'precache' data to the precache so they will be served cache-first and be available while offline. Depends on the <a href="https://wordpress.org/plugins/pwa/">PWA plugin</a>. To flag a given stylesheet for precaching, just do <code>wp_style_add_data( $handle, 'precache', true )</code> after registering it (e.g. at the <code>wp_enqueue_scripts</code> action). | |
* Plugin URI: https://gist.github.com/westonruter/caa6e08b8d1f70cd3ddab1064d7d1bc2 | |
* Version: 0.1.0 | |
* Author: Weston Ruter, Google | |
* Author URI: https://weston.ruter.net/ | |
* License: GNU General Public License v2 (or later) | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
namespace PWA_Precache_Styles; | |
/** | |
* Register each style with 'precache' data for precaching. | |
* | |
* @eee WP_Service_Worker_Styles_Integration | |
* @see \WP_Styles::_css_href() | |
* @see \WP_Styles::do_item() | |
* @link https://github.com/xwp/pwa-wp/blob/0.3.0/integrations/class-wp-service-worker-styles-integration.php | |
* @param \WP_Service_Worker_Scripts $scripts Scripts. | |
*/ | |
function register_styles_for_precaching( $scripts ) { | |
if ( ! $scripts instanceof \WP_Service_Worker_Scripts ) { | |
_doing_it_wrong( __FUNCTION__, 'Argument is not instance of WP_Service_Worker_Scripts. Perhaps PWA plugin changed?', '0.1' ); | |
return; | |
} | |
if ( ! method_exists( $scripts->precaching_routes(), 'register' ) ) { | |
_doing_it_wrong( __FUNCTION__, 'Unable to locate WP_Service_Worker_Scripts::precaching_routes()->register() method. Perhaps PWA plugin changed?', '0.1' ); | |
return; | |
} | |
foreach ( wp_styles()->registered as $handle => $dependency ) { | |
if ( empty( $dependency->extra['precache'] ) ) { | |
continue; | |
} | |
// This code is copied from \WP_Styles::_css_href() and \WP_Styles::do_item(). | |
$url = $dependency->src; | |
if ( is_string( $url ) && ! preg_match( '|^(https?:)?//|', $url ) && ! ( wp_styles()->content_url && 0 === strpos( $url, wp_styles()->content_url ) ) ) { | |
$url = wp_styles()->base_url . $url; | |
} | |
// This code is copied from . | |
if ( null === $dependency->ver ) { | |
$ver = ''; | |
} else { | |
$ver = $dependency->ver ? $dependency->ver : get_bloginfo( 'version' ); | |
} | |
// Amend the version to the URL. | |
if ( ! empty( $ver ) ) { | |
$url = add_query_arg( compact( 'ver' ), $url ); | |
} | |
/** This filter is documented in wp-includes/class.wp-styles.php */ | |
$url = apply_filters( 'style_loader_src', $url, $handle ); | |
$scripts->precaching_routes()->register( $url, $ver ); | |
} | |
} | |
add_action( 'wp_front_service_worker', __NAMESPACE__ . '\register_styles_for_precaching' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment