Skip to content

AppCache

此包已被弃用,因为此包所依赖的 applicationCache 已被弃用,并且在最新的浏览器上不可用。请考虑使用 Service Worker 作为替代品。

¥This package has been deprecated since applicationCache, which this package relies on, has been deprecated and is not available on the latest browsers. Plaese consider using Service Worker as an replacement.

appcache 包将 Meteor 应用的静态部分(客户端 Javascript、HTML、CSS 和图片)存储在浏览器的 应用缓存 中。要启用缓存,只需将 appcache 包添加到你的项目中。

¥The appcache package stores the static parts of a Meteor application (the client side Javascript, HTML, CSS, and images) in the browser's application cache. To enable caching simply add the appcache package to your project.

  • 用户首次访问 Meteor 应用并且应用已被缓存后,在后续访问中,网页加载速度会更快,因为浏览器可以从缓存中加载应用而无需先联系服务器。

    ¥Once a user has visited a Meteor application for the first time and the application has been cached, on subsequent visits the web page loads faster because the browser can load the application out of the cache without contacting the server first.

  • 当应用继续运行时,浏览器会在后台加载热代码推送。新代码完全加载后,浏览器可以快速切换到新代码。

    ¥Hot code pushes are loaded by the browser in the background while the app continues to run. Once the new code has been fully loaded the browser is able to switch over to the new code quickly.

  • 应用缓存允许即使浏览器没有互联网连接也能加载应用,因此可以离线使用该应用。

    ¥The application cache allows the application to be loaded even when the browser doesn't have an Internet connection, and so enables using the app offline.

(但请注意,appcache 包本身不会使数据离线可用:在离线加载的应用中,Meteor Collection 在客户端中将显示为空,直到互联网可用并且浏览器能够建立 DDP 连接)。

¥(Note however that the appcache package by itself doesn't make data available offline: in an application loaded offline, a Meteor Collection will appear to be empty in the client until the Internet becomes available and the browser is able to establish a DDP connection).

要为特定浏览器关闭 AppCache,请使用:

¥To turn AppCache off for specific browsers use:

js
Meteor.AppCache.config({
  chrome: false,
  firefox: false
});

可以启用或禁用的受支持浏览器包括但不限于 androidchromechromiumchromeMobileIOSfirefoxiemobileSafarisafari

¥The supported browsers that can be enabled or disabled include, but are not limited to, android, chrome, chromium, chromeMobileIOS, firefox, ie, mobileSafari and safari.

浏览器限制它们将放入应用缓存的数据量,这可能会因磁盘空间的可用量等因素而有所不同。不幸的是,如果你的应用超出限制,而不是完全禁用应用缓存并在线运行应用,浏览器将无法执行该缓存的特定更新,从而使你的用户运行旧代码。

¥Browsers limit the amount of data they will put in the application cache, which can vary due to factors such as how much disk space is free. Unfortunately if your application goes over the limit rather than disabling the application cache altogether and running the application online, the browser will instead fail that particular update of the cache, leaving your users running old code.

因此最好将缓存大小保持在 5MB 以下。如果缓存的资源总大小超过 5MB,appcache 包将在 Meteor 服务器控制台上打印警告。

¥Thus it's best to keep the size of the cache below 5MB. The appcache package will print a warning on the Meteor server console if the total size of the resources being cached is over 5MB.

appcache@1.2.5 开始,如果你需要更高级的逻辑来启用/禁用缓存,则可以使用根据每个请求进行评估的 enableCallback 选项。例如:

¥Starting from appcache@1.2.5, if you need more advanced logic to enable/disable the cache, you can use the enableCallback option that is evaluated on a per-request basis. For example:

js
// Enable offline mode using a value from database and certificate validation
Meteor.AppCache.config({
  // This option is available starting from appcache@1.2.4
  enableCallback: () => {
    if (!getSettingsFromDb("public.appcache_enabled")) {
      return false;
    }

    const validation = validateClientCert({
      clientCertPayload: req.headers["x-client-cert"],
      url: req.url.href,
    });

    return validation.passed;
  },
});

如果你的文件太大而无法放入缓存中,则可以通过 URL 前缀禁用缓存。例如,

¥If you have files too large to fit in the cache you can disable caching by URL prefix. For example,

js
Meteor.AppCache.config({ onlineOnly: ['/online/'] });

导致 public/online 目录中的文件不被缓存,因此它们只能在线使用。然后,你可以将大文件移动到该目录中,并在新 URL 中引用它们:

¥causes files in your public/online directory to not be cached, and so they will only be available online. You can then move your large files into that directory and refer to them at the new URL:

html
<img src="/online/bigimage.jpg">

如果你不想移动文件,可以使用文件名本身作为 URL 前缀:

¥If you'd prefer not to move your files, you can use the file names themselves as the URL prefix:

js
Meteor.AppCache.config({
  onlineOnly: [
    '/bigimage.jpg',
    '/largedata.json'
  ]
});

但请记住,由于排除是通过前缀进行的(这是应用缓存清单的限制),因此排除 /largedata.json 也会排除 /largedata.json.orig/largedata.json/file1 等 URL。

¥though keep in mind that since the exclusion is by prefix (this is a limitation of the application cache manifest), excluding /largedata.json will also exclude such URLs as /largedata.json.orig and /largedata.json/file1.

有关 Meteor 如何与应用缓存交互的更多信息,请参阅 Meteor wiki 中的 AppCache 页面

¥For more information about how Meteor interacts with the application cache, see the AppCache page in the Meteor wiki.