<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tomer Steinfeld</title>
    <description>The latest articles on DEV Community by Tomer Steinfeld (@corevo).</description>
    <link>https://dev.to/corevo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F202842%2F76f80214-7f39-477c-9f2e-e22bbd15387a.png</url>
      <title>DEV Community: Tomer Steinfeld</title>
      <link>https://dev.to/corevo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/corevo"/>
    <language>en</language>
    <item>
      <title>The Weird Case of Streaming in Safari</title>
      <dc:creator>Tomer Steinfeld</dc:creator>
      <pubDate>Wed, 30 Jun 2021 07:36:59 +0000</pubDate>
      <link>https://dev.to/corevo/the-weird-case-of-streaming-in-safari-1a3b</link>
      <guid>https://dev.to/corevo/the-weird-case-of-streaming-in-safari-1a3b</guid>
      <description>&lt;p&gt;Last week I had the pleasure of having to write some code in order to get Safari to stream video. There are plenty of other valuable blog posts out there explaining how to do it. But the gist of it is to support the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range"&gt;Range&lt;/a&gt; header, and to make sure the &lt;strong&gt;url ends with an extension&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning partial content through HTTP
&lt;/h2&gt;

&lt;p&gt;Basically the &lt;code&gt;Range&lt;/code&gt; header is a way for the browser to tell our server how much of a resource it wants. The server in turn responds with status &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206"&gt;206 partial content&lt;/a&gt;, with more headers telling the browser how much content is left to be consumed.&lt;/p&gt;

&lt;p&gt;Safari won't render a video unless we support partial content response, in fact we can see that Safari actually tests us.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PDnHJZIF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ug9w1jch4wpyg8ioix1c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PDnHJZIF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ug9w1jch4wpyg8ioix1c.png" alt="Safari network tab"&gt;&lt;/a&gt;&lt;br&gt;
You can see that safari splits the request for the video resource into two. The first requests only requests the first 2 bytes, if we fail to respond with 206 Safari will not render the video.&lt;/p&gt;

&lt;p&gt;Although my server responded correctly, Safari still won't play the video.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content type and Safari
&lt;/h2&gt;

&lt;p&gt;I couldn't figure out what was wrong. The video was obviously formatted correctly, because when I just link to the file locally it would play. The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type"&gt;Content-Type&lt;/a&gt; was correct, so was the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source#attr-type"&gt;type attribute&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then I tried to add a file extension, I was out of ideas, I changed the route from &lt;code&gt;/video&lt;/code&gt; to &lt;code&gt;/video.webm&lt;/code&gt; and it worked! Apparently Safari checks three things in order to determine the type of a resource to be a video. The type attribute must match the &lt;code&gt;Content-Type&lt;/code&gt; header, and the url must end with a matching extension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Safari doesn't follow the path of other browsers when it comes to streaming videos, in order to get videos to stream on Safari make sure to follow these guidelines&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Support 206 partial content&lt;/li&gt;
&lt;li&gt;Return the correct &lt;code&gt;Content-Type&lt;/code&gt; header and &lt;code&gt;type&lt;/code&gt; attribute&lt;/li&gt;
&lt;li&gt;Add a matching file extension to the video &lt;code&gt;src&lt;/code&gt; URL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A working WebM example can be found &lt;a href="https://github.com/corevo/safari-video-streaming"&gt;here&lt;/a&gt;, cheers!&lt;/p&gt;

</description>
      <category>safari</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
