【问题标题】:PWA Best Practice: Web or Local Fonts?PWA 最佳实践:Web 字体还是本地字体?
【发布时间】:2019-04-30 16:49:10
【问题描述】:

我正在构建一个 preact PWA(渐进式 Web 应用程序),我不知道是否应该将字体保留在外部以减小应用程序大小,还是将字体保留在本地以改善离线查看?最佳做法是什么?

【问题讨论】:

    标签: caching fonts progressive-web-apps preact


    【解决方案1】:

    2021 年 2 月更新:浏览器通常朝着 partitioning the HTTP cache 的方向发展,这意味着类似 CDN 的 URL(例如 Google 字体)不太可能导致缓存由于之前请求相同 URL 的不相关 Web 应用程序而导致的点击。这消除了支持使用 CDN 字体的常见论点。像 fontsource 这样的项目可以自动发送开源字体的本地副本,虽然这需要更多的工作,但如果你最关心性能,我可能会选择它。

    我不认为使用第三方或第一方托管的网络字体会根据您的要求有所不同。可以在 service worker 中使用 Cache Storage API 来缓存一般的第一方或第三方请求,包括 Web 字体请求,并在离线时使用这些缓存的资源。

    有一些加载网络字体的一般最佳实践,Zach Leatherman 的 this article 是我的首选参考。不过,这些技术并非专门用于构建具有离线功能的 PWA。

    【讨论】:

      【解决方案2】:

      首选在文件夹/fonts/中的根网站文件中托管字体文件

      我们不得不使用来自https://www.dafont.com/ 的字体,它提供了一个.ttf 文件,因此需要使用任何免费的转换器工具将其更改为.woff 文件,在我的例子中:https://convertio.co/ttf-woff/ 帮助了.

      然后在你的 CSS 中声明新字体并在需要的地方使用:

      @font-face {
          font-family: 'Custom Font';
          font-weight: 400;
          font-style: normal;
          font-display: swap;
          /* Read next point */
          unicode-range: U+000-5FF;
          /* Download only latin glyphs */
          src:    local('Custom Font'),
                  url('/fonts/custom-font.woff') format('woff');
      }
      h1, h2, h3, h4, h5 {
          font-family: 'Custom Font', 'Helvetica', 'Arial', 'sans-serif';
      }
      

      我们的 PWA 需要一个 service worker 来在本地存储文件以用于离线模式,因此使用 JS 我们调用service-worker.js,它位于网站文件的根目录:

      // When document is ready.
      document.addEventListener('DOMContentLoaded', function () {
          // Steps followed from Google developer codelabs:
          // https://codelabs.developers.google.com/codelabs/offline/#5
          var registerServiceWorker = function registerServiceWorker() {
              if('serviceWorker' in navigator) {
                  navigator.serviceWorker.register('/service-worker.js');
              }
          }
      
          registerServiceWorker();
      });
      

      在我们的/service-worker.js 中,一个基本网站需要所有这些文件:

      self.addEventListener('install', function(e) {
          e.waitUntil(
              caches.open('my-website-name').then(function(cache) {
                  return cache.addAll([
                      '/',
                      '/favicon.ico',
                      '/manifest.json',
                      '/fonts/custom-font.woff',
                      '/images/favicon/android-chrome-192x192.png',
                      '/images/favicon/android-chrome-512x512.png',
                      '/images/favicon/apple-touch-icon.png',
                      '/images/favicon/favicon-16x16.png',
                      '/images/favicon/favicon-32x32.png',
                      '/images/favicon/maskable_icon.png',
                      '/images/favicon/monochrome_icon.png'
                  ]);
              })
          );
      });
      self.addEventListener('fetch', function(event) {
          event.respondWith(
              caches.match(event.request).then(function(response) {
                  return response || fetch(event.request);
              })
          );
      });
      

      确保为这个 /service-worker.js 文件更新 my-website-name,并通过添加 PWA 正常脱机工作所需的所有其他网站文件来更新 cache.addAll 数组。

      这基本上是为 PWA 加载自定义字体文件的所有代码参考。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 2011-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多