runtime-caching-converter.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. /*
  3. Copyright 2018 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. const ol = require('common-tags').oneLine;
  9. const errors = require('./errors');
  10. const stringifyWithoutComments = require('./stringify-without-comments');
  11. /**
  12. * Given a set of options that configures runtime caching behavior, convert it
  13. * to the equivalent Workbox method calls.
  14. *
  15. * @param {ModuleRegistry} moduleRegistry
  16. * @param {Object} options See
  17. * https://developers.google.com/web/tools/workbox/modules/workbox-build#generateSW-runtimeCaching
  18. * @return {string} A JSON string representing the equivalent options.
  19. *
  20. * @private
  21. */
  22. function getOptionsString(moduleRegistry, options = {}) {
  23. let plugins = [];
  24. if (options.plugins) {
  25. // Using libs because JSON.stringify won't handle functions.
  26. plugins = options.plugins.map(stringifyWithoutComments);
  27. delete options.plugins;
  28. } // Pull handler-specific config from the options object, since they are
  29. // not directly used to construct a plugin instance. If set, need to be
  30. // passed as options to the handler constructor instead.
  31. const handlerOptionKeys = ['cacheName', 'networkTimeoutSeconds', 'fetchOptions', 'matchOptions'];
  32. const handlerOptions = {};
  33. for (const key of handlerOptionKeys) {
  34. if (key in options) {
  35. handlerOptions[key] = options[key];
  36. delete options[key];
  37. }
  38. }
  39. for (const [pluginName, pluginConfig] of Object.entries(options)) {
  40. // Ensure that we have some valid configuration to pass to the plugin.
  41. if (Object.keys(pluginConfig).length === 0) {
  42. continue;
  43. }
  44. let pluginCode;
  45. switch (pluginName) {
  46. case 'backgroundSync':
  47. {
  48. const name = pluginConfig.name;
  49. const plugin = moduleRegistry.use('workbox-background-sync', 'BackgroundSyncPlugin');
  50. pluginCode = `new ${plugin}(${JSON.stringify(name)}`;
  51. if ('options' in pluginConfig) {
  52. pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`;
  53. }
  54. pluginCode += `)`;
  55. break;
  56. }
  57. case 'broadcastUpdate':
  58. {
  59. const channelName = pluginConfig.channelName;
  60. const opts = Object.assign({
  61. channelName
  62. }, pluginConfig.options);
  63. const plugin = moduleRegistry.use('workbox-broadcast-update', 'BroadcastUpdatePlugin');
  64. pluginCode = `new ${plugin}(${stringifyWithoutComments(opts)})`;
  65. break;
  66. }
  67. case 'cacheableResponse':
  68. {
  69. const plugin = moduleRegistry.use('workbox-cacheable-response', 'CacheableResponsePlugin');
  70. pluginCode = `new ${plugin}(${stringifyWithoutComments(pluginConfig)})`;
  71. break;
  72. }
  73. case 'expiration':
  74. {
  75. const plugin = moduleRegistry.use('workbox-expiration', 'ExpirationPlugin');
  76. pluginCode = `new ${plugin}(${stringifyWithoutComments(pluginConfig)})`;
  77. break;
  78. }
  79. default:
  80. {
  81. throw new Error(errors['bad-runtime-caching-config'] + pluginName);
  82. }
  83. }
  84. plugins.push(pluginCode);
  85. }
  86. if (Object.keys(handlerOptions).length > 0 || plugins.length > 0) {
  87. const optionsString = JSON.stringify(handlerOptions).slice(1, -1);
  88. return ol`{
  89. ${optionsString ? optionsString + ',' : ''}
  90. plugins: [${plugins.join(', ')}]
  91. }`;
  92. } else {
  93. return '';
  94. }
  95. }
  96. module.exports = (moduleRegistry, runtimeCaching) => {
  97. return runtimeCaching.map(entry => {
  98. const method = entry.method || 'GET';
  99. if (!entry.urlPattern) {
  100. throw new Error(errors['urlPattern-is-required']);
  101. }
  102. if (!entry.handler) {
  103. throw new Error(errors['handler-is-required']);
  104. } // This validation logic is a bit too gnarly for joi, so it's manually
  105. // implemented here.
  106. if (entry.options && entry.options.networkTimeoutSeconds && entry.handler !== 'NetworkFirst') {
  107. throw new Error(errors['invalid-network-timeout-seconds']);
  108. } // urlPattern might be a string, a RegExp object, or a function.
  109. // If it's a string, it needs to be quoted.
  110. const matcher = typeof entry.urlPattern === 'string' ? JSON.stringify(entry.urlPattern) : entry.urlPattern;
  111. const registerRoute = moduleRegistry.use('workbox-routing', 'registerRoute');
  112. if (typeof entry.handler === 'string') {
  113. const optionsString = getOptionsString(moduleRegistry, entry.options);
  114. const handler = moduleRegistry.use('workbox-strategies', entry.handler);
  115. const strategyString = `new ${handler}(${optionsString})`;
  116. return `${registerRoute}(${matcher}, ${strategyString}, '${method}');\n`;
  117. } else if (typeof entry.handler === 'function') {
  118. return `${registerRoute}(${matcher}, ${entry.handler}, '${method}');\n`;
  119. }
  120. }).filter(entry => Boolean(entry)); // Remove undefined map() return values.
  121. };