Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Empty object when adding runtimeCaching custom plugins with workbox-webpack-plugin #1597

Closed
tsirlucas opened this issue Aug 6, 2018 · 0 comments
Labels
Bug An issue with our existing, production codebase. Developer Experience Related to ease of use for developers. workbox-build

Comments

@tsirlucas
Copy link
Contributor

Library Affected:
workbox-webpack-plugin and workbox-build

Browser & Platform:
not browser-related

Issue or Feature Request Description:
When adding custom plugins to runtimeCaching option on workbox-webpack-plugin, the output sw comes with an empty object instead of the object with functions to be executed.

I think this is happening because of this line once JSON.stringify removes functions. I've managed to fix the issue on a fork with this commit parsing the object to string instead (I think it's the desired behavior and it works).

My solution doesn't parse comments and once the populate-sw-template puts the plugin content inline, single-line comments can break everything. I can work on a solution for this either but I'd like to know if you guys would accept a PR and what would be the desired behavior when dealing with comments. Should them be removed or transformed into to multi-line comments?

Code to reproduce:

new GenerateSW({
    swDest: 'sw.js',
    clientsClaim: true,
    skipWaiting: true,
    importWorkboxFrom: 'local',
    navigateFallback: '/',
    runtimeCaching: [
      {
        urlPattern: /^https:\/\/api-random\.com\/random\//,
        handler: 'cacheFirst',
        options: {
          cacheName: 'random-cache',
          expiration: {
            maxEntries: 40,
            maxAgeSeconds: 200000,
          },
          cacheableResponse: {statuses: [0, 200, 201, 206, 301, 304, 302]},
          plugins: [{
            cacheWillUpdate: ({request, response}) => {
              return caches.match(request.url, {ignoreSearch: true}).then((res) => {
                /* ignore repeated songs */
                if (res) {
                  return null;
                }
          
                /* cache only save=true param */
                if (request.url.includes('?save=true')) {
                  return response;
                }
          
                /* ignore any other thing */
                return null;
              });
            },
            cachedResponseWillBeUsed: ({request, cachedResponse}) => {
              if (cachedResponse) {
                return cachedResponse;
              }
              /* this will match same url/diff query string where the original failed */
              return caches.match(request.url, {ignoreSearch: true});
            },
          }],
        },
      },
    ],
  });

@tsirlucas tsirlucas changed the title Empty object adding runtimeCaching custom plugins with workbox-webpack-plugin Empty object when adding runtimeCaching custom plugins with workbox-webpack-plugin Aug 6, 2018
@tsirlucas tsirlucas changed the title Empty object when adding runtimeCaching custom plugins with workbox-webpack-plugin [BUG] Empty object when adding runtimeCaching custom plugins with workbox-webpack-plugin Aug 6, 2018
@jeffposnick jeffposnick added Bug An issue with our existing, production codebase. Developer Experience Related to ease of use for developers. workbox-build labels Aug 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug An issue with our existing, production codebase. Developer Experience Related to ease of use for developers. workbox-build
Projects
None yet
Development

No branches or pull requests

2 participants