Skip to content

Commit

Permalink
Use NetworkFirst as default navigation caching strategy and provide d…
Browse files Browse the repository at this point in the history
…efault args
  • Loading branch information
westonruter committed Oct 10, 2020
1 parent 681b672 commit f07b2f0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ class WP_Service_Worker_Caching_Routes implements WP_Service_Worker_Registry {
*/
const STRATEGY_NETWORK_ONLY = 'NetworkOnly';

/**
* Cache name for responses for navigation requests.
*
* @since 0.6
* @var string
*/
const NAVIGATIONS_CACHE_NAME = 'navigations';

/**
* Registered caching routes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,44 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
*
* @param string $caching_strategy Caching strategy to use for frontend navigation requests.
*/
$caching_strategy = apply_filters( 'wp_service_worker_navigation_caching_strategy', WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_ONLY );
$caching_strategy = apply_filters( 'wp_service_worker_navigation_caching_strategy', WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST );

$caching_strategy_args = array(
'cacheName' => WP_Service_Worker_Caching_Routes::NAVIGATIONS_CACHE_NAME,
);
if ( WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST === $caching_strategy ) {
/*
* The value of 2 seconds is informed by the Largest Contentful Paint (LCP) metric, of which Time to
* First Byte (TTFB) is a major component. As long as all assets on a page are cached, then this allows
* for the service worker to serve a previously-cached page and then for LCP to occur before 2.5s and
* so remain within the good threshold.
*/
$caching_strategy_args['networkTimeoutSeconds'] = 2;
}

/*
* By default cache only the last 10 pages visited. This may end up being too high as it seems likely that
* most site visitors will view one page and then maybe a couple others.
*/
$caching_strategy_args['plugins']['expiration']['maxEntries'] = 10;

/**
* Filters the caching strategy args used for frontend navigation requests.
*
* @since 0.2
* @since 0.6 Added $caching_strategy param and initial array has default values provided.
* @see WP_Service_Worker_Caching_Routes::register()
*
* @param array $caching_strategy_args Caching strategy args.
* @param array $caching_strategy_args {
* Caching strategy args.
*
* @type string $cacheName Cache name to store navigation responses.
* @type int $networkTimeoutSeconds Network timeout seconds when NetworkFirst strategy is used.
* @type array $plugins Configuration for plugins, in particular expiration.
* }
* @param string $caching_strategy Caching strategy being used.
*/
$caching_strategy_args = apply_filters( 'wp_service_worker_navigation_caching_strategy_args', array() );
$caching_strategy_args = apply_filters( 'wp_service_worker_navigation_caching_strategy_args', $caching_strategy_args, $caching_strategy );

$caching_strategy_args_js = WP_Service_Worker_Caching_Routes::prepare_strategy_args_for_js_export( $caching_strategy_args );

Expand Down

0 comments on commit f07b2f0

Please sign in to comment.