123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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 './_version.js';
- class CacheFirst {
-
- constructor(options = {}) {
- this._cacheName = cacheNames.getRuntimeName(options.cacheName);
- this._plugins = options.plugins || [];
- 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: 'CacheFirst',
- funcName: 'makeRequest',
- paramName: 'request',
- });
- }
- 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(`No response found in the '${this._cacheName}' cache. ` +
- `Will respond with a network request.`);
- }
- try {
- response = await this._getFromNetwork(request, event);
- }
- catch (err) {
- 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('CacheFirst', 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 responseClone = response.clone();
- const cachePutPromise = cacheWrapper.put({
- cacheName: this._cacheName,
- request,
- response: responseClone,
- 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 { CacheFirst };
|