Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WordPressでPWA/pwa-night-202007

 WordPressでPWA/pwa-night-202007

Hidetaka Okamoto

July 15, 2020
Tweet

More Decks by Hidetaka Okamoto

Other Decks in Programming

Transcript

  1. – h t t p s : / / d

    e v e l o p e r. m o z i l l a . o rg / j a / d o c s / We b / P ro g re s s i v e _ w e b _ a p p s “΢ΣϒΞϓϦΛ PWA ͱݺͿʹ͸ɺ ٕज़తʹݴ͑͹ɺ ҆શͳίϯςΩετ (HTTPS)ɺ 1ͭҎ্ͷαʔϏεϫʔΧʔɺ ϚχϑΣετϑΝΠϧΛ࣋ͭ΂͖Ͱ͢ɻ”
  2. MPA to SPA͸࣮࣭ϑϧϦχϡʔΞϧ • WordPress΍Laravelͷ֎ͰϑϩϯτΛߏங • ͜Ε·ͰͷϓϥάΠϯɾπʔϧɾϊ΢ϋ΢ΛࣺͯΔ • React /

    Gatsby / NetlifyͳͲͷίϯςΩετʹ৐Δίετ • SPAαʔόʔʴAPIαʔόʔͷΠϯϑϥ؅ཧΛͲ͏͢Δ͔
  3. PWA Plugin by PWA plugin contributors • Google͕։ൃʹࢀը͍ͯ͠ΔϓϥάΠϯ • ίΞ΁ͷ౷߹Λݟਾ͑ͨ։ൃମ੍

    • SWͷڝ߹ʢBYOSWʣ໰୊ΛWPతʹղܾ͢Δ • ࠷௿ݶͷػೳ͸Πϯετʔϧ͚ͩͰ΄΅ରԠ • ࣮ଶ͸Workbox https://wordpress.org/plugins/pwa/
  4. { "name": "Less than 15 words", "start_url": "https://50c106d8-45d6-47af-8561-b6a93a3067dc.appdev.getshifter.io:56104/", "display": "minimal-ui",

    "dir": "ltr", "lang": "en-US", "background_color": "#f5efe0", "theme_color": "#f5efe0", "description": "Just another WordPress site", "icons": [ { "src": "/wp-content/uploads/2020/07/cropped-A9D30BD8-CEDE-4990-B168-8DE593A8A295-scaled-2-192x192.jpeg", "sizes": "192x192", "type": "image/jpeg" }, { "src": "/wp-content/uploads/2020/07/cropped-A9D30BD8-CEDE-4990-B168-8DE593A8A295-scaled-2.jpeg", "sizes": "512x512", "type": "image/jpeg" } ] } Test Call WP API GET /wp-json/wp/v2/web-app- manifest
  5. add_filter( 'web_app_manifest', function( $manifest ) { $manifest['short_name'] = 'Shortness'; //

    ৭ͱ͔΋ม͑ΕΔ // $manifest['theme_color'] = '#0073AA'; return $manifest; } ); Customize by WP Hooks
  6. Example: Cache my-theme-image.png add_action( 'wp_front_service_worker', function( \WP_Service_Worker_Scripts $scripts ) {

    $scripts->precaching_routes()->register( 'https://example.com/wp-content/themes/my-theme/my-theme-image.png', array( 'revision' => get_bloginfo( 'version' ), ) ); } );
  7. Example: Cache wp-content image files add_action( ‘wp_front_service_worker', function( \WP_Service_Worker_Scripts $scripts

    ) { $scripts->caching_routes()->register( ‘/wp-content/.*\.(?:png|gif|jpg|jpeg|svg|webp)(\?.*)?$', array( 'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST, 'cacheName' => ‘images', 'plugins' => array( 'expiration' => array( 'maxEntries' => 60, 'maxAgeSeconds' => 60 * 60 * 24, ), ), ) ); } );
  8. Lists of WP hooks • [Filter Hook] wp_service_worker_skip_waiting • [Filter

    Hook] wp_service_worker_clients_claim • [Filter Hook] wp_service_worker_nativation_preload • [Filter Hook] wp_offline_error_precache_entry • [Filter Hook] wp_server_error_precache_entry • [Filter Hook] wp_service_worker_errormessages • [Action Hook] wp_front_service_worker • [Action Hook] wp_admin_service_worker • [Action Hook] wp_default_service_workers
  9. Service WorkerΛ࢖ͬͯɺ ΦϑϥΠϯ / HTTP500ͷදࣔΛม͑ΕΔ • Offline page templateʢ/YOUR-THEME/offline.phpʣ https://your-site-name.com/?wp_error_template=offline;

    • HTTP500 page templateʢ/YOUR-THEME/500.phpʣ https://your-site-name.com/?wp_error_template=500 • ಡΈࠐΈ৔ॴɾॱংͳͲ͸ίʔυࢀর(TwentyγϦʔζ͸σϑΥϧτରԠ) https://github.com/GoogleChromeLabs/pwa-wp/blob/develop/wp-includes/template.php#L31-L64
  10. σόΠεͷσΟεΫ༰ྔʹґଘ͢Δ͕ɺ ࣗ෼͚͕ͩ޷͖ʹ࢖ͬͯΑ͍΋ͷͰ͸ͳ͍ – h t t p s : /

    / l o v e 2 d e v. c o m / b l o g / w h a t - i s - t h e - s e r v i c e - w o r k e r- c a c h e - s t o r a g e - l i m i t / “The amount of storage is dependent upon how much available disk space the customer's device has. The general range is 50MB over 20GB. Before you get all excited about 20GB of storage you still need to be responsible.”
  11. ετΞ͢Δσʔλ͸શମͰ50MBʹೲΊΔ – h t t p s : / /

    l o v e 2 d e v. c o m / b l o g / w h a t - i s - t h e - s e r v i c e - w o r k e r- c a c h e - s t o r a g e - l i m i t / “Since most visitors are on a mobile device, you should plan for about 50MB of available space. This should keep you within a safe zone that enables you to create a good experience without consuming the user's bandwidth.”
  12. – h t t p s : / / d

    e v e l o p e r s . g o o g l e . c o m / w e b / f u n d a m e n t a l s / c o d e l a b s / y o u r- f i r s t - p w a p p ? h l = j a “͔͠͠Ұ౓ΞϓϦΛΠϯετʔϧͯ͠ΞϓϦΛ։࢝͢Δͱɺͦͷۤ࿑͸͢΂ͯใΘΕ ·͢ɻ࣮ߦ·Ͱʹ଴ͨ͞ΕΔ͕࣌ؒมΘΔΑ͏ͳ͜ͱ͸͋Γ·ͤΜɻΞϓϦέʔγϣ ϯͷىಈ͸લճͱಉ͘͡Β͍ૣ͘ɺมΘΔ͜ͱ͸͋Γ·ͤΜɻΠϯετʔϧ͢Δ͜ͱ ͰϢʔβʔ͕ظ଴͢ΔΑ͏ʹͳΔ͜ͷ৴པͰ͖ΔύϑΥʔϚϯεΛϓϩάϨογϒ ΢ΣϒΞϓϦ͸ఏڙ͠ͳ͚Ε͹ͳΓ·ͤΜɻ”
  13. ʮPWA͔ͩΒૣ͍ʯ ʮૣ͍ΞϓϦ͕PWAͩͬͨʯ • ϘτϧωοΫͷௐࠪ • CDN΍NginxͳͲͷΩϟογϡͷ׆༻ • AWS / GCPͳͲͰPHP

    / MySQLαʔόʔΛߴ଎Խ • Jamstack΁ͷϑϧϦχϡʔΞϧ • ʮPWAͷߴ଎Խʯͷલʹ΍Δ͜ͱ͕ͳ͍͔Λ͔֬ΊΔ