Skip to content

Commit e19150d

Browse files
Yenya030mattrbeck
authored andcommitted
fix(service-worker): preserve redirect policy on reconstructed asset requests
Preserve the redirect mode when rebuilding asset requests in newRequestWithMetadata(). This keeps explicit redirect:error semantics intact across service-worker redirect handling. Update the worker test mocks to model redirect defaults correctly and add focused regression coverage for redirected lazy assets with redirect:error. (cherry picked from commit 07abfbc)
1 parent 0e496c1 commit e19150d

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

packages/service-worker/worker/src/assets.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ export abstract class AssetGroup {
501501
* Create a new `Request` based on the specified URL and `RequestInit` options, preserving only
502502
* metadata that are known to be safe.
503503
*
504-
* Currently, only headers are preserved.
504+
* Currently, only headers and redirect policy are preserved.
505505
*
506506
* NOTE:
507507
* Things like credential inclusion are intentionally omitted to avoid issues with opaque
@@ -512,7 +512,10 @@ export abstract class AssetGroup {
512512
* https://github.com/angular/angular/issues/41931#issuecomment-1227601347
513513
*/
514514
private newRequestWithMetadata(url: string, options: RequestInit): Request {
515-
return this.adapter.newRequest(url, {headers: options.headers});
515+
return this.adapter.newRequest(url, {
516+
headers: options.headers,
517+
redirect: options.redirect,
518+
});
516519
}
517520

518521
/**

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,12 @@ import {envIsSupported} from '../testing/utils';
16891689
expect(redirectReq.mode).toBe('cors'); // The default value.
16901690
expect((redirectReq as any).unknownOption).toBeUndefined();
16911691
});
1692+
1693+
it('does not follow redirects when redirect policy is error', async () => {
1694+
await expectAsync(
1695+
makeRequest(scope, '/lazy/redirected.txt', undefined, {redirect: 'error'}),
1696+
).toBeRejected();
1697+
});
16921698
});
16931699
});
16941700

packages/service-worker/worker/testing/fetch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class MockRequest extends MockBody implements Request {
115115
readonly keepalive: boolean = true;
116116
readonly method: string = 'GET';
117117
readonly mode: RequestMode = 'cors';
118-
readonly redirect: RequestRedirect = 'error';
118+
readonly redirect: RequestRedirect = 'follow';
119119
readonly referrer: string = '';
120120
readonly referrerPolicy: ReferrerPolicy = 'no-referrer';
121121
readonly signal: AbortSignal = null as any;
@@ -153,6 +153,9 @@ export class MockRequest extends MockBody implements Request {
153153
if (init.method !== undefined) {
154154
this.method = init.method;
155155
}
156+
if (init.redirect !== undefined) {
157+
this.redirect = init.redirect;
158+
}
156159
if (init.destination !== undefined) {
157160
this.destination = init.destination;
158161
}
@@ -167,6 +170,7 @@ export class MockRequest extends MockBody implements Request {
167170
mode: this.mode,
168171
credentials: this.credentials,
169172
headers: this.headers,
173+
redirect: this.redirect,
170174
});
171175
}
172176
}

packages/service-worker/worker/testing/mock.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ export class MockServerState {
165165
}
166166
const url = req.url.split('?')[0];
167167
if (this.resources.has(url)) {
168-
return this.resources.get(url)!.clone();
168+
const response = this.resources.get(url)!.clone();
169+
if ((response as any).redirected && req.redirect === 'error') {
170+
throw new Error('Redirect disallowed by request policy.');
171+
}
172+
return response;
169173
}
170174
if (this.errors.has(url)) {
171175
throw new Error('Intentional failure!');

0 commit comments

Comments
 (0)