Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
clean up
  • Loading branch information
ellatrix committed Jul 2, 2021
commit f6f274e1e2700c865c39ef0fab11a7cafd7ae2a4
42 changes: 25 additions & 17 deletions lib/pwa-load.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* global scriptVars */

const win = window;

function addManifest( manifest ) {
const link = document.createElement( 'link' );
link.rel = 'manifest';
Expand Down Expand Up @@ -32,7 +30,7 @@ function createIcon( svgElement, size, backgroundColor ) {
context.fillStyle = backgroundColor;
context.fillRect( 0, 0, canvas.width, canvas.height );

const blob = new win.Blob( [ svgElement.outerHTML ], {
const blob = new window.Blob( [ svgElement.outerHTML ], {
type: 'image/svg+xml',
} );
const url = URL.createObjectURL( blob );
Expand All @@ -54,16 +52,35 @@ function createIcon( svgElement, size, backgroundColor ) {
} );
}

if ( 'serviceWorker' in win.navigator ) {
win.addEventListener( 'load', function () {
const { serviceWorkerUrl, logo, manifest } = scriptVars;
if ( 'serviceWorker' in window.navigator ) {
// eslint-disable-next-line @wordpress/no-global-event-listener
window.addEventListener( 'load', function () {
const { serviceWorkerUrl, logo, siteTitle, adminUrl } = scriptVars;
const manifest = {
name: siteTitle,
display: 'standalone',
orientation: 'portrait',
start_url: adminUrl,
// Open front-end, login page, and any external URLs in a browser
// modal.
scope: adminUrl,
icons: [],
};

win.navigator.serviceWorker.register( serviceWorkerUrl );
window.navigator.serviceWorker.register( serviceWorkerUrl );

const adminBar = document.getElementById( 'wpadminbar' );
const { backgroundColor } = window.getComputedStyle( adminBar );
const svgElement = createSvgElement( logo );

function addToManifest( base64data ) {
manifest.icons.push( {
src: base64data,
sizes: '192x192',
type: 'image/png',
} );
}

createIcon( svgElement, 180, backgroundColor ).then( ( base64data ) => {
const iconLink = document.createElement( 'link' );
iconLink.rel = 'apple-touch-icon';
Expand All @@ -73,18 +90,9 @@ if ( 'serviceWorker' in win.navigator ) {
iconLink,
document.head.firstElementChild
);
addToManifest( base64data );
} );

manifest.icons = [];

function addToManifest( base64data ) {
manifest.icons.push( {
src: base64data,
sizes: '192x192',
type: 'image/png',
} );
}

Promise.all( [
createIcon( svgElement, 192, backgroundColor ).then(
addToManifest
Expand Down
48 changes: 4 additions & 44 deletions lib/pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,15 @@
add_filter(
'admin_head',
function() {
$icon_sizes = array( 180, 192, 512 );

$icons = array(
array(
'src' => 'https://s1.wp.com/i/favicons/apple-touch-icon-180x180.png',
'sizes' => '180x180',
'type' => 'image/png',
),
// Android/Chrome.
array(
'src' => 'https://wordpress.com/calypso/images/manifest/icon-192x192.png',
'sizes' => '192x192',
'type' => 'image/png',
),
array(
'src' => 'https://wordpress.com/calypso/images/manifest/icon-512x512.png',
'sizes' => '512x512',
'type' => 'image/png',
),
);

if ( false ) {
$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 = 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(),
);

$script = file_get_contents( __DIR__ . '/pwa-load.js' );
$script_vars = wp_json_encode(
array(
// Must be at the admin root so the scope is correct. Move to the
// wp-admin folder when merging with core.
'serviceWorkerUrl' => admin_url( '?service-worker' ),
'logo' => file_get_contents( ABSPATH . 'wp-admin/images/wordpress-logo-white.svg' ),
'manifest' => $manifest,
'siteTitle' => get_bloginfo( 'name' ),
'adminUrl' => admin_url(),
)
);

Expand All @@ -74,6 +32,8 @@ function() {
}

header( 'Content-Type: text/javascript' );
// Must be at the admin root so the scope is correct. Move to the
// wp-admin folder when merging with core.
echo file_get_contents( __DIR__ . '/service-worker.js' );
exit;
}
Expand Down