123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import { assert } from 'workbox-core/_private/assert.js';
- import { cacheNames } from 'workbox-core/_private/cacheNames.js';
- import { cacheWrapper } from 'workbox-core/_private/cacheWrapper.js';
- import { fetchWrapper } from 'workbox-core/_private/fetchWrapper.js';
- import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
- import { logger } from 'workbox-core/_private/logger.js';
- import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
- import { messages } from './utils/messages.js';
- import { cacheOkAndOpaquePlugin } from './plugins/cacheOkAndOpaquePlugin.js';
- import './_version.js';
- class StaleWhileRevalidate {
-
- constructor(options = {}) {
- this._cacheName = cacheNames.getRuntimeName(options.cacheName);
- this._plugins = options.plugins || [];
- if (options.plugins) {
- const isUsingCacheWillUpdate = options.plugins.some((plugin) => !!plugin.cacheWillUpdate);
- this._plugins = isUsingCacheWillUpdate ?
- options.plugins : [cacheOkAndOpaquePlugin, ...options.plugins];
- }
- else {
-
- this._plugins = [cacheOkAndOpaquePlugin];
- }
- this._fetchOptions = options.fetchOptions;
- this._matchOptions = options.matchOptions;
- }
-
- async handle({ event, request }) {
- const logs = [];
- if (typeof request === 'string') {
- request = new Request(request);
- }
- if (process.env.NODE_ENV !== 'production') {
- assert.isInstance(request, Request, {
- moduleName: 'workbox-strategies',
- className: 'StaleWhileRevalidate',
- funcName: 'handle',
- paramName: 'request',
- });
- }
- const fetchAndCachePromise = this._getFromNetwork({ request, event });
- let response = await cacheWrapper.match({
- cacheName: this._cacheName,
- request,
- event,
- matchOptions: this._matchOptions,
- plugins: this._plugins,
- });
- let error;
- if (response) {
- if (process.env.NODE_ENV !== 'production') {
- logs.push(`Found a cached response in the '${this._cacheName}'` +
- ` cache. Will update with the network response in the background.`);
- }
- if (event) {
- try {
- event.waitUntil(fetchAndCachePromise);
- }
- catch (error) {
- if (process.env.NODE_ENV !== 'production') {
- logger.warn(`Unable to ensure service worker stays alive when ` +
- `updating cache for '${getFriendlyURL(request.url)}'.`);
- }
- }
- }
- }
- else {
- if (process.env.NODE_ENV !== 'production') {
- logs.push(`No response found in the '${this._cacheName}' cache. ` +
- `Will wait for the network response.`);
- }
- try {
- response = await fetchAndCachePromise;
- }
- catch (err) {
- error = err;
- }
- }
- if (process.env.NODE_ENV !== 'production') {
- logger.groupCollapsed(messages.strategyStart('StaleWhileRevalidate', request));
- for (const log of logs) {
- logger.log(log);
- }
- messages.printFinalResponse(response);
- logger.groupEnd();
- }
- if (!response) {
- throw new WorkboxError('no-response', { url: request.url, error });
- }
- return response;
- }
-
- async _getFromNetwork({ request, event }) {
- const response = await fetchWrapper.fetch({
- request,
- event,
- fetchOptions: this._fetchOptions,
- plugins: this._plugins,
- });
- const cachePutPromise = cacheWrapper.put({
- cacheName: this._cacheName,
- request,
- response: response.clone(),
- event,
- plugins: this._plugins,
- });
- if (event) {
- try {
- event.waitUntil(cachePutPromise);
- }
- catch (error) {
- if (process.env.NODE_ENV !== 'production') {
- logger.warn(`Unable to ensure service worker stays alive when ` +
- `updating cache for '${getFriendlyURL(request.url)}'.`);
- }
- }
- }
- return response;
- }
- }
- export { StaleWhileRevalidate };
|