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

Chris Schmitz picture Chris Schmitz · May 29, 2018 · Viewed 8.2k times · Source

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??

Answer

jun picture jun · Jun 4, 2018

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.