PWA Night CONFERENCE 2020 に
興味を
実装時に
作成した ページ
作成した
ファイル構成
以下の
amp-soundcloud で
ファイル名 | 内容 |
---|---|
index.html | INDEX AMPページ |
index2.html | INDEX AMPページからリンクしているAMPページ |
offline.html | オフライン時に表示するページ |
serviceworker.html | ServiceWorkerを読み込むIframeページ |
sw.js | ServiceWorker |
AMP ページで ServiceWorker を 使用する ための 実装内容
AMP ページで
index.html
index.html内の
metaタグ内に、
以下の スクリプトを 記載する。 <script async custom-element="amp-install-serviceworker" src="https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js"></script>
amp-install-serviceworker タグで、
ServiceWorker の JavaScript と、 Iframeを 指定する <amp-install-serviceworker src="/amppwa/sw.js" data-iframe-src="https://examples.monotalk.xyz/amppwa/serviceworker.html" layout="nodisplay"> </amp-install-serviceworker>
serviceworker.html
serviceworker.html の
<!doctype html> <html> <head> <title>installing service worker</title> <script type="text/javascript"> var swsource = "https://examples.monotalk.xyz/amppwa/sw.js"; if("serviceWorker" in navigator) { navigator.serviceWorker.register(swsource).then(function(reg){ console.log('ServiceWorker scope: ', reg.scope); }).catch(function(err) { console.log('ServiceWorker registration failed: ', err); }); }; </script> </head> <body> </body> </html>
var swsource = "https://examples.monotalk.xyz/amppwa/sw.js";
では
sw.js
sw.js
は
importScripts('https://cdn.ampproject.org/sw/amp-sw.js'); AMP_SW.init({ linkPrefetchOptions: { maxAgeSecondsInCache: 1000 }, offlinePageOptions: { url: '/amppwa/offline.html', assets: [], } });
最小限の1line
の
importScripts('https://cdn.ampproject.org/sw/amp-sw.js'); AMP_SW.init();
amp-sw の 挙動に ついて
amp-sw
のREADME.md
、
以下、
ユースケースに ついて
以下は、README.md
に
httpレスポンスヘッダの
設定よりも 長い 期間、 stale-while-revalidate
でAMPスクリプトを キャッシュする。 有効な
表示済みの AMPドキュメントを キャッシュし、 不安定な ネットワーク状態の 場合にのみ 表示する。 指定したCache戦略で
ページに とって 重要な アセットを キャッシュする。 ユーザーアクセスした
ことのない ページに 移動した ときの ために、 オフラインページを キャッシュする。 AMPページからの
発信リンクを prefetchする。
5つの モジュールに 分かれている
以下、
AMP caching module
AMP のJavaScript 等の リソースを cacheする モジュールです。 Document caching module
AMP のHTML を cacheする モジュールです。 Asset caching module
画像/フォントなどの静的アセットを cacheする モジュールです。 Offline page module
表示したことのない ページを オフライン時に 表示した 際、 offline用の HTMLを 表示する モジュールです。 Link prefetch module
link rel=prefetch
をサポートしない ブラウザに プリフェッチ機能を 提供する モジュールです。
AMPのページは、 Cacheされると、 link rel=prefetch
の記述は 削除されてしまいます。
CacheされたHTMLでもprefetchの 機能を 実現する ために 提供されていると 考えられます。
AMP_SW.init() の 実装
AMP_SW.init() の
function init(config: ServiceWorkerConfiguration = {}) { ampCachingModule.init(); let fallbackOfflinePageUrl; if (config.offlinePageOptions) { fallbackOfflinePageUrl = config.offlinePageOptions.url; } const navRoute = documentCachingModule.init( config.documentCachingOptions, fallbackOfflinePageUrl, ); if (config.assetCachingOptions) { import(/* webpackChunkName: "optional-modules" */ '../asset-caching/index').then( async ({ AssetCachingAmpModule }) => { const assetCachingModule = new AssetCachingAmpModule(); await assetCachingModule.init( config.assetCachingOptions as AssetCachingOptions, ); }, ); } if (config.linkPrefetchOptions) { import(/* webpackChunkName: "optional-modules" */ '../link-prefetch/index').then( async ({ LinkPrefetchAmpModule }) => { const linkPrefetchModule = new LinkPrefetchAmpModule(); await linkPrefetchModule.init( config.linkPrefetchOptions as LinkPrefetchOptions, navRoute, ); }, ); } if (config.offlinePageOptions) { import(/* webpackChunkName: "optional-modules" */ '../offline-page/index').then( async ({ OfflinePageAmpSwModule }) => { const offlinePageModule = new OfflinePageAmpSwModule(); const offlinePageConfig: OfflinePageOptions = config.offlinePageOptions as OfflinePageOptions; await offlinePageModule.init( offlinePageConfig.url, offlinePageConfig.assets, ); }, ); } // Taking over the document self.addEventListener('install', function(e: ExtendableEvent) { const { skipWaiting } = self as ServiceWorkerGlobalScope; e.waitUntil(skipWaiting()); }); self.addEventListener('activate', async (e: ExtendableEvent) => { const { clients } = self as ServiceWorkerGlobalScope; e.waitUntil( clients.claim().then(async () => { // Cache current document if its AMP. const windowClients = await clients.matchAll({ type: 'window' }); return Promise.all( documentCachingModule.cacheAMPDocument(windowClients), ); }), ); }); }
ソースを
- AMP caching module
- Document caching module
以下は
- Asset caching module
- Offline page module
- Link prefetch module
サンプルを 動かして 気づいたこと
AMP と
いう プレフィックスが ついたCacheが 作られる
それぞれのモジュールに 対応する AMPと いう プレフィックスが ついた 名前で Cacheが 作られます。 Link prefetch module は
動作しない?
data-rel='prefetch'
をつけた リンクを Cache してくれるようなのですが、 特に 動作している 雰囲気は ありませんでした。
Google にCacheされた ページの 場合動作するのかもしれません。
参考
以下、
offline用の
自サイトの
以上です。
コメント