Skip to content
Closed
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
10 changes: 9 additions & 1 deletion packages/service-worker/worker/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/service-worker/worker/test/happy_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions packages/service-worker/worker/testing/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export class MockRequest extends MockBody implements Request {
if (init.credentials !== undefined) {
this.credentials = init.credentials;
}
if (init.method !== undefined) {
this.method = init.method;
}
}

clone(): Request {
Expand Down