2

I have a userscript where I'd like to cache audio files from audio elements. fetch() isn't viable here as I have to wait for it to fetch to get a blob out of it. Is there a way to use the audio element itself for caching? The codec of the audio is FLAC.

5
  • fetch() isn't viable here as I have to wait for it how is that an issue? Commented Oct 2, 2023 at 4:53
  • there is also this mdn : Request: cache property Commented Oct 2, 2023 at 6:09
  • @JaromandaX As I have to wait for the fetch to finish, the audio doesn't start playing immediately after the play button is pressed and that's suboptimal even if it works Commented Oct 2, 2023 at 19:13
  • OK then I totally misunderstand what you're trying to achieve Commented Oct 2, 2023 at 21:42
  • I have audio links which I must cache when they're played. I can't use fetch() as that would require waiting for the entire file to fetch, which is much slower compared to a serviceWorker, which can clone a request from an audio element so that the audio plays normally and gets cached when it's fully loaded. Commented Oct 2, 2023 at 21:51

1 Answer 1

0

If you use the appropriate Cache-Control header, the browser will cache for you. No other work is required.

For example:

Cache-Control: public, max-age=604800, immutable
Sign up to request clarification or add additional context in comments.

5 Comments

that would require access to the server - at a guess, since OP is using tampermonkey, they are trying to do this on a site that isn't theirs?
@JaromandaX Tampermonkey can't alter response headers? I've never used it myself, but it seems like it should be able to, yeah?
You may be right, been years since I used it :p
I don't think Cache-Control can be used here as all the URL parameters change per visit except for one
@SeetheMoar Sad. In that case, yeah you need a Service Worker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.