• Resolved bhushanravi2009

    (@bhushanravi2009)


    I am new to PWA, I have a requirement, I need to intercept an ajax request and if offline then fetch data from indexdb and return,is there something ( filter or action) where I can write my custom code.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    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

    Thread Starter bhushanravi2009

    (@bhushanravi2009)

    Thanks for your quick and precise repsonse.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Return some custom reponse’ is closed to new replies.