某些瀏覽器在處理媒體資產要求 (也就是 <video> 和 <audio> 元素的 src 屬性指定網址) 時有些複雜問題,除非您在設定 Workbox 時採取具體步驟,否則可能導致放送行為錯誤。
問題
如要進一步瞭解瀏覽器提供音訊和影片素材資源的相關問題,請參閱這個 GitHub 問題討論。整體情況複雜,但重點如下:
- 必須透過
workbox-range-requests模組至做為處理常式的策略,要求 Workbox 遵循Range要求標頭。 <video>或<audio>元素必須透過crossorigin屬性啟用 CORS 模式。- 如要透過快取提供媒體,請提前將其明確新增至快取。如要這麼做,可以預先快取、利用
cache.add(),或 Workbox 方案中的 hotStrategyCache 方法。由於媒體素材資源是在執行階段串流,因此無法快取,因為播放期間只會從網路擷取部分內容。
以下說明如何在 Workbox 中配合這些規定,從媒體資產的適當標記開始著手:
<!-- In your page: --> <!-- You need to set `crossorigin`, even for same-origin URLs! --> <video src="movie.mp4" crossorigin="anonymous"></video> <audio src="song.mp3" crossorigin="anonymous"></audio> 然後在 Service Worker 中,使用 workbox-range-request 外掛程式據此處理媒體素材資源:
// sw.js import {registerRoute} from 'workbox-routing'; import {CacheFirst} from 'workbox-strategies'; import {CacheableResponsePlugin} from 'workbox-cacheable-response'; import {RangeRequestsPlugin} from 'workbox-range-requests'; // In your service worker: // It's up to you to either precache, use warmRuntimeCache, or // explicitly call cache.add() to populate the cache with media assets. // If you choose to cache media assets up front, do so with care, // as they can be quite large and exceed storage quotas. // // This route will go to the network if there isn't a cache match, // but it won't populate the cache at runtime because the response for // the media asset will be a partial 206 response. If there is a cache // match, then it will properly serve partial responses. registerRoute( ({request}) => { const {destination} = request; return destination === 'video' || destination === 'audio' }, new CacheFirst({ cacheName: 'your-cache-name-here', plugins: [ new CacheableResponsePlugin({ statuses: [200] }), new RangeRequestsPlugin(), ], }), ); 這樣一來,就能確保服務工作處理程序能正確擷取及快取您網站的媒體資產,同時將範圍要求和其他與媒體要求相關的潛在問題納入考量。