Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: Admin PWA #33102

Merged
merged 13 commits into from
Jul 5, 2021
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/navigation-page.php';
require __DIR__ . '/experiments-page.php';
require __DIR__ . '/global-styles.php';
require __DIR__ . '/pwa.php';

require __DIR__ . '/block-supports/generated-classname.php';
require __DIR__ . '/block-supports/elements.php';
Expand Down
67 changes: 67 additions & 0 deletions lib/pwa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Progressive Web App
*
* @package gutenberg
*/

add_filter(
'admin_head',
function() {
$icon_sizes = array( 32, 180, 192, 270, 512 );

// Ideally we have the WordPress logo as the default app logo in the sizes
// below.
$icons = array();

if ( has_site_icon() ) {
$type = wp_check_filetype( get_site_icon_url() );

foreach ( $icon_sizes as $size ) {
$icons[] = array(
'src' => get_site_icon_url( $size ),
'sizes' => $size . 'x' . $size,
'type' => $type['type'],
);
}
}

$manifest = wp_json_encode(
array(
'name' => get_bloginfo( 'name' ),
'icons' => $icons,
'display' => 'standalone',
'orientation' => 'portrait',
'start_url' => admin_url(),
// Open front-end, login page, and any external URLs in a browser modal.
'scope' => admin_url(),
)
);

// Must be at the admin root so the scope is correct. Move to the
// wp-admin folder when merging with core.
$service_worker_url = admin_url( '?service-worker' );

echo '<link rel="manifest" href=\'data:application/manifest+json,' . $manifest . '\'>';
echo '<script>
if( "serviceWorker" in window.navigator ) {
window.addEventListener( "load", function() {
window.navigator.serviceWorker.register( "' . $service_worker_url . '" );
} );
}
</script>';
}
);

add_filter(
'load-index.php',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PWA plugin makes use of admin-ajax.php for serving the service worker which also works.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we could do that too :)

function() {
if ( ! isset( $_GET['service-worker'] ) ) {
return;
}

header( 'Content-Type: text/javascript' );
echo file_get_contents( __DIR__ . '/service-worker.js' );
exit;
}
);
14 changes: 14 additions & 0 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* global self */

// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting
self.addEventListener( 'install', function ( event ) {
event.waitUntil( self.skipWaiting() );
} );

// https://developer.mozilla.org/en-US/docs/Web/API/Clients/claim
self.addEventListener( 'activate', function ( event ) {
event.waitUntil( self.clients.claim() );
} );

// Necessary for Chrome to show the install button.
self.addEventListener( 'fetch', function () {} );