Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active January 29, 2020 06:35
Show Gist options
  • Save westonruter/ce3b5a568f10f4ca76f8bc5d5e8e4ab3 to your computer and use it in GitHub Desktop.
Save westonruter/ce3b5a568f10f4ca76f8bc5d5e8e4ab3 to your computer and use it in GitHub Desktop.
Proof of concept to integrate the WordPress PWA plugin with the Content Indexing API. Depends on https://github.com/xwp/pwa-wp/pull/234
if ( ! wp.hooks ) {
console.warn( 'The WP Content Indexing API depends on this PR from the PWA plugin: https://github.com/xwp/pwa-wp/pull/234' );
} else {
wp.hooks.addAction( 'navigation_response_success', 'basic_site_caching.navigation_response_success', async ( { event, response, navigationCacheStrategy } ) => {
if ( navigationCacheStrategy instanceof wp.serviceWorker.strategies.NetworkOnly ) {
console.warn( 'Unable to use Content Indexing API because NetworkOnly navigation caching strategy is being used.' );
return;
}
if ( !registration.index ) {
console.warn( 'Unable to use Content Indexing API because it is not currently enabled (e.g. via origin trial). See: https://web.dev/content-indexing-api/' );
return;
}
console.info( "navigation_response_success", { event, response, navigationCacheStrategy } );
console.info( { registration } )
// @todo Scrape the title, URL, ID, description, and icons
return;
await registration.index.add( {
// Required; set to something unique within your web app.
id: 'article-123',
// Required; this URL needs to be an offline-capable HTML page.
launchUrl: '/articles/123',
// Required; used in user-visible lists of content.
title: 'Article title',
// Required; used in user-visible lists of content.
description: 'Amazing article about things!',
// Required; used in user-visible lists of content.
icons: [ {
src: '/img/article-123.png',
sizes: '64x64',
type: 'image/png',
} ],
// Optional; valid categories are currently:
// 'homepage', 'article', 'video', 'audio', or '' (default).
category: 'article',
} );
// @todo what about removal?
} );
}
<?php
/**
* WP Content Indexing API
*
* @package WP_Content_Indexing_API
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @wordpress-plugin
* Plugin Name: WP Content Indexing API
* Description: Proof of concept for how to integrate the Content Indexing API into the WordPress service worker generated by the PWA plugin.
* Plugin URI: https://gist.github.com/westonruter/ce3b5a568f10f4ca76f8bc5d5e8e4ab3
* 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 WP_Content_Indexing_API;
// Add caching for uploaded images.
add_action(
'wp_front_service_worker',
function ( \WP_Service_Worker_Scripts $scripts ) {
$scripts->register(
'content_indexing_api',
array(
'src' => plugin_dir_url( __FILE__ ) . 'wp-content-indexing-api.js',
)
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment