<?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: Kasper Aamodt</title>
    <description>The latest articles on DEV Community by Kasper Aamodt (@kasperaamodt).</description>
    <link>https://dev.to/kasperaamodt</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%2F780623%2F0015b47c-f612-43b6-bd8f-84706a4372d1.jpeg</url>
      <title>DEV Community: Kasper Aamodt</title>
      <link>https://dev.to/kasperaamodt</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kasperaamodt"/>
    <language>en</language>
    <item>
      <title>Next JS - replace React with Preact</title>
      <dc:creator>Kasper Aamodt</dc:creator>
      <pubDate>Wed, 06 Apr 2022 10:53:59 +0000</pubDate>
      <link>https://dev.to/kasperaamodt/next-js-replace-react-with-preact-2f7i</link>
      <guid>https://dev.to/kasperaamodt/next-js-replace-react-with-preact-2f7i</guid>
      <description>&lt;p&gt;I made &lt;a href="https://aamodt.xyz"&gt;my website&lt;/a&gt; with &lt;a href="https://nextjs.org"&gt;Next JS&lt;/a&gt;, an excellent react framework for making production-ready websites. The downside to &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; frameworks is that they can ship a lot of js to the browser, which comes at a performance cost. Luckily, you can take some steps to optimize this, and replacing React with &lt;a href="https://preactjs.com/"&gt;Preact&lt;/a&gt; is one of them.&lt;/p&gt;

&lt;p&gt;Now I do not want to take any credit for this tip. I saw it in a &lt;a href="https://youtu.be/R59e1Vl5lO8?t=177"&gt;video&lt;/a&gt; from &lt;a href="https://vercel.com"&gt;Vercel's&lt;/a&gt; Director of Developer Relations, &lt;a href="https://twitter.com/leeerob"&gt;Lee Robinson&lt;/a&gt;. Switching it out is easy as long as you are not using &lt;a href="https://preactjs.com/guide/v8/differences-to-react/"&gt;any of these features&lt;/a&gt;. So without further ado, here is what you need to do:&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Preact:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;npm i preact&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add this snippet to your next.config.js file:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;module.exports = {&lt;br&gt;
  webpack: (config, { dev, isServer }) =&amp;amp;gt; {&lt;br&gt;
    // Replace React with Preact only in client production build&lt;br&gt;
    if (!dev &amp;amp;amp;&amp;amp;amp; !isServer) {&lt;br&gt;
      Object.assign(config.resolve.alias, {&lt;br&gt;
        react: 'preact/compat',&lt;br&gt;
        'react-dom/test-utils': 'preact/test-utils',&lt;br&gt;
        'react-dom': 'preact/compat',&lt;br&gt;
      });&lt;br&gt;
    }&lt;br&gt;
    return config;&lt;br&gt;
  },&lt;br&gt;
};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's it. Now Preact will be used in production builds instead of React. This website now uses Preact. Before the switch, 91.8 kb of js was loaded in the browser without caching. After the switch, 55.5 kb of js was loaded when testing the same site.&lt;/p&gt;

&lt;p&gt;NB! This implementation doesn’t currently work with React 18&lt;/p&gt;

&lt;p&gt;Originally posted on &lt;a href="https://aamodt.xyz/blog/next-js-replace-react-with-preact"&gt;aamodt.xyz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>nextjs</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Next JS – optimize blog posts</title>
      <dc:creator>Kasper Aamodt</dc:creator>
      <pubDate>Thu, 20 Jan 2022 16:38:06 +0000</pubDate>
      <link>https://dev.to/kasperaamodt/next-js-optimize-blog-posts-183m</link>
      <guid>https://dev.to/kasperaamodt/next-js-optimize-blog-posts-183m</guid>
      <description>&lt;p&gt;This website is built with Next JS and WordPress. I used WP for the blog part of the website, as there is no need to reinvent the wheel when it comes to a CMS. I worked with WP long before starting with Next JS, so it was a natural choice. &lt;/p&gt;

&lt;p&gt;Many writers, including me, use images in blogs posts. So when you import that blog post into Next, most people use dangerouslySetInnerHTML to render all the HTML. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div dangerouslySetInnerHTML={{__html: post.content}} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This usually works well, but it could be better. The problem here is that all of the images in the content will be in a standard &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt; tag, which is not recommended in Next JS. In Next JS, you should use the Image component from ‘next/image’, which optimizes images to make them smaller in size, therefore reducing the page size and ultimately the loading times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;html-react-parser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, how can you fix this? Well, as the heading suggests, html-react-parser. The parser converts an HTML string into one or more React elements. We can use this package instead of dangerouslySetInnerHTML to parse the HTML from our post and replace content when it is being parsed. Here is how you do it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install the parser&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i html-react-parser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Import and use&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Parsing the HTML string with the parse function, which is taking in the HTML string and options as parameters.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;div&amp;gt;&lt;br&gt;
    {parse(blog.content, replaceImage)}&lt;br&gt;
&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The options I pass to the parser, to target the img tag and replace it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const replaceImage = {&lt;br&gt;
    replace: ({ name, attribs, children }) =&amp;gt; {&lt;br&gt;
        if (name === "figure" &amp;amp;&amp;amp; /wp-block-image/.test(attribs.class)) {&lt;br&gt;
            return &amp;lt;&amp;gt;{domToReact(children, replaceImage)}&amp;lt;/&amp;gt;;&lt;br&gt;
        }&lt;br&gt;
        if (name === "img") {&lt;br&gt;
            return (&lt;br&gt;
                &amp;lt;Image&lt;br&gt;
                    src={attribs.src}&lt;br&gt;
                    width={attribs.width}&lt;br&gt;
                    height={attribs.height}&lt;br&gt;
                    alt={attribs.alt ? attribs.alt : "Blog post image"}&lt;br&gt;
                /&amp;gt;&lt;br&gt;
            );&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
};&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now all inserted images in the blog post use the Image component.&lt;br&gt;
&lt;a href="https://gist.github.com/kasperaamodt/055c6203d8761b4500c5fb108f0e4f03"&gt;Full Gist.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, why should you do this? As stated above, it will optimize all images in posts, leading to faster load times, and ultimately, better SEO. Of course, the package itself does increase the bundle size. But the slight increase that adds, compared to the savings on image sizes, is a net saving in bundle size.&lt;/p&gt;

&lt;p&gt;Originally posted on &lt;a href="https://aamodt.xyz/blog/next-js-optimize-blog-posts"&gt;aamodt.xyz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>nextjs</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
