@@ -18,6 +18,13 @@ export async function cacheFirstFetch(request: Request): Promise<Response> {
1818 return cachedResponse ;
1919 }
2020
21+ /**
22+ * Strip the Range header if present. Safari sometimes adds Range headers
23+ * to requests, which results in 206 Partial Content responses that can't
24+ * be cached using .put() since they're incomplete responses.
25+ */
26+ const requestWithoutRangeHeader = stripRangeHeader ( request ) ;
27+
2128 /**
2229 * Ensure the response is not coming from HTTP cache.
2330 *
@@ -26,7 +33,7 @@ export async function cacheFirstFetch(request: Request): Promise<Response> {
2633 *
2734 * See service-worker.ts for more details.
2835 */
29- const response = await fetchFresh ( request ) ;
36+ const response = await fetchFresh ( requestWithoutRangeHeader ) ;
3037 if ( response . ok ) {
3138 /**
3239 * Confirm the current service worker is still active
@@ -38,7 +45,7 @@ export async function cacheFirstFetch(request: Request): Promise<Response> {
3845 // Intentionally do not await writing to the cache so the response
3946 // promise can be returned immediately and observed for progress events.
4047 // NOTE: This is a race condition for simultaneous requests for the same asset.
41- offlineModeCache . put ( request , response . clone ( ) ) ;
48+ offlineModeCache . put ( requestWithoutRangeHeader , response . clone ( ) ) ;
4249 }
4350 }
4451
@@ -179,6 +186,26 @@ export function shouldCacheUrl(url: URL) {
179186 return self . location . hostname === url . hostname ;
180187}
181188
189+ /**
190+ * Removes the Range header from a request if present.
191+ *
192+ * Safari sometimes adds Range headers which cause 206 Partial Content responses
193+ * that can't be cached using Cache API's .put() method.
194+ *
195+ * @param request The original request
196+ * @returns A new request without the Range header
197+ */
198+ function stripRangeHeader ( request : Request ) : Request {
199+ if ( ! request . headers . has ( 'range' ) ) {
200+ return request ;
201+ }
202+
203+ const headers = new Headers ( request . headers ) ;
204+ headers . delete ( 'range' ) ;
205+
206+ return new Request ( request , { headers } ) ;
207+ }
208+
182209/**
183210 * Fetches a resource and avoids stale responses from browser cache.
184211 *
0 commit comments