Slide 1

Slide 1 text

WordPressͰPWA P WA N i g h t 2 0 2 0 /0 7/ 1 5

Slide 2

Slide 2 text

Agenda • MPAͰPWAΛΊ͟͢ • WordPressͰPWA • ސ٬͕ຊ౰ʹཉ͔ͬͨ͠΋ͷ

Slide 3

Slide 3 text

Agenda • MPAͰPWAΛΊ͟͢ • WordPressͰPWA • ސ٬͕ຊ౰ʹཉ͔ͬͨ͠΋ͷ

Slide 4

Slide 4 text

SPA͡Όͳͯ͘΋ PWA͸ͭ͘ΕΔ

Slide 5

Slide 5 text

PWA is… • ʮઌਐతʯͰ • ʮ৴པੑʯͷ͋Δ • ʮΠϯετʔϧՄೳʯͳ • ʮ΢ΣϒΞϓϦέʔγϣϯʯ https://web.dev/what-are-pwas/

Slide 6

Slide 6 text

– 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ͭҎ্ͷαʔϏεϫʔΧʔɺ ϚχϑΣετϑΝΠϧΛ࣋ͭ΂͖Ͱ͢ɻ”

Slide 7

Slide 7 text

We need ✅ SSL Certification (HTTPS) ✅ Service Worker ✅ manifest.json Single Page Application

Slide 8

Slide 8 text

Multi Page ApplicationͰ͓͖Δ͜ͱ • ϖʔδભҠͰωοτϫʔΫΞΫηε͕ൃੜ͢Δ • ΦϑϥΠϯͰ͸ભҠઌͷHTML͕SWΩϟογϡ͞ΕΔඞཁ͋Γ • ʮ๚໰ͨ͠ϖʔδ͸ΦϑϥΠϯͰΈΕΔʯ͕ɺଞͷϖʔδ͸NG • SPA / SSGͩͱΞϓϦ಺ͰͲ͏ʹ͔Ͱ͖Δ͕ɺMPAͰ͸ݫ͍͠

Slide 9

Slide 9 text

MPA to SPA͸࣮࣭ϑϧϦχϡʔΞϧ • WordPress΍Laravelͷ֎ͰϑϩϯτΛߏங • ͜Ε·ͰͷϓϥάΠϯɾπʔϧɾϊ΢ϋ΢ΛࣺͯΔ • React / Gatsby / NetlifyͳͲͷίϯςΩετʹ৐Δίετ • SPAαʔόʔʴAPIαʔόʔͷΠϯϑϥ؅ཧΛͲ͏͢Δ͔

Slide 10

Slide 10 text

Agenda • MPAͰPWAΛΊ͟͢ • WordPressͰPWA • ސ٬͕ຊ౰ʹཉ͔ͬͨ͠΋ͷ

Slide 11

Slide 11 text

https://wordpress.org/plugins/pwa/

Slide 12

Slide 12 text

PWA Plugin by PWA plugin contributors • Google͕։ൃʹࢀը͍ͯ͠ΔϓϥάΠϯ • ίΞ΁ͷ౷߹Λݟਾ͑ͨ։ൃମ੍ • SWͷڝ߹ʢBYOSWʣ໰୊ΛWPతʹղܾ͢Δ • ࠷௿ݶͷػೳ͸Πϯετʔϧ͚ͩͰ΄΅ରԠ • ࣮ଶ͸Workbox https://wordpress.org/plugins/pwa/

Slide 13

Slide 13 text

Install and Activate https://wordpress.org/plugins/pwa/

Slide 14

Slide 14 text

Set Site Title Site Icon Site Title͸13จࣈҎ্ͩͱ লུ͞ΕΔ

Slide 15

Slide 15 text

{ "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

Slide 16

Slide 16 text

Check the site by Lighthouse short_nameͱPNG Icon͸ ͨ·ʹҾ͔͔ͬΔͷͰɺ ը૾αΠζ΍จࣈ਺Λௐ੔͢Δ

Slide 17

Slide 17 text

add_filter( 'web_app_manifest', function( $manifest ) { $manifest['short_name'] = 'Shortness'; // ৭ͱ͔΋ม͑ΕΔ // $manifest['theme_color'] = '#0073AA'; return $manifest; } ); Customize by WP Hooks

Slide 18

Slide 18 text

BYOSW(Bring Your Own Service Worker)໰୊ • ϓϥάΠϯ΍ςʔϚͰͦΕͧΕService WorkerΛొ࿥͢ΔͱԾఆ • ొ࿥͞ΕΔService Worker͸1αΠτʹ1ͭ • PWAϓϥάΠϯ(with Google)͸ू໿͢ΔͨΊͷϑοΫΛ࣋ͭ

Slide 19

Slide 19 text

WPͰService WorkerΛ࢖͏3ͭͷಓ 1. All in Oneͳawesome PWAϓϥάΠϯʹҰ೚͢Δ 2. PWAϓϥάΠϯ(with Google)Λ࢖͍ɺϑοΫͰௐ੔͢Δ 3. ࣗલͰϑϧεΫϥον

Slide 20

Slide 20 text

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' ), ) ); } );

Slide 21

Slide 21 text

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, ), ), ) ); } );

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Offline & HTTP500 Support

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

