JavaScriptService worker / Workbox

Try precache files on Workbox documents

At last post, I try to getting start Workbox. Today, I'll try to use precache feature by Workbox.Official […]

広告ここから
広告ここまで

At last post, I try to getting start Workbox. Today, I'll try to use precache feature by Workbox.
Official docs: Precache Files

Current status

For now, we can cache some files by Workbox. But when you access the page as offline, your page does not showing.

Let's start to show the page in offline.

Define precached files

Workbox can define precache files by workbox.precaching.precacheAndRoute().So we update sw.js to add following codes.

importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js');
if (workbox) {
  registerRoutes()
  workbox.precaching.precacheAndRoute([
    { url: '/scripts/app.js', revision: '383676' },
    { url: '/css/style.css', revision: '383676' },
    { url: '/index.html', revision: '383676' },
  ]);
} else {
  console.log('Oh, workbox did not load')
}

Access the page by offline

After update sw.js,

let's reload the page.(You have to remove browser cache).After reload the page, it will be precached. And when you reload as offline, you can still see the page.

Why the style.css and app.js happened error ? Probably the reason is that these files cache strategies are not cacheFirst. I think the workbox try to get these files from Network at first. And when fail to get them, they will get from precache.

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts