some pain point for building podcast over pwa.
sw can't access to DOM API it includes DOMParser API too. so it neccessary for import extra xml feed parser in sw. it reasonable for me that sw can't access to window dom tree but if DOMParser is indepened from tree, I wish I can use them.
in my case, I serve feed xml by myself, so I export json version of feed.
I wanna display some ID3 tag data in mp3. in that case i wanna get ArraBuffer from tag which load mp3 data. I mean something like
// <audio src=episode.mp3>
$('audio').addEventListener('load', (e) => {
new ID3Parser(e.target.buffer)
})
but there are no such api so I need to do fetch by myself
const res = await fetch('episode.mp3')
const buffer = await res.arrayBuffer
new ID3Parser(buffer)
$('audio').src = createObjectURL(buffer)
but I basically wanna make <audio>
to natively fetch
with partial request with own header each browser intended to send.
so I pending this.
IIUC, background fetch need to call with total result byte size. if fetching new Audio mp3 with Shownote HTML,
const option = {
title: 'new episode',
downloadTotal: html.size + mp3.size
icons: [{src: 'logo.png', sizes: '256x256', type: 'image/webp'}]
}
const registration = await navigator.serviceWorker.ready
const task = await registration.backgroundFetch.fetch(id, [html, mp3], option)
basically podcast feed includes mp3 size, but there are no html size.
in my case, I deligate html caching to sw like below.
// window
const controller = navigator.serviceWorker.controller
controller.postMessage({type: 'save', url: this.dataset.page})
// sw
self.addEventListener('message', async (e) => {
const { type, url } = e.data;
if (type === 'save') {
const cache = await caches.open(SHOWNOTE)
cache.add(url)
}
})
I wonder if there are convenient way to do this.
I'll update if I remember.
@jeffposnick Ah thanks for pointing out. Yes It's correct, but in my code, SW only knows which Cache Name (means value for
caches.open(Name)
) is fit for save. so I pass it to sw.so I should say in other words like, bgfetch with unknown length content will fit to this case.