1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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 { logger } from 'workbox-core/_private/logger.js';
- import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
- import { messages } from './utils/messages.js';
- import './_version.js';
- class CacheOnly {
-
- constructor(options = {}) {
- this._cacheName = cacheNames.getRuntimeName(options.cacheName);
- this._plugins = options.plugins || [];
- this._matchOptions = options.matchOptions;
- }
-
- async handle({ event, request }) {
- if (typeof request === 'string') {
- request = new Request(request);
- }
- if (process.env.NODE_ENV !== 'production') {
- assert.isInstance(request, Request, {
- moduleName: 'workbox-strategies',
- className: 'CacheOnly',
- funcName: 'makeRequest',
- paramName: 'request',
- });
- }
- const response = await cacheWrapper.match({
- cacheName: this._cacheName,
- request,
- event,
- matchOptions: this._matchOptions,
- plugins: this._plugins,
- });
- if (process.env.NODE_ENV !== 'production') {
- logger.groupCollapsed(messages.strategyStart('CacheOnly', request));
- if (response) {
- logger.log(`Found a cached response in the '${this._cacheName}'` +
- ` cache.`);
- messages.printFinalResponse(response);
- }
- else {
- logger.log(`No response found in the '${this._cacheName}' cache.`);
- }
- logger.groupEnd();
- }
- if (!response) {
- throw new WorkboxError('no-response', { url: request.url });
- }
- return response;
- }
- }
- export { CacheOnly };
|