1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { assert } from 'workbox-core/_private/assert.js';
- import { logger } from 'workbox-core/_private/logger.js';
- import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
- import { Strategy } from './Strategy.js';
- import { messages } from './utils/messages.js';
- import './_version.js';
- class CacheFirst extends Strategy {
-
- async _handle(request, handler) {
- const logs = [];
- if (process.env.NODE_ENV !== 'production') {
- assert.isInstance(request, Request, {
- moduleName: 'workbox-strategies',
- className: this.constructor.name,
- funcName: 'makeRequest',
- paramName: 'request',
- });
- }
- let response = await handler.cacheMatch(request);
- let error = undefined;
- if (!response) {
- if (process.env.NODE_ENV !== 'production') {
- logs.push(`No response found in the '${this.cacheName}' cache. ` +
- `Will respond with a network request.`);
- }
- try {
- response = await handler.fetchAndCachePut(request);
- }
- catch (err) {
- if (err instanceof Error) {
- error = err;
- }
- }
- if (process.env.NODE_ENV !== 'production') {
- if (response) {
- logs.push(`Got response from network.`);
- }
- else {
- logs.push(`Unable to get a response from the network.`);
- }
- }
- }
- else {
- if (process.env.NODE_ENV !== 'production') {
- logs.push(`Found a cached response in the '${this.cacheName}' cache.`);
- }
- }
- if (process.env.NODE_ENV !== 'production') {
- logger.groupCollapsed(messages.strategyStart(this.constructor.name, 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;
- }
- }
- export { CacheFirst };
|