Service Worker 配置
Service worker configuration
前提条件
Prerequisites
对下列知识有基本的了解:
A basic understanding of the following:
配置文件 ngsw-config.json
指定了 Angular Service Worker 应该缓存哪些文件和数据的 URL,以及如何更新缓存的文件和数据。 Angular CLI 会在 ng build
期间处理配置文件。 如果想手动处理,你可以使用 ngsw-config
工具(这里的 <project-name>
就是要构建的项目名):
The ngsw-config.json
configuration file specifies which files and data URLs the Angular service worker should cache and how it should update the cached files and data. The Angular CLI processes the configuration file during ng build
. Manually, you can process it with the ngsw-config
tool (where <project-name>
is the name of the project being built):
./node_modules/.bin/ngsw-config ./dist/<project-name> ./ngsw-config.json [/base/href]
该配置文件使用 JSON 格式。 所有文件路径都必须以 /
开头,也就是相应的部署目录 —— 在 CLI 项目中的它通常是 dist/<project-name>
。
The configuration file uses the JSON format. All file paths must begin with /
, which corresponds to the deployment directory—usually dist/<project-name>
in CLI projects.
如无特别说明,这些模式都使用受限的 glob 格式:
Unless otherwise noted, patterns use a limited glob format:
**
匹配 0 到多段路径。**
matches 0 or more path segments.*
匹配 0 个或更多个除/
之外的字符。*
matches 0 or more characters excluding/
.?
匹配除/
之外的一个字符。?
matches exactly one character excluding/
.!
前缀表示该模式是反的,也就是说只包含与该模式不匹配的文件。The
!
prefix marks the pattern as being negative, meaning that only files that don't match the pattern will be included.
范例模式:
Example patterns:
/**/*.html
指定所有 HTML 文件。/**/*.html
specifies all HTML files./*.html
仅指定根目录下的 HTML 文件。/*.html
specifies only HTML files in the root.!/**/*.map
排除了所有源码映射文件。!/**/*.map
exclude all sourcemaps.
下面讲讲配置文件中的每一节。
Each section of the configuration file is described below.
appData
本节允许你传递用来描述这个特定应用版本的任何数据。 SwUpdate
服务会在更新通知中包含这些数据。 许多应用会使用本节来提供 UI 弹窗时要显示的附加信息,以通知用户有可用的更新。
This section enables you to pass any data you want that describes this particular version of the application. The SwUpdate
service includes that data in the update notifications. Many applications use this section to provide additional information for the display of UI popups, notifying users of the available update.
index
指定用来充当索引页的文件以满足导航请求。通常是 /index.html
。
Specifies the file that serves as the index page to satisfy navigation requests. Usually this is /index.html
.
assetGroups
资产(Assets)是与应用一起更新的应用版本的一部分。 它们可以包含从页面的同源地址加载的资源以及从 CDN 和其它外部 URL 加载的第三方资源。 由于在构建时可能没法提前知道所有这些外部 URL,因此也可以指定 URL 的模式。
Assets are resources that are part of the application version that update along with the application. They can include resources loaded from the page's origin as well as third-party resources loaded from CDNs and other external URLs. As not all such external URLs may be known at build time, URL patterns can be matched.
该字段包含一个资产组的数组,每个资产组中会定义一组资产资源和它们的缓存策略。
This field contains an array of asset groups, each of which defines a set of asset resources and the policy by which they are cached.
{
"assetGroups": [
{
...
},
{
...
}
]
}
当 ServiceWorker 处理请求时,它将按照资源组在 ngsw-config.json
中出现的顺序对其进行检查。与所请求的资源匹配的第一个资源组将处理该请求。
When the ServiceWorker handles a request, it checks asset groups in the order in which they appear in ngsw-config.json
. The first asset group that matches the requested resource handles the request.
建议将更具体的资源组放在列表中较高的位置。比如,与 /foo.js
匹配的资源组应出现在与 *.js
匹配的资源组之前。
It is recommended that you put the more specific asset groups higher in the list. For example, an asset group that matches /foo.js
should appear before one that matches *.js
.
每个资产组都会指定一组资源和一个管理它们的策略。 此策略用来决定何时获取资源以及当检测到更改时该怎么做。
Each asset group specifies both a group of resources and a policy that governs them. This policy determines when the resources are fetched and what happens when changes are detected.
这些资产组会遵循下面的 Typescript 接口:
Asset groups follow the Typescript interface shown here:
interface AssetGroup {
name: string;
installMode?: 'prefetch' | 'lazy';
updateMode?: 'prefetch' | 'lazy';
resources: {
files?: string[];
urls?: string[];
};
cacheQueryOptions?: {
ignoreSearch?: boolean;
};
}
name
name
是强制性的。它用来标识该配置文件版本中这个特定的资产组。
A name
is mandatory. It identifies this particular group of assets between versions of the configuration.
installMode
installMode
决定了这些资源最初的缓存方式。installMode
可以取如下两个值之一:
The installMode
determines how these resources are initially cached. The installMode
can be either of two values:
prefetch
告诉 Angular Service Worker 在缓存当前版本的应用时要获取每一个列出的资源。 这是个带宽密集型的模式,但可以确保这些资源在请求时可用,即使浏览器正处于离线状态。prefetch
tells the Angular service worker to fetch every single listed resource while it's caching the current version of the application. This is bandwidth-intensive but ensures resources are available whenever they're requested, even if the browser is currently offline.lazy
不会预先缓存任何资源。相反,Angular Service Worker 只会缓存它收到请求的资源。 这是一种按需缓存模式。永远不会请求的资源也永远不会被缓存。 这对于像为不同分辨率提供的图片之类的资源很有用,那样 Service Worker 就只会为特定的屏幕和设备方向缓存正确的资源。lazy
does not cache any of the resources up front. Instead, the Angular service worker only caches resources for which it receives requests. This is an on-demand caching mode. Resources that are never requested will not be cached. This is useful for things like images at different resolutions, so the service worker only caches the correct assets for the particular screen and orientation.
默认为 prefetch
。
Defaults to prefetch
.
updateMode
对于已经存在于缓存中的资源,updateMode
会决定在发现了新版本应用后的缓存行为。 自上一版本以来更改过的所有组中资源都会根据 updateMode
进行更新。
For resources already in the cache, the updateMode
determines the caching behavior when a new version of the application is discovered. Any resources in the group that have changed since the previous version are updated in accordance with updateMode
.
prefetch
会告诉 Service Worker 立即下载并缓存更新过的资源。prefetch
tells the service worker to download and cache the changed resources immediately.lazy
告诉 Service Worker 不要缓存这些资源,而是先把它们看作未被请求的,等到它们再次被请求时才进行更新。lazy
这个updateMode
只有在installMode
也同样是lazy
时才有效。lazy
tells the service worker to not cache those resources. Instead, it treats them as unrequested and waits until they're requested again before updating them. AnupdateMode
oflazy
is only valid if theinstallMode
is alsolazy
.
其默认值为 installMode
的值。
Defaults to the value installMode
is set to.
resources
本节描述要缓存的资源,分为如下几组:
This section describes the resources to cache, broken up into the following groups:
files
列出了与dist
目录中的文件相匹配的模式。它们可以是单个文件也可以是能匹配多个文件的类似 glob 的模式。files
lists patterns that match files in the distribution directory. These can be single files or glob-like patterns that match a number of files.urls
包括要在运行时进行匹配的 URL 和 URL 模式。 这些资源不是直接获取的,也没有内容散列,但它们会根据 HTTP 标头进行缓存。 这对于像 Google Fonts 服务这样的 CDN 非常有用。
(不支持 glob 的逆模式,?
将会按字面匹配;也就是说它不会匹配除了?
之外的任何字符。)urls
includes both URLs and URL patterns that will be matched at runtime. These resources are not fetched directly and do not have content hashes, but they will be cached according to their HTTP headers. This is most useful for CDNs such as the Google Fonts service.
(Negative glob patterns are not supported and?
will be matched literally; that is, it will not match any character other than?
.)
cacheQueryOptions
这些选项用来修改对请求进行匹配的行为。它们会传给浏览器的 Cache#match
函数。详情参阅 MDN。目前,只支持下列选项:
These options are used to modify the matching behavior of requests. They are passed to the browsers Cache#match
function. See MDN for details. Currently, only the following options are supported:
ignoreSearch
: 忽略查询参数。默认为false
。ignoreSearch
: Ignore query parameters. Defaults tofalse
.
dataGroups
与这些资产性(asset)资源不同,数据请求不会随应用一起版本化。 它们会根据手动配置的策略进行缓存,这些策略对 API 请求和所依赖的其它数据等情况会更有用。
Unlike asset resources, data requests are not versioned along with the application. They're cached according to manually-configured policies that are more useful for situations such as API requests and other data dependencies.
本字段包含一个数据组的数组,其中的每一个条目都定义了一组数据资源以及对它们的缓存策略。
This field contains an array of data groups, each of which defines a set of data resources and the policy by which they are cached.
{
"dataGroups": [
{
...
},
{
...
}
]
}
当 ServiceWorker 处理请求时,它将按照数据组在 ngsw-config.json
中出现的顺序对其进行检查。与所请求的资源匹配的第一个数据组将处理该请求。
When the ServiceWorker handles a request, it checks data groups in the order in which they appear in ngsw-config.json
. The first data group that matches the requested resource handles the request.
建议将更具体的数据组放在列表中较高的位置。比如,与 /api/foo.json
匹配的数据组应出现在与 /api/*.json
匹配的数据组之前。
It is recommended that you put the more specific data groups higher in the list. For example, a data group that matches /api/foo.json
should appear before one that matches /api/*.json
.
数据组遵循下列 TypeScript 接口:
Data groups follow this Typescript interface:
export interface DataGroup {
name: string;
urls: string[];
version?: number;
cacheConfig: {
maxSize: number;
maxAge: string;
timeout?: string;
strategy?: 'freshness' | 'performance';
};
cacheQueryOptions?: {
ignoreSearch?: boolean;
};
}
name
和 assetGroups
下类似,每个数据组也都有一个 name
,用作它的唯一标识。
Similar to assetGroups
, every data group has a name
which uniquely identifies it.
urls
一个 URL 模式的列表。匹配这些模式的 URL 将会根据该数据组的策略进行缓存。只有非修改型的请求(GET 和 HEAD)才会进行缓存。
A list of URL patterns. URLs that match these patterns are cached according to this data group's policy. Only non-mutating requests (GET and HEAD) are cached.
(不支持 glob 中的否定模式)。
Negative glob patterns are not supported.
?
只做字面匹配,也就是说,它只能匹配?
字符。?
is matched literally; that is, it matches only the character?
.
version
API 有时可能会以不向后兼容的方式更改格式。 新版本的应用可能与旧的 API 格式不兼容,因此也就与该 API 中目前已缓存的资源不兼容。
Occasionally APIs change formats in a way that is not backward-compatible. A new version of the application may not be compatible with the old API format and thus may not be compatible with existing cached resources from that API.
version
提供了一种机制,用于指出这些被缓存的资源已经通过不向后兼容的方式进行了更新,并且旧的缓存条目(即来自以前版本的缓存条目)应该被丢弃。
version
provides a mechanism to indicate that the resources being cached have been updated in a backwards-incompatible way, and that the old cache entries—those from previous versions—should be discarded.
version
是个整型字段,默认为 0
。
version
is an integer field and defaults to 1
.
cacheConfig
本节定义了对匹配上的请求进行缓存时的策略。
This section defines the policy by which matching requests will be cached.
maxSize
(必需)缓存的最大条目数或响应数。开放式缓存可以无限增长,并最终超过存储配额,建议适时清理。
(required) The maximum number of entries, or responses, in the cache. Open-ended caches can grow in unbounded ways and eventually exceed storage quotas, calling for eviction.
maxAge
(必需)maxAge
参数表示在响应因失效而要清除之前允许在缓存中留存的时间。maxAge
是一个表示持续时间的字符串,可使用以下单位作为后缀:
(required) The maxAge
parameter indicates how long responses are allowed to remain in the cache before being considered invalid and evicted. maxAge
is a duration string, using the following unit suffixes:
d
:天数d
: daysh
:小时数h
: hoursm
:分钟数m
: minutess
:秒数s
: secondsu
:微秒数u
: milliseconds
比如,字符串 3d12h
规定此内容最多缓存三天半。
For example, the string 3d12h
will cache content for up to three and a half days.
timeout
这个表示持续时间的字符串用于指定网络超时时间。 如果配置了网络超时时间,Angular Service Worker 就会先等待这么长时间再使用缓存。timeout
是一个表示持续时间的字符串,使用下列后缀单位:
This duration string specifies the network timeout. The network timeout is how long the Angular service worker will wait for the network to respond before using a cached response, if configured to do so. timeout
is a duration string, using the following unit suffixes:
d
:天d
: daysh
:小时h
: hoursm
:分钟m
: minutess
:秒s
: secondsu
:毫秒u
: milliseconds
比如,字符串 5s30u
将会被翻译成 5 秒零 30 毫秒的网络超时。
For example, the string 5s30u
will translate to five seconds and 30 milliseconds of network timeout.
strategy
Angular Service Worker 可以使用两种缓存策略之一来获取数据资源。
The Angular service worker can use either of two caching strategies for data resources.
performance
,默认值,为尽快给出响应而优化。如果缓存中存在某个资源,则使用这个缓存版本,而不再发起网络请求。 它允许资源有一定的陈旧性(取决于maxAge
)以换取更好的性能。适用于那些不经常改变的资源,例如用户头像。performance
, the default, optimizes for responses that are as fast as possible. If a resource exists in the cache, the cached version is used, and no network request is made. This allows for some staleness, depending on themaxAge
, in exchange for better performance. This is suitable for resources that don't change often; for example, user avatar images.freshness
为数据的即时性而优化,优先从网络获取请求的数据。只有当网络超时时,请求才会根据timeout
的设置回退到缓存中。这对于那些频繁变化的资源很有用,例如账户余额。freshness
optimizes for currency of data, preferentially fetching requested data from the network. Only if the network times out, according totimeout
, does the request fall back to the cache. This is useful for resources that change frequently; for example, account balances.
你还可以模拟第三种策略 staleWhileRevalidate,它会返回缓存的数据(如果可用),但是也会在后台从网络上获取新数据,以供下次使用。 要使用本策略,请在 cacheConfig
中把 strategy
设置为 freshness
,并且把 timeout
设置为 0u
。
You can also emulate a third strategy, staleWhileRevalidate, which returns cached data (if available), but also fetches fresh data from the network in the background for next time. To use this strategy set strategy
to freshness
and timeout
to 0u
in cacheConfig
.
本质上说,它会做如下工作:
This will essentially do the following:
首先尝试从网络上获取。
Try to fetch from the network first.
如果网络请求没有在 0ms 内(也就是立刻)完成,就用缓存做为后备(忽略缓存有效期)。
If the network request does not complete after 0ms (that is, immediately), fall back to the cache (ignoring cache age).
一旦网络请求完成,就更新缓存,以供将来的请求使用。
Once the network request completes, update the cache for future requests.
如果指定的资源在缓存中不存在,总是等待网络请求。
If the resource does not exist in the cache, wait for the network request anyway.
cacheQueryOptions
详情参阅 assetGroups。
See assetGroups for details.
navigationUrls
这个可选节让你可以指定一个自定义的 URL 列表,它们都会被重定向到索引文件。
This optional section enables you to specify a custom list of URLs that will be redirected to the index file.
处理导航请求
Handling navigation requests
对于没有匹配上任何 asset
或 data
组的导航请求,ServiceWorker 会把它们重定向到指定的索引文件。下列请求将会视为导航请求:
The ServiceWorker will redirect navigation requests that don't match any asset
or data
group to the specified index file. A request is considered to be a navigation request if:
它的模式是
navigation
。Its mode is
navigation
.它接受
text/html
响应(根据Accept
头的值决定)。It accepts a
text/html
response (as determined by the value of theAccept
header).它的 URL 符合特定的条件(稍后讲)。
Its URL matches certain criteria (see below).
默认情况下,这些条件是:
By default, these criteria are:
URL 的最后一段路径中不能包含文件扩展名(比如
.
)。The URL must not contain a file extension (that is, a
.
) in the last path segment.URL 中不能包含
__
。The URL must not contain
__
.
要配置浏览请求是否发送到网络,请参阅 navigationRequestStrategy 部分。
To configure whether navigation requests are sent through to the network or not, see the navigationRequestStrategy section.
匹配导航请求的 URL
Matching navigation request URLs
虽然这些默认条件在大多数情况下都挺好用,不过有时还是要配置一些不同的规则。比如,你可能希望忽略一些特定的路由(它们可能不是 Angular 应用的一部分),而是把它们透传给服务器。
While these default criteria are fine in most cases, it is sometimes desirable to configure different rules. For example, you may want to ignore specific routes (that are not part of the Angular app) and pass them through to the server.
该字段包含一个将要在运行期间匹配的 URL 和 类似 glob 的 URL 模式。 它既可以包含正向模式也可以包含反向模式(比如用 !
开头的模式)。
This field contains an array of URLs and glob-like URL patterns that will be matched at runtime. It can contain both negative patterns (that is, patterns starting with !
) and non-negative patterns and URLs.
只有那些能匹配任意正向 URL 或 URL 模式并且不匹配任何一个反向模式的 URL 才会视为导航请求。当匹配时,这些 URL 查询将会被忽略。
Only requests whose URLs match any of the non-negative URLs/patterns and none of the negative ones will be considered navigation requests. The URL query will be ignored when matching.
如果省略了该字段,它的默认值是:
If the field is omitted, it defaults to:
[
'/**', // Include all URLs.
'!/**/*.*', // Exclude URLs to files.
'!/**/*__*', // Exclude URLs containing `__` in the last segment.
'!/**/*__*/**', // Exclude URLs containing `__` in any other segment.
]
navigationRequestStrategy
通过此可选属性,你可以配置服务工作者如何处理导航请求:
This optional property enables you to configure how the service worker handles navigation requests:
{
"navigationRequestStrategy": "freshness"
}
可能的值:
Possible values:
'performance'
:默认设置。提供指定的索引文件,它通常会被缓存。'performance'
: The default setting. Serves the specified index file, which is typically cached.'freshness'
:将请求透传到网络,并在脱机时回退到performance
模式。当服务器在用 HTTP 重定向(3xx 状态代码)将导航请求重定向到其他位置时,此值很有用。使用此值的原因包括:'freshness'
: Passes the requests through to the network and falls back to theperformance
behavior when offline. This value is useful when the server redirects the navigation requests elsewhere using an HTTP redirect (3xx status code). Reasons for using this value include:当应用尚未处理身份验证时,重定向到身份验证网站。
Redirecting to an authentication website when authentication is not handled by the application.
重定向特定的 URL,以免在网站重新设计后破坏现有的链接/书签。
Redirecting specific URLs to avoid breaking existing links/bookmarks after a website redesign.
当页面暂时关闭时,重定向到其他网站,例如服务器状态页。
Redirecting to a different website, such as a server-status page, while a page is temporarily down.
freshness
策略通常会导致向服务器发送更多请求,这可能会增加响应延迟。建议你尽可能使用默认的性能策略。
The freshness
strategy usually results in more requests sent to the server, which can increase response latency. It is recommended that you use the default performance strategy whenever possible.