(req: Request, event: ExtendableEvent)
| 144 | } |
| 145 | |
| 146 | async handleFetch(req: Request, event: ExtendableEvent): Promise<Response | null> { |
| 147 | // Check the request against each `AssetGroup` in sequence. If an `AssetGroup` can't handle the |
| 148 | // request, |
| 149 | // it will return `null`. Thus, the first non-null response is the SW's answer to the request. |
| 150 | // So reduce |
| 151 | // the group list, keeping track of a possible response. If there is one, it gets passed |
| 152 | // through, and if |
| 153 | // not the next group is consulted to produce a candidate response. |
| 154 | const asset = await this.assetGroups.reduce(async (potentialResponse, group) => { |
| 155 | // Wait on the previous potential response. If it's not null, it should just be passed |
| 156 | // through. |
| 157 | const resp = await potentialResponse; |
| 158 | if (resp !== null) { |
| 159 | return resp; |
| 160 | } |
| 161 | |
| 162 | // No response has been found yet. Maybe this group will have one. |
| 163 | return group.handleFetch(req, event); |
| 164 | }, Promise.resolve<Response | null>(null)); |
| 165 | |
| 166 | // The result of the above is the asset response, if there is any, or null otherwise. Return the |
| 167 | // asset |
| 168 | // response if there was one. If not, check with the data caching groups. |
| 169 | if (asset !== null) { |
| 170 | return asset; |
| 171 | } |
| 172 | |
| 173 | // Perform the same reduction operation as above, but this time processing |
| 174 | // the data caching groups. |
| 175 | const data = await this.dataGroups.reduce(async (potentialResponse, group) => { |
| 176 | const resp = await potentialResponse; |
| 177 | if (resp !== null) { |
| 178 | return resp; |
| 179 | } |
| 180 | |
| 181 | return group.handleFetch(req, event); |
| 182 | }, Promise.resolve<Response | null>(null)); |
| 183 | |
| 184 | // If the data caching group returned a response, go with it. |
| 185 | if (data !== null) { |
| 186 | return data; |
| 187 | } |
| 188 | |
| 189 | // Next, check if this is a navigation request for a route. Detect circular |
| 190 | // navigations by checking if the request URL is the same as the index URL. |
| 191 | if (this.adapter.normalizeUrl(req.url) !== this.indexUrl && this.isNavigationRequest(req)) { |
| 192 | if (this.manifest.navigationRequestStrategy === 'freshness') { |
| 193 | // For navigation requests the freshness was configured. The request will always go trough |
| 194 | // the network and fallback to default `handleFetch` behavior in case of failure. |
| 195 | try { |
| 196 | return await this.scope.fetch(req); |
| 197 | } catch { |
| 198 | // Navigation request failed - application is likely offline. |
| 199 | // Proceed forward to the default `handleFetch` behavior, where |
| 200 | // `indexUrl` will be requested and it should be available in the cache. |
| 201 | } |
| 202 | } |
| 203 |
nothing calls this directly
no test coverage detected