123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- import { assert } from 'workbox-core/_private/assert.js';
- import { cacheNames } from 'workbox-core/_private/cacheNames.js';
- import { dontWaitFor } from 'workbox-core/_private/dontWaitFor.js';
- import { getFriendlyURL } from 'workbox-core/_private/getFriendlyURL.js';
- import { logger } from 'workbox-core/_private/logger.js';
- import { registerQuotaErrorCallback } from 'workbox-core/registerQuotaErrorCallback.js';
- import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
- import { CacheExpiration } from './CacheExpiration.js';
- import './_version.js';
- class ExpirationPlugin {
-
- constructor(config = {}) {
-
- this.cachedResponseWillBeUsed = async ({ event, request, cacheName, cachedResponse, }) => {
- if (!cachedResponse) {
- return null;
- }
- const isFresh = this._isResponseDateFresh(cachedResponse);
-
-
- const cacheExpiration = this._getCacheExpiration(cacheName);
- dontWaitFor(cacheExpiration.expireEntries());
-
-
- const updateTimestampDone = cacheExpiration.updateTimestamp(request.url);
- if (event) {
- try {
- event.waitUntil(updateTimestampDone);
- }
- catch (error) {
- if (process.env.NODE_ENV !== 'production') {
-
- if ('request' in event) {
- logger.warn(`Unable to ensure service worker stays alive when ` +
- `updating cache entry for ` +
- `'${getFriendlyURL(event.request.url)}'.`);
- }
- }
- }
- }
- return isFresh ? cachedResponse : null;
- };
-
- this.cacheDidUpdate = async ({ cacheName, request, }) => {
- if (process.env.NODE_ENV !== 'production') {
- assert.isType(cacheName, 'string', {
- moduleName: 'workbox-expiration',
- className: 'Plugin',
- funcName: 'cacheDidUpdate',
- paramName: 'cacheName',
- });
- assert.isInstance(request, Request, {
- moduleName: 'workbox-expiration',
- className: 'Plugin',
- funcName: 'cacheDidUpdate',
- paramName: 'request',
- });
- }
- const cacheExpiration = this._getCacheExpiration(cacheName);
- await cacheExpiration.updateTimestamp(request.url);
- await cacheExpiration.expireEntries();
- };
- if (process.env.NODE_ENV !== 'production') {
- if (!(config.maxEntries || config.maxAgeSeconds)) {
- throw new WorkboxError('max-entries-or-age-required', {
- moduleName: 'workbox-expiration',
- className: 'Plugin',
- funcName: 'constructor',
- });
- }
- if (config.maxEntries) {
- assert.isType(config.maxEntries, 'number', {
- moduleName: 'workbox-expiration',
- className: 'Plugin',
- funcName: 'constructor',
- paramName: 'config.maxEntries',
- });
- }
- if (config.maxAgeSeconds) {
- assert.isType(config.maxAgeSeconds, 'number', {
- moduleName: 'workbox-expiration',
- className: 'Plugin',
- funcName: 'constructor',
- paramName: 'config.maxAgeSeconds',
- });
- }
- }
- this._config = config;
- this._maxAgeSeconds = config.maxAgeSeconds;
- this._cacheExpirations = new Map();
- if (config.purgeOnQuotaError) {
- registerQuotaErrorCallback(() => this.deleteCacheAndMetadata());
- }
- }
-
- _getCacheExpiration(cacheName) {
- if (cacheName === cacheNames.getRuntimeName()) {
- throw new WorkboxError('expire-custom-caches-only');
- }
- let cacheExpiration = this._cacheExpirations.get(cacheName);
- if (!cacheExpiration) {
- cacheExpiration = new CacheExpiration(cacheName, this._config);
- this._cacheExpirations.set(cacheName, cacheExpiration);
- }
- return cacheExpiration;
- }
-
- _isResponseDateFresh(cachedResponse) {
- if (!this._maxAgeSeconds) {
-
- return true;
- }
-
-
-
- const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
- if (dateHeaderTimestamp === null) {
-
- return true;
- }
-
-
- const now = Date.now();
- return dateHeaderTimestamp >= now - this._maxAgeSeconds * 1000;
- }
-
- _getDateHeaderTimestamp(cachedResponse) {
- if (!cachedResponse.headers.has('date')) {
- return null;
- }
- const dateHeader = cachedResponse.headers.get('date');
- const parsedDate = new Date(dateHeader);
- const headerTime = parsedDate.getTime();
-
-
- if (isNaN(headerTime)) {
- return null;
- }
- return headerTime;
- }
-
- async deleteCacheAndMetadata() {
-
-
- for (const [cacheName, cacheExpiration] of this._cacheExpirations) {
- await self.caches.delete(cacheName);
- await cacheExpiration.delete();
- }
-
- this._cacheExpirations = new Map();
- }
- }
- export { ExpirationPlugin };
|