Workbox 似乎没有从 webpack 构建中预先缓存块

编程入门 行业动态 更新时间:2024-10-28 10:21:21
本文介绍了Workbox 似乎没有从 webpack 构建中预先缓存块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Webpack 编译我的网站和

不是最好的 UI IMO.

I'm using Webpack to compile my site and the Workbox Webpack Plugin to compile the service worker.

Despite the manifest created by Workbox containing a link to an array of correct chunk names (with the correct hashes for that particular build), the Chrome DevTools don't seem to include the chunks in the precache list (DevTools > Application > Cache Storage).

Here is my configuration for the plugin:

new GenerateSW({ swDest: '../service-worker.js', globDirectory: 'priv/static', globPatterns: ['**/*.{js,css,png,svg,jpg,gif,woff2}'], runtimeCaching: [ { urlPattern: /^https:\/\/js.intercomcdn\/[a-zA-Z0-9-/_.]*(js|woff)/, handler: 'NetworkFirst' }, { urlPattern: /^https:\/\/fonts\.googleapis\/, handler: 'NetworkFirst', options: { cacheName: 'google-fonts-stylesheets' } }, { urlPattern: /^https:\/\/fonts\.gstatic\/, handler: 'CacheFirst', options: { cacheName: 'google-fonts-webfonts', cacheableResponse: { statuses: [0, 200] }, expiration: { maxEntries: 5, maxAgeSeconds: 60 * 60 * 24 * 365, // one year } } }, { urlPattern: /^https:\/\/logo.clearbit/, handler: 'NetworkFirst' }, { urlPattern: /^https:\/\/www.gravatar\/avatar\//, handler: 'NetworkFirst' } ] })

There are warnings in the shell about not needing to include globDirectory and globPatterns, as Webpack already keeps track of files when its compiling. However, as per the advice here, caching of additional assets which Webpack is not aware of should be done through this configuration option. I'm not using webpack-dev-server, and I'm serving the site through the Phoenix framework.

Looking at the /service-worker.js generated from Workbox, it is importing a manifest which does contain references to the correct chunks. But they're just not being loaded into the precache in Cache Storage:

importScripts( "/js/precache-manifest.94f30538f0c03a9278244eabf2ce9e52.js" // This points to a manifest with the correct chunk names/hashes );

It's probably worth noting that the Network tab in DevTools does say (ServiceWorker) in the Size column of the chunk names. But how am I to trust this when those scripts don't appear in the Cache Storage?

What's even stranger is if I remove the references to globDirectory and globPatterns, the Webpack chunk files do appear in the Cache Storage. The problem with this, though, is that I have assets that Webpack is unaware of that I want to be precached using Workbox.

Used plugin version is 4.x.x. Later versions of the plugin do not allow params globDirectory and globPatterns.

解决方案

As per Jeff's comment on the GitHub issue, this was due to a UI confusion on ChromeDev Tools which paginates the Cache Storage Results.

If the total number happens to be greater than 50, you'll need to use the arrows to page through entries, 50 at a time.

Here are the aforementioned inconspicuous page buttons:

Not the best UI IMO.

更多推荐

Workbox 似乎没有从 webpack 构建中预先缓存块

本文发布于:2023-07-04 06:36:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1019721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   Workbox   webpack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!