@@ -94,6 +94,12 @@ export class Driver implements Debuggable, UpdateSource {
94
94
*/
95
95
private scheduledNavUpdateCheck : boolean = false ;
96
96
97
+ /**
98
+ * Keep track of whether we have logged an invalid `only-if-cached` request.
99
+ * (See `.onFetch()` for details.)
100
+ */
101
+ private loggedInvalidOnlyIfCachedRequest : boolean = false ;
102
+
97
103
/**
98
104
* A scheduler which manages a queue of tasks that need to be executed when the SW is
99
105
* not doing any other work (not processing any other requests).
@@ -156,12 +162,13 @@ export class Driver implements Debuggable, UpdateSource {
156
162
* asynchronous execution that eventually resolves for respondWith() and waitUntil().
157
163
*/
158
164
private onFetch ( event : FetchEvent ) : void {
165
+ const req = event . request ;
166
+
159
167
// The only thing that is served unconditionally is the debug page.
160
- if ( this . adapter . parseUrl ( event . request . url , this . scope . registration . scope ) . path ===
161
- '/ngsw/state' ) {
168
+ if ( this . adapter . parseUrl ( req . url , this . scope . registration . scope ) . path === '/ngsw/state' ) {
162
169
// Allow the debugger to handle the request, but don't affect SW state in any
163
170
// other way.
164
- event . respondWith ( this . debugger . handleFetch ( event . request ) ) ;
171
+ event . respondWith ( this . debugger . handleFetch ( req ) ) ;
165
172
return ;
166
173
}
167
174
@@ -177,6 +184,24 @@ export class Driver implements Debuggable, UpdateSource {
177
184
return ;
178
185
}
179
186
187
+ // When opening DevTools in Chrome, a request is made for the current URL (and possibly related
188
+ // resources, e.g. scripts) with `cache: 'only-if-cached'` and `mode: 'no-cors'`. These request
189
+ // will eventually fail, because `only-if-cached` is only allowed to be used with
190
+ // `mode: 'same-origin'`.
191
+ // This is likely a bug in Chrome DevTools. Avoid handling such requests.
192
+ // (See also https://github.com/angular/angular/issues/22362.)
193
+ // TODO(gkalpak): Remove once no longer necessary (i.e. fixed in Chrome DevTools).
194
+ if ( ( req . cache as string ) === 'only-if-cached' && req . mode !== 'same-origin' ) {
195
+ // Log the incident only the first time it happens, to avoid spamming the logs.
196
+ if ( ! this . loggedInvalidOnlyIfCachedRequest ) {
197
+ this . loggedInvalidOnlyIfCachedRequest = true ;
198
+ this . debugger . log (
199
+ `Ignoring invalid request: 'only-if-cached' can be set only with 'same-origin' mode` ,
200
+ `Driver.fetch(${ req . url } , cache: ${ req . cache } , mode: ${ req . mode } )` ) ;
201
+ }
202
+ return ;
203
+ }
204
+
180
205
// Past this point, the SW commits to handling the request itself. This could still
181
206
// fail (and result in `state` being set to `SAFE_MODE`), but even in that case the
182
207
// SW will still deliver a response.
@@ -611,8 +636,8 @@ export class Driver implements Debuggable, UpdateSource {
611
636
* offline (detected as response status 504).
612
637
*/
613
638
private async fetchLatestManifest ( ignoreOfflineError ?: false ) : Promise < Manifest > ;
614
- private async fetchLatestManifest ( ignoreOfflineError : true ) : Promise < Manifest | null > ;
615
- private async fetchLatestManifest ( ignoreOfflineError = false ) : Promise < Manifest | null > {
639
+ private async fetchLatestManifest ( ignoreOfflineError : true ) : Promise < Manifest | null > ;
640
+ private async fetchLatestManifest ( ignoreOfflineError = false ) : Promise < Manifest | null > {
616
641
const res =
617
642
await this . safeFetch ( this . adapter . newRequest ( 'ngsw.json?ngsw-cache-bust=' + Math . random ( ) ) ) ;
618
643
if ( ! res . ok ) {
0 commit comments