Preload audio with <link rel="preload"> from Audio object (i.e. no audio tags in html)

Viewed 778

In my js I have created one Audio object, with which I play/pause several audio files. I was wondering if and how I could preload the files present in the /audio folder without inserting any <audio> element in the HTML.

I tried <link rel="preload" href="/audio/file.mp3" as="audio"> but Chrome gives me this warning: "link rel=preload must have a valid as value".

I also tried putting media (instead of audio) in the as attribute. Same warning.

What am I doing wrong?

2 Answers

You must add style using the "as" attribute like this:

<link rel="preload" href="style.css" as="style">

This takes care of stylesheet in rel="stylesheet" which has been replaced by preload.

Hopefully this answer will get outdated soon, but when I look at the official docs, (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link), I find that "preload" has questionable support for both Chrome and Safari. The warning we are all seeing is likely due to the fact that Chrome/Safari is recognizing the "preload" tag but is not recognizing "audio" (or whatever you are using) as a valid value for the "as" tag.

Related