Skip to content
Browse files

fix(service-worker): allow creating post api requests after cache fai…

…lure (#33930)

Before creating a mutating http request, service-worker
invalidates lru cache entry and writes to cache storage.
Therefore, cache storage failure can prevent making post requests.
Fix this by catching and logging cache error, add a test case.

Fixes #33793

PR Close #33930
  • Loading branch information
apocalyp0sys authored and matsko committed Nov 21, 2019
1 parent 82be621 commit 621b659aa97d42eea6bf0a1ad4d1f06acc2ff7fe
Showing with 30 additions and 1 deletion.
  1. +9 −1 packages/service-worker/worker/src/data.ts
  2. +21 −0 packages/service-worker/worker/test/happy_spec.ts
@@ -275,7 +275,15 @@ export class DataGroup {
const table = await this.lruTable;
return table.write('lru', this._lru !.state);
try {
return table.write('lru', this._lru !.state);
} catch (err) {
// Writing lru cache table failed. This could be a result of a full storage.
// Continue serving clients as usual.
this.debugHandler.log(err, `DataGroup(${}@${this.config.version}).syncLru()`);
// TODO: Better detect/handle full storage; e.g. using
// [](

@@ -1330,6 +1330,27 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';

it('keeps serving mutating api requests when failing to write to cache',
// sw can invalidate LRU cache entry and try to write to cache storage on mutating request
async() => {
// Initialize the SW.
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
await driver.initialized;

// Make the caches unwritable.
spyOn(MockCache.prototype, 'put').and.throwError('Can\'t touch this');
spyOn(driver.debugger, 'log');
expect(await makeRequest(scope, '/api/foo', 'default', {
method: 'post'
})).toEqual('this is api foo');
// Since we are swallowing an error here, make sure it is at least properly logged
.toHaveBeenCalledWith(new Error('Can\'t touch this'), 'DataGroup(api@42).syncLru()');

it('enters degraded mode when something goes wrong with the latest version', async() => {
await driver.initialized;

0 comments on commit 621b659

Please sign in to comment.
You can’t perform that action at this time.