fix(service-worker): allow creating post api requests after cache failure (#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
This commit is contained in:
Denis Omelkov 2019-11-21 16:09:46 +05:00 committed by Matias Niemelä
parent 82be6215af
commit 621b659aa9
2 changed files with 30 additions and 1 deletions

View File

@ -275,7 +275,15 @@ export class DataGroup {
return;
}
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.name}@${this.config.version}).syncLru()`);
// TODO: Better detect/handle full storage; e.g. using
// [navigator.storage](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorStorage/storage).
}
}
/**

View File

@ -1330,6 +1330,27 @@ import {SwTestHarness, SwTestHarnessBuilder} from '../testing/scope';
server.assertSawRequestFor('/api-static/bar');
});
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;
server.clearRequests();
// 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');
expect(driver.state).toBe(DriverReadyState.NORMAL);
// Since we are swallowing an error here, make sure it is at least properly logged
expect(driver.debugger.log)
.toHaveBeenCalledWith(new Error('Can\'t touch this'), 'DataGroup(api@42).syncLru()');
server.assertSawRequestFor('/api/foo');
});
it('enters degraded mode when something goes wrong with the latest version', async() => {
await driver.initialized;