<?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>Forem: Adam Wojnicki</title>
    <description>The latest articles on Forem by Adam Wojnicki (@adamwojnicki).</description>
    <link>https://forem.com/adamwojnicki</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F275175%2Fea3248b6-f7d7-42f8-8af9-746abab478b7.jpg</url>
      <title>Forem: Adam Wojnicki</title>
      <link>https://forem.com/adamwojnicki</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/adamwojnicki"/>
    <language>en</language>
    <item>
      <title>React &lt;video&gt; autoPlay solution</title>
      <dc:creator>Adam Wojnicki</dc:creator>
      <pubDate>Thu, 31 Mar 2022 16:22:46 +0000</pubDate>
      <link>https://forem.com/adamwojnicki/react-autoplay-solution-2bkl</link>
      <guid>https://forem.com/adamwojnicki/react-autoplay-solution-2bkl</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hello, DEV community!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Today I was struggling with a video element in a react app. I was trying to insert an video to a website that would play looped in the background. &lt;/p&gt;

&lt;p&gt;That would require adding &lt;code&gt;autoPlay&lt;/code&gt;, &lt;code&gt;loop&lt;/code&gt; and &lt;code&gt;muted&lt;/code&gt; attributes to the video tag like here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.jsx //

import mutedVideo from './media/muted_vid.mp4'
import './App.css';

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      {/* VIDEO */}
      &amp;lt;video autoPlay muted loop&amp;gt;
        &amp;lt;source src={mutedVideo} /&amp;gt;
      &amp;lt;/video&amp;gt;
      {/* //VIDEO */}
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For some reason during source code bundling, React keeps ignoring &lt;code&gt;muted&lt;/code&gt; attribute. So the final outcome in production looks like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68tu4emub9tkarvudol6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F68tu4emub9tkarvudol6.png" alt="Image description" width="800" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After a while of research and browsing Stack Overflow, I learned that it is a common issue in React that still hasn't been solved for years!&lt;/p&gt;

&lt;p&gt;I've also found a workaround solution to overcome this bug. The solution is creating my own &lt;code&gt;Video&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// components/Video.jsx //

