How to use the workbox-core/_private/fetchWrapper.js.fetchWrapper.fetch function in workbox-core

To help you get started, we’ve selected a few workbox-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GoogleChrome / workbox / packages / workbox-precaching / src / PrecacheController.ts View on Github external
async _addURLToCache({cacheKey, url, cacheMode, event, plugins, integrity}: {
    cacheKey: string,
    url: string,
    cacheMode: "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached" | undefined,
    event?: ExtendableEvent,
    plugins?: WorkboxPlugin[],
    integrity?: string,
  }) {
    const request = new Request(url, {
      integrity,
      cache: cacheMode,
      credentials: 'same-origin',
    });

    let response = await fetchWrapper.fetch({
      event,
      plugins,
      request,
    });

    // Allow developers to override the default logic about what is and isn't
    // valid by passing in a plugin implementing cacheWillUpdate(), e.g.
    // a workbox.cacheableResponse.CacheableResponsePlugin instance.
    let cacheWillUpdatePlugin;
    for (const plugin of (plugins || [])) {
      if ('cacheWillUpdate' in plugin) {
        cacheWillUpdatePlugin = plugin;
      }
    }

    const isValidResponse = cacheWillUpdatePlugin ?
github GoogleChrome / workbox / packages / workbox-strategies / src / CacheFirst.ts View on Github external
async _getFromNetwork(request: Request, event?: ExtendableEvent) {
    const response = await fetchWrapper.fetch({
      request,
      event,
      fetchOptions: this._fetchOptions,
      plugins: this._plugins,
    });

    // Keep the service worker while we put the request to the cache
    const responseClone = response.clone();
    const cachePutPromise = cacheWrapper.put({
      cacheName: this._cacheName,
      request,
      response: responseClone,
      event,
      plugins: this._plugins,
    });
github GoogleChrome / workbox / packages / workbox-strategies / src / NetworkFirst.ts View on Github external
async _getNetworkPromise({timeoutId, request, logs, event}: {
    request: Request,
    logs: any[],
    timeoutId?: number,
    event?: ExtendableEvent,
  }): Promise {
    let error;
    let response;
    try {
      response = await fetchWrapper.fetch({
        request,
        event,
        fetchOptions: this._fetchOptions,
        plugins: this._plugins,
      });
    } catch (err) {
      error = err;
    }

    if (timeoutId) {
      clearTimeout(timeoutId);
    }

    if (process.env.NODE_ENV !== 'production') {
      if (response) {
        logs.push(`Got response from network.`);
github GoogleChrome / workbox / packages / workbox-strategies / src / StaleWhileRevalidate.ts View on Github external
async _getFromNetwork({request, event}: {
    request: Request,
    event?: ExtendableEvent,
  }): Promise {
    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 {
github GoogleChrome / workbox / packages / workbox-strategies / src / NetworkOnly.ts View on Github external
async handle({event, request}: RouteHandlerCallbackOptions): Promise {
    if (process.env.NODE_ENV !== 'production') {
      assert!.isInstance(request, Request, {
        moduleName: 'workbox-strategies',
        className: 'NetworkOnly',
        funcName: 'handle',
        paramName: 'request',
      });
    }

    let error;
    let response;
    try {
      response = await fetchWrapper.fetch({
        request,
        event,
        fetchOptions: this._fetchOptions,
        plugins: this._plugins,
      });
    } catch (err) {
      error = err;
    }

    if (process.env.NODE_ENV !== 'production') {
      logger.groupCollapsed(
          messages.strategyStart('NetworkOnly', request));
      if (response) {
        logger.log(`Got response from network.`);
      } else {
        logger.log(`Unable to get a response from the network.`);