Yes. To do this you’d need to write Workbox.js code. Let’s say you have an ajax-offline
plugin in which you have the following PHP code:
add_action( 'wp_front_service_worker', static function ( $scripts ) {
$scripts->register(
'ajax-offline',
array(
'src' => plugin_dir_url( __FILE__ ) . 'ajax-offline.js',
)
);
} );
Inside this plugin you also have here a ajax-offline.js
file. In this file you put JS code to interact with Workbox.js or even directly write your own fetch
handlers if you want.
For Workbox, you’d want to do workbox.routing.registerRoute('/url/to/ajax/endpoint', handler)
. The handler
would be probably involving the NetworkFirst
strategy and but then falling back to your custom code when offline. For more I recommend the Workbox documentation and support channels: https://developers.google.com/web/tools/workbox/guides/route-requests