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

Support for registering non-GET via runtimeCaching #662

Merged
merged 2 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/workbox-build/src/lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ module.exports = {
`staticFileGlobs are set. Please fully migrate to globPatterns.`,
'both-templated-urls-dynamic-urls': `Both templatedUrls and ` +
`dynamicUrlToDependencies are set. Please fully migrate to templatedUrls.`,
'method-not-supported': `Using a method other than 'GET' in runtimeCaching is
not supported.`,
'urlPattern-is-required': `The urlPattern option is required when using
runtimeCaching.`,
'handler-is-required': `The handler option is required when using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ function getOptionsString(options) {
module.exports = (runtimeCaching) => {
runtimeCaching = runtimeCaching || [];
return runtimeCaching.map((entry) => {
if (entry.method && entry.method !== 'GET') {
throw new Error(errors['method-not-supported']);
}
const method = entry.method || 'GET';

if (!entry.urlPattern) {
throw new Error(errors['urlPattern-is-required']);
Expand Down Expand Up @@ -69,10 +67,10 @@ module.exports = (runtimeCaching) => {
`workboxSW.strategies.${handlerName}(${optionsString})`;

return `workboxSW.router.registerRoute(` +
`${matcher}, ${strategyString});`;
`${matcher}, ${strategyString}, '${method}');`;
} else if (typeof entry.handler === 'function') {
return `workboxSW.router.registerRoute(` +
`${matcher}, ${entry.handler});`;
`${matcher}, ${entry.handler}, '${method}');`;
}
}).filter((entry) => Boolean(entry)); // Remove undefined map() return values.
};
16 changes: 8 additions & 8 deletions packages/workbox-build/test/node/lib-write-sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,15 +714,15 @@ const fileManifest = [

const workboxSW = new self.WorkboxSW();
workboxSW.precache(fileManifest);
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.cacheFirst({}));
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.cacheOnly({}));
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.networkFirst({}));
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.networkOnly({}));
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.staleWhileRevalidate({}));
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.staleWhileRevalidate({}));
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.cacheFirst({}), 'GET');
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.cacheOnly({}), 'GET');
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.networkFirst({}), 'GET');
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.networkOnly({}), 'GET');
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.staleWhileRevalidate({}), 'GET');
workboxSW.router.registerRoute(/^https:\\/\\/example\\.com\\/api/, workboxSW.strategies.staleWhileRevalidate({}), 'GET');
workboxSW.router.registerRoute('/images/:size/:name.jpg', (request, values, options) => {
return new Promise('params: ' + values.join(','));
});
}, 'GET');
workboxSW.router.registerRoute(/\\/articles\\//, workboxSW.strategies.staleWhileRevalidate({
"cacheName": "example-cache",
"cacheExpiration": {
Expand All @@ -743,7 +743,7 @@ workboxSW.router.registerRoute(/\\/articles\\//, workboxSW.strategies.staleWhile
"Example-Header-2": "Header-Value-2"
}
}
}));
}), 'GET');
`;
const writeSw = proxyquire('../../src/lib/write-sw', {
'mkdirp': {
Expand Down
29 changes: 17 additions & 12 deletions packages/workbox-build/test/node/utils-runtime-caching-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function validate(runtimeCachingOptions, convertedOptions) {
const registerRouteCall = globalScope.workboxSW.router.registerRoute.getCall(i);
expect(registerRouteCall.args[0]).to.eql(runtimeCachingOption.urlPattern);

if (runtimeCachingOption.method) {
expect(registerRouteCall.args[2]).to.eql(runtimeCachingOption.method);
} else {
expect(registerRouteCall.args[2]).to.eql('GET');
}

if (typeof runtimeCachingOption.handler === 'function') {
// We can't make assumptions about what custom function handlers will do.
return;
Expand Down Expand Up @@ -72,18 +78,6 @@ function validate(runtimeCachingOptions, convertedOptions) {
}

describe('src/lib/utils/runtime-caching-converter.js', function() {
it(`should throw when a method other than GET is used`, function() {
const runtimeCachingOptions = [{
urlPattern: /xyz/,
handler: 'cacheFirst',
method: 'POST',
}];

expect(() => {
runtimeCachingConverter(runtimeCachingOptions);
}).to.throw(errors['method-not-supported']);
});

it(`should throw when urlPattern isn't set`, function() {
const runtimeCachingOptions = [{
handler: 'cacheFirst',
Expand Down Expand Up @@ -166,4 +160,15 @@ describe('src/lib/utils/runtime-caching-converter.js', function() {
const convertedOptions = runtimeCachingConverter(runtimeCachingOptions);
validate(runtimeCachingOptions, convertedOptions);
});

it(`should support registering non-GET methods`, function() {
const runtimeCachingOptions = [{
urlPattern: '/path/to/file',
handler: 'cacheFirst',
method: 'POST',
}];

const convertedOptions = runtimeCachingConverter(runtimeCachingOptions);
validate(runtimeCachingOptions, convertedOptions);
});
});