Skip to content
Closed

Sw bugs #19764

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: 5 additions & 5 deletions packages/service-worker/worker/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ export class DataGroup {
}

// The request completed in time, so cache it inline with the response flow.
// Make sure to clone it so the real response can still be returned to the user.
await this.cacheResponse(req, res.clone(), lru);
await this.cacheResponse(req, res, lru);
return res;
}

Expand Down Expand Up @@ -409,7 +408,7 @@ export class DataGroup {

// No response in the cache. No choice but to fall back on the full network fetch.
res = await networkFetch;
await this.cacheResponse(req, res.clone(), lru, true);
await this.cacheResponse(req, res, lru, true);
return res;
}

Expand Down Expand Up @@ -517,8 +516,9 @@ export class DataGroup {
// until enough other resources are requested that it falls off the end of the LRU chain.
lru.accessed(req.url);

// Store the response in the cache.
await(await this.cache).put(req, res);
// Store the response in the cache (cloning because the browser will consume
// the body during the caching operation).
await(await this.cache).put(req, res.clone());

// Store the age of the cache.
const ageTable = await this.ageTable;
Expand Down
2 changes: 1 addition & 1 deletion packages/service-worker/worker/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class Driver implements Debuggable, UpdateSource {
}

// Handle the push and keep the SW alive until it's handled.
msg.waitUntil(this.handlePush(msg.data));
msg.waitUntil(this.handlePush(msg.data.json()));
}

private async handleMessage(msg: MsgAny&{action: string}, from: Client): Promise<void> {
Expand Down
5 changes: 5 additions & 0 deletions packages/service-worker/worker/testing/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export class MockCache implements Cache {
async put(request: RequestInfo, response: Response): Promise<void> {
const url = (typeof request === 'string' ? request : request.url);
this.cache.set(url, response.clone());

// Even though the body above is cloned, consume it here because the
// real cache consumes the body.
await response.text();

return;
}

Expand Down
13 changes: 10 additions & 3 deletions packages/service-worker/worker/testing/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class MockBody implements Body {
constructor(public _body: string|null) {}

async arrayBuffer(): Promise<ArrayBuffer> {
this.bodyUsed = true;
this.markBodyUsed();
if (this._body !== null) {
const buffer = new ArrayBuffer(this._body.length);
const access = new Uint8Array(buffer);
Expand All @@ -28,7 +28,7 @@ export class MockBody implements Body {
async blob(): Promise<Blob> { throw 'Not implemented'; }

async json(): Promise<any> {
this.bodyUsed = true;
this.markBodyUsed();
if (this._body !== null) {
return JSON.parse(this._body);
} else {
Expand All @@ -37,7 +37,7 @@ export class MockBody implements Body {
}

async text(): Promise<string> {
this.bodyUsed = true;
this.markBodyUsed();
if (this._body !== null) {
return this._body;
} else {
Expand All @@ -46,6 +46,13 @@ export class MockBody implements Body {
}

async formData(): Promise<FormData> { throw 'Not implemented'; }

private markBodyUsed(): void {
if (this.bodyUsed === true) {
throw new Error('Cannot reuse body without cloning.');
}
this.bodyUsed = true;
}
}

export class MockHeaders implements Headers {
Expand Down
5 changes: 4 additions & 1 deletion packages/service-worker/worker/testing/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ class MockMessageEvent extends MockExtendableEvent {
}

class MockPushEvent extends MockExtendableEvent {
constructor(readonly data: Object) { super(); }
constructor(private _data: Object) { super(); }
data = {
json: () => this._data,
};
}

class MockInstallEvent extends MockExtendableEvent {}
Expand Down