export default class Video extends Component {
  state = {
    html: "",
  };
  componentDidMount() {
    const src = this.props.src;
    const html = `
        &amp;lt;video autoPlay muted loop&amp;gt;
            &amp;lt;source src=${src} /&amp;gt;
        &amp;lt;/video&amp;gt;
      `;
    this.setState({ html });
  }
  render() {
    return &amp;lt;div dangerouslySetInnerHTML={{ __html: this.state.html }}&amp;gt;&amp;lt;/div&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component takse &lt;code&gt;src&lt;/code&gt; of a video as a prop and returns a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; with directly injected &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt; tag and it's attributes.&lt;/p&gt;

&lt;p&gt;Here's App code with new Video component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.jsx //

import mutedVideo from "./media/muted_vid.mp4";
import Video from "./components/Video";
import "./App.css";

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      {/* VIDEO */}
      &amp;lt;Video src={mutedVideo} /&amp;gt;
      {/* //VIDEO */}
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this gave me result that I expected in production output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vfqsg72bn6m6v76fov3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5vfqsg72bn6m6v76fov3.png" alt="Image description" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As an effect, the video can be easily played in the background.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT NOTE!&lt;/strong&gt; &lt;code&gt;dangerouslySetInnerHTML&lt;/code&gt; is not called "dangerous" for no reason. Even though it is safe to use it in this particular case, in many other cases it should not be used unless you really know what you're doing :). &lt;a href="https://blog.logrocket.com/using-dangerouslysetinnerhtml-in-a-react-application/" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is more detailed article about this property.&lt;/p&gt;

&lt;p&gt;Did you happen to deal with such an issue? Please share your story in the comment section.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/WojnickiAdam" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Importance of frontend performance optimization</title>
      <dc:creator>Adam Wojnicki</dc:creator>
      <pubDate>Wed, 30 Mar 2022 14:37:31 +0000</pubDate>
      <link>https://forem.com/adamwojnicki/importance-of-frontend-performance-optimization-4e05</link>
      <guid>https://forem.com/adamwojnicki/importance-of-frontend-performance-optimization-4e05</guid>
      <description>&lt;p&gt;You might have at least once such issue like me. Browsing posts on a social media app (whether it be Facebook, Instagram, Twitter), your eye got caught by a product ad post and you'd like to know something more about it. You click on the 'Learn more' CTA and a website starts loading... and it loads and it loads... seems to be loading forever. You get impatient and decide not to wait for this page to load and loose interest in the product after all.&lt;/p&gt;

&lt;p&gt;I had this kind of situation couple of times and it made me think about the importance of well optimized websites.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does frontend performance optimization mean?
&lt;/h3&gt;

&lt;p&gt;In simple words, it means making a website or app work fast, seamlessly with no stutters giving the best user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is it so important?
&lt;/h3&gt;

&lt;p&gt;The example above describes it pretty well. You would not like your clients/users/visitors get impatient while trying to reach you. &lt;/p&gt;

&lt;p&gt;Making your potential customers wait for too long makes them less interested in your offer. &lt;a href="https://www.cloudflare.com/learning/performance/more/website-performance-conversion-rates/"&gt;This article&lt;/a&gt; describes well how poor performance can affect your conversion rate.&lt;/p&gt;

&lt;p&gt;Poor site's performance can also make it harder to reach through search engines. Slow websites are indexed in lower positions by Google.&lt;/p&gt;

&lt;h3&gt;
  
  
  How can I measure my websites' performance?
&lt;/h3&gt;

&lt;p&gt;You may of course launch a website in your browser and check loading time with your watch, BUT... &lt;/p&gt;

&lt;p&gt;There are professional tools online that will measure websites' performance an will give you important notes and valuable feedback on what can be done in order to make it better and what are potential gains. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://web.dev/measure/"&gt;Google's PageSpeed Insights&lt;/a&gt; for example will give you overall performance and UX ratings. I personally recommend this tool since it can tell a lot about what can be improved to increase pages' performance, accessibility, SEO and best practices. &lt;/p&gt;

&lt;h3&gt;
  
  
  Common performance issues
&lt;/h3&gt;

&lt;p&gt;Here I'll describe most frequent issues that make your website slow and give you advice how can you address them:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Too large image sizes&lt;/strong&gt; - Pay attention on images sizes. Sometimes images downloaded from &lt;a href="https://unsplash.com/"&gt;Unsplash&lt;/a&gt; or &lt;a href="https://www.pexels.com/"&gt;Pexels&lt;/a&gt; can be up to 6000px wide or even bigger. This is definitely too much even for a desktop or laptop screen. Reducing image width down (with a graphic editor of your choice) to 1920px will make huge difference in file size. Reducing picture size for mobile devices an using &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images"&gt;responsive picture tags&lt;/a&gt; will increase performance on mobile devices. &lt;br&gt;
Additionally it's worth compressing image file sizes without quality loss. &lt;a href="https://tinypng.com/"&gt;TinyPNG&lt;/a&gt; will take care of it for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Too many css and script links in HTML file&lt;/strong&gt; - This may cause too many http requests being made in order to load necessary style sheets and JavaScript code. This can be handled by bundling multiple css files and js files into single ones. Doing it manually might take forever to do. This is why I started to get interested into JavaScript bundlers. The most popular ones are &lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt;, &lt;a href="https://parceljs.org/"&gt;Parcel&lt;/a&gt;, and my latest favorite pick - &lt;a href="https://vitejs.dev/"&gt;Vite&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Website waiting for all images to load&lt;/strong&gt; - By default an HTML document loads images from entire page. We don't need all images to load right at the start. This can be solved by lazy loading - a technique that makes images load as soon as they appear in the viewport. It is described in detail in an MDN doc &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading"&gt;here&lt;/a&gt;.     &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In my view, since majority of web traffic is being taken over by mobile devices, frontend optimization is a matter of high importance. Loading speed of a website can have huge impact on conversion rates, sales and customers' interest. Growing variety of tools lake bundlers and image compressors as well as new HTML features make it easier to make your website faster. &lt;/p&gt;

&lt;p&gt;In short words - Don't make your visitors wait too long.&lt;/p&gt;

&lt;p&gt;Cheers! :)&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/WojnickiAdam"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>html</category>
      <category>javascript</category>
      <category>css</category>
    </item>
    <item>
      <title>Simple Web Development Starter Pack for future projects</title>
      <dc:creator>Adam Wojnicki</dc:creator>
      <pubDate>Sat, 25 Jan 2020 17:01:56 +0000</pubDate>
      <link>https://forem.com/adamwojnicki/simple-web-development-starter-pack-for-future-projects-223k</link>
      <guid>https://forem.com/adamwojnicki/simple-web-development-starter-pack-for-future-projects-223k</guid>
      <description>&lt;h3&gt;
  
  
  Hello DEV community!
&lt;/h3&gt;

&lt;p&gt;I have created something that may make my (or your) life easier.&lt;br&gt;
It's a simple boilerplate to use for new projects.&lt;br&gt;
It includes basic setup with Parcel bundler, Babel, Sass and recommended ESLint configuration (can be easily configured with your favourite set).&lt;/p&gt;

&lt;p&gt;Thanks to it I no longer have to spend time on configuring project from the scratch. I found it a good idea to share GitHub repo here with DEV community.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/adamwojnicki/web-dev-starter"&gt;Here&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;Some of you may find it useful. &lt;br&gt;
I am also open for any feedback and opinion on this. I would really appreciate it.&lt;/p&gt;

&lt;p&gt;For now I wish you peaceful weekend and happy coding! :)&lt;/p&gt;

&lt;p&gt;Adam&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>html</category>
      <category>javascript</category>
      <category>css</category>
    </item>
  </channel>
</rss>
