Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<link rel=preload> must have a valid `as` value

Tags:

html

preload

I'm trying to preload a video using the link tag's preload rel value per mdn's documentation on preload.

in my index.html file I'm adding the following to the head:

<link rel="preload" as="video" type="video/mp4" href="video/2_1.mp4" />

In chrome this works fine and preloads the file without issue.

When I open the page in safari 11.3 either on my desktop or on an iphone I get a console error message:

must have a valid as value

According to the "what types of content can be preloaded" section of the documentation that contains the list of valid values I'm definitely using the correct video type.

I checked both the mdn documentation for mobile safari preload option on the link tag and it shows a "compatibility unknown" question mark. I also checkedcaniuse and it seems to indicate that as long as my mobile safari version is at 11.3 I should be able to use it.

The phone and my desktop are both at safari 11.3, so I'm not sure why I'm getting this error.

Any ideas/insight??

like image 246
Chris Schmitz Avatar asked May 29 '18 21:05

Chris Schmitz


People also ask

What is preload in link rel?

The preload value of the <link> element's rel attribute lets you declare fetch requests in the HTML's <head> , specifying resources that your page will need very soon, which you want to start loading early in the page lifecycle, before browsers' main rendering machinery kicks in.

What is preload tag in HTML?

The preload attribute specifies if and how the author thinks that the media file should be loaded when the page loads. The preload attribute allows the author to provide a hint to the browser about what he/she thinks will lead to the best user experience.

How do you preload a script?

The rel="preload" attribute value is used to preload assets. It can be applied to several file formats, including CSS, JS, fonts, images, and more. Depending on the type of file you would like to preload, the corresponding as attribute may also need to be included along with rel="preload" .

What is prefetch and preload?

preload is a declarative fetch, allowing you to force the browser to make a request for a resource without blocking the document's onload event. Prefetch is a hint to the browser that a resource might be needed, but delegates deciding whether and when loading it is a good idea or not to the browser.


2 Answers

it seems webkit disable preload for video and audio file.

if (RuntimeEnabledFeatures::sharedFeatures().mediaPreloadingEnabled() && (equalLettersIgnoringASCIICase(as, "video") || equalLettersIgnoringASCIICase(as, "audio")))
    return CachedResource::MediaResource;
if (equalLettersIgnoringASCIICase(as, "font"))
    return CachedResource::FontResource;
#if ENABLE(VIDEO_TRACK)
if (equalLettersIgnoringASCIICase(as, "track"))
    return CachedResource::TextTrackResource;
#endif
return std::nullopt;

https://github.com/WebKit/webkit/blob/master/Source/WebCore/loader/LinkLoader.cpp#L125

auto type = LinkLoader::resourceTypeFromAsAttribute(as);
if (!type) {
    document.addConsoleMessage(MessageSource::Other, MessageLevel::Error, String("<link rel=preload> must have a valid `as` value"));
    return nullptr;
}

https://github.com/WebKit/webkit/blob/master/Source/WebCore/loader/LinkLoader.cpp#L239-L243

I'm not sure if we can enable mediaPreloadingEnabled on safari by changing some configuration.

like image 62
jun Avatar answered Oct 19 '22 02:10

jun


as="video|audio|document" isn't supported on Chrome.

Didn't found any official post/bug on the browsers sites, but I did found this on chromium repository: https://github.com/chromium/chromium/blob/99314be8152e688bafbbf9a615536bdbb289ea87/third_party/blink/web_tests/fast/dom/HTMLLinkElement/link-preload-unsupported-destination.html

<link rel=preload href="resources/empty.html">
<link rel=preload href="resources/empty.html" as="document">
<link rel=preload href="../../../media/content/test.mp4" as="audio">
<link rel=preload href="../../../media/content/test.wav" as="video">
<!--This test verifies that specific warnings are shown when preload
   resources use unsupported (but valid) destinations. -->

Basically it's a test that verify that Chrome throws a warning when using those link unsupported 'as' attributes

like image 42
Eylon Sultan Avatar answered Oct 19 '22 03:10

Eylon Sultan