࣮ફతͳ࢖͍ํͷΞΠσΟΞ • offline.phpͰΦϑϥΠϯίϯςϯπΛ༻ҙ͢Δ • 500.phpͰαʔόʔΤϥʔ͕ൃੜͨ͠৔߹ͷίϯςϯπΛ༻ҙ͢Δ • ςʔϚͷΞηοτʢΞΠίϯͳͲʣ΍Ajaxݺͼग़͠ΛSWͰprecache • ΦϑϥΠϯͰͷίϝϯτ஗Ԇૹ৴

Slide 26

Slide 26 text

͜͏͍͏࢖͍ํ΋Ͱ͖ͦ͏Ͱ͕͢ɾɾɾ • ECαΠτͷ঎඼ը૾Λ͢΂ͯService WorkerͰΩϟογϡ • 2ճ໨Ҏ߱͸ը૾ͷωοτϫʔΫΞΫηε͕ͳ͘ͳͬͯߴ଎Խʂ • ʮͦͷը૾ɺͲ͜ʹอଘ͞Ε͍ͯ·͔͢ʁʯ

Slide 27

Slide 27 text

࣍๚ΕΔ͔΋ Θ͔Βͳ͍αΠτͷͨΊʹɺ ͋ͳͨͷσΟεΫ༰ྔΛ ࠩ͠ग़͍ͨ͠ͱࢥ͍·͔͢ʁ

Slide 28

Slide 28 text

σόΠεͷσΟεΫ༰ྔʹґଘ͢Δ͕ɺ ࣗ෼͚͕ͩ޷͖ʹ࢖ͬͯΑ͍΋ͷͰ͸ͳ͍ – 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.”

Slide 29

Slide 29 text

ετΞ͢Δσʔλ͸શମͰ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.”

Slide 30

Slide 30 text

ྺ࢙ʹֶͼɺ Evil͸ආ͚Δ https://webkit.org/blog/10218/full- third-party-cookie-blocking-and- more/

Slide 31

Slide 31 text

Agenda • MPAͰPWAΛΊ͟͢ • WordPressͰPWA • ސ٬͕ຊ౰ʹཉ͔ͬͨ͠΋ͷ

Slide 32

Slide 32 text

PWAͰʮύϑΥʔϚϯε͕վળʯ͢Δ΋ͷͷྫ • ΦϑϥΠϯ • Ωϟογϡ͞Εͨίϯςϯπɾϖʔδͷදࣔ • Predictive PrefetchingʹΑΔઌಡΈͰͷߴ଎Խ

Slide 33

Slide 33 text

– 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 “͔͠͠Ұ౓ΞϓϦΛΠϯετʔϧͯ͠ΞϓϦΛ։࢝͢Δͱɺͦͷۤ࿑͸͢΂ͯใΘΕ ·͢ɻ࣮ߦ·Ͱʹ଴ͨ͞ΕΔ͕࣌ؒมΘΔΑ͏ͳ͜ͱ͸͋Γ·ͤΜɻΞϓϦέʔγϣ ϯͷىಈ͸લճͱಉ͘͡Β͍ૣ͘ɺมΘΔ͜ͱ͸͋Γ·ͤΜɻΠϯετʔϧ͢Δ͜ͱ ͰϢʔβʔ͕ظ଴͢ΔΑ͏ʹͳΔ͜ͷ৴པͰ͖ΔύϑΥʔϚϯεΛϓϩάϨογϒ ΢ΣϒΞϓϦ͸ఏڙ͠ͳ͚Ε͹ͳΓ·ͤΜɻ”

Slide 34

Slide 34 text

׬શͳ৽نϢʔβʔʹͱͬͯ PWAʹΑΔߴ଎ԽͷԸܙ͸ͳ͍

Slide 35

Slide 35 text

ʮPWA͔ͩΒૣ͍ʯ ʮૣ͍ΞϓϦ͕PWAͩͬͨʯ • ϘτϧωοΫͷௐࠪ • CDN΍NginxͳͲͷΩϟογϡͷ׆༻ • AWS / GCPͳͲͰPHP / MySQLαʔόʔΛߴ଎Խ • Jamstack΁ͷϑϧϦχϡʔΞϧ • ʮPWAͷߴ଎Խʯͷલʹ΍Δ͜ͱ͕ͳ͍͔Λ͔֬ΊΔ

Slide 36

Slide 36 text

PWAͰϢʔβʔ͕تͼͦ͏ͳ͜ͱΛߟ͑Δ • ΦϑϥΠϯʹͳͬͯ΋ߪೖϑϩʔ͕தஅ͞Εͳ͍ECαΠτ • τϯωϧʹೖͬͯ΋ίϝϯτ౤ߘ͕Ͱ͖Δܝࣔ൘ɾϒϩά • ϝʔϧड৴ͳͲͷόοΫάϥ΢ϯυλεΫʹؔ͢ΔPUSH௨஌ • αʔόʔʹো֐͕ى͖ͯ΋୅ସίϯςϯπ͕ΈΕΔϝσΟΞ

Slide 37

Slide 37 text

How hard can it be?

Slide 38

Slide 38 text

Thanks you! • MPAͰPWAΛΊ͟͢ • WordPressͰPWA • ސ٬͕ຊ౰ʹཉ͔ͬͨ͠΋ͷ Shifter https://getshifter.io Twitter @motchi0214 GitHub https://github.com/hideokamoto/