Skip to content

Commit

Permalink
pwa support
Browse files Browse the repository at this point in the history
  • Loading branch information
billerickson committed Nov 16, 2019
1 parent 965b966 commit 70e2805
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function ea_child_theme_setup() {
// Plugin Support
include_once( get_stylesheet_directory() . '/inc/acf.php' );
include_once( get_stylesheet_directory() . '/inc/amp.php' );
include_once( get_stylesheet_directory() . '/inc/pwa.php' );
include_once( get_stylesheet_directory() . '/inc/shared-counts.php' );
include_once( get_stylesheet_directory() . '/inc/wpforms.php' );

Expand Down
81 changes: 81 additions & 0 deletions inc/pwa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* PWA
*
* @package EAGenesisChild
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/

namespace Basic_Site_Caching;

// Customize the manifest
add_filter(
'web_app_manifest',
function( $manifest ) {
//$manifest['short_name'] = 'Client Short Name';
$manifest['display'] = 'standalone';
return $manifest;
}
);

// Enable network-first caching strategy for navigation requests (i.e. clicking around the site).
add_filter(
'wp_service_worker_navigation_caching_strategy',
function () {
return \WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST;
}
);
// Hold on to a certain number of navigated pages in the cache.
add_filter(
'wp_service_worker_navigation_caching_strategy_args',
function ( $args ) {
$args['cacheName'] = 'pages';
$args['plugins']['expiration']['maxEntries'] = 20;
return $args;
}
);
// Cache theme assets with runtime network-first caching strategy. This includes both the parent theme and child theme.
add_action(
'wp_front_service_worker',
function ( \WP_Service_Worker_Scripts $scripts ) {
$theme_directory_uri_patterns = [
preg_quote( trailingslashit( get_template_directory_uri() ), '/' ),
];
if ( get_template() !== get_stylesheet() ) {
$theme_directory_uri_patterns[] = preg_quote( trailingslashit( get_stylesheet_directory_uri() ), '/' );
}
$scripts->caching_routes()->register(
'^(' . implode( '|', $theme_directory_uri_patterns ) . ').*',
array(
'strategy' => \WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
'cacheName' => 'theme-assets',
'plugins' => array(
'expiration' => array(
'maxEntries' => 25, // Limit the cached entries to the number of files loaded over network, e.g. JS, CSS, and PNG.
),
),
)
);
}
);
// Add caching for uploaded images.
add_action(
'wp_front_service_worker',
function ( \WP_Service_Worker_Scripts $scripts ) {
$upload_dir = wp_get_upload_dir();
$scripts->caching_routes()->register(
'^(' . preg_quote( $upload_dir['baseurl'], '/' ) . ').*\.(png|gif|jpg|jpeg|svg|webp)(\?.*)?$',
array(
'strategy' => \WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST,
'cacheName' => 'uploads',
'plugins' => array(
'expiration' => array(
'maxAgeSeconds' => MONTH_IN_SECONDS,
),
),
)
);
}
);
26 changes: 26 additions & 0 deletions offline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* PWA Offline
*
* @package EAGenesisChild
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/

/**
* Offline Content
*
*/
function ea_pwa_offline_content() {
echo '<h1>Oops, it looks like you\'re offline</h1>';
if( function_exists( 'wp_service_worker_error_message_placeholder' ) )
wp_service_worker_error_message_placeholder();

}
add_action( 'genesis_loop', 'ea_pwa_offline_content' );
remove_action( 'genesis_loop', 'ea_archive_loop' );
remove_action( 'genesis_loop', 'genesis_do_loop' );

// Build the page
genesis();

0 comments on commit 70e2805

Please sign in to comment.