<?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: Lukas Müller</title>
    <description>The latest articles on DEV Community by Lukas Müller (@lukem).</description>
    <link>https://dev.to/lukem</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%2F539639%2F23ced20a-73a8-4173-851e-76f5d7e38525.jpeg</url>
      <title>DEV Community: Lukas Müller</title>
      <link>https://dev.to/lukem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lukem"/>
    <language>en</language>
    <item>
      <title>Conditional rendering for responsiveness in SSR applications </title>
      <dc:creator>Lukas Müller</dc:creator>
      <pubDate>Mon, 25 Jan 2021 11:54:39 +0000</pubDate>
      <link>https://dev.to/lukem/conditional-rendering-for-responsiveness-in-ssr-applications-2188</link>
      <guid>https://dev.to/lukem/conditional-rendering-for-responsiveness-in-ssr-applications-2188</guid>
      <description>&lt;p&gt;I recently ran into a problem in a Gatsby site I'm building. In one of the pages, an element should only be visible in the desktop view, not on mobile devices. To achieve this, I wrote a utility function ...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const isMobile = () =&amp;gt; window.innerWidth &amp;lt; 768
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... and then used that to conditionally render the element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ !isMobile() &amp;amp;&amp;amp; &amp;lt;Element /&amp;gt; }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works fine in the local preview of the site, but I ran into problem in the statically built version. &lt;code&gt;&amp;lt;Element&amp;gt;&lt;/code&gt; received faulty classnames from its immediate sibling. I wasn't able to find out the exact cause of this but I inferred it had something to do with SSR (server side rendering) + conditional rendering based on the following:&lt;/p&gt;

&lt;p&gt;1) the problem doesn't happen in the client side rendered version&lt;br&gt;
2) the only affected element is the conditionally rendered one&lt;/p&gt;

&lt;p&gt;My solution was to go away from conditional rendering (in the React sense) and just not display the element in mobile breakpoints.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.element {
    &amp;amp;--invisible-on-mobile {
        @media (max-width: 768px) {
          display: none;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works fine and to me it seems that this is also the better way to approach this. Here's why:&lt;/p&gt;

&lt;p&gt;Based on my understanding, when using SSR, upon page load, the browser receives a proper HTML document filled with the full content of the page. This is different to a classic client side rendered SPA which is empty on page load except for a root container into which the frontend framework (React, Vue, etc.) renders the application. In this approach, it makes sense to conditionally render things because the browser starts with nothing and asks itself the question "I have not rendered anything yet. What do I want to show the user?". It can then choose what to render based on the applications' state. &lt;br&gt;
In a static build, however, the whole content is there already, so the browser renders it immediately. The question then becomes: "What do I hide from the user?" This seems easier and cleaner to solve with CSS.&lt;/p&gt;

&lt;p&gt;Is there a flaw in my understanding of SSR? Or is this indeed the better approach to what I'm trying to achieve?&lt;/p&gt;

</description>
      <category>react</category>
      <category>ssr</category>
      <category>responsive</category>
      <category>gatsby</category>
    </item>
    <item>
      <title>My functional approach to HackerRank's 'Mini-Max Sum' challenge</title>
      <dc:creator>Lukas Müller</dc:creator>
      <pubDate>Thu, 24 Dec 2020 13:17:57 +0000</pubDate>
      <link>https://dev.to/lukem/my-functional-approach-to-hackerrank-s-mini-max-sum-challenge-h8f</link>
      <guid>https://dev.to/lukem/my-functional-approach-to-hackerrank-s-mini-max-sum-challenge-h8f</guid>
      <description>&lt;p&gt;Problem: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. &lt;/p&gt;

&lt;p&gt;Link: &lt;a href="https://www.hackerrank.com/challenges/mini-max-sum/problem"&gt;https://www.hackerrank.com/challenges/mini-max-sum/problem&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My thought process went like this:&lt;/p&gt;

&lt;p&gt;1) The function will receive an array of 5 inputs and should calculate the sum of 4 of those. Thus, 1 value of the array should be removed (either the highest or the lowest). We can use &lt;code&gt;Math.max&lt;/code&gt; and &lt;code&gt;Math.min&lt;/code&gt; to find out which value needs to be removed.&lt;/p&gt;

&lt;p&gt;2) The array could have identical values, in that case only one of them should be removed. For this, we can use &lt;code&gt;Array.indexOf&lt;/code&gt; which only returns the index of the &lt;strong&gt;first&lt;/strong&gt; match it finds.&lt;/p&gt;

&lt;p&gt;3) To remove the value from the array, we can use &lt;code&gt;Array.splice&lt;/code&gt;. Unfortunately, that function mutates the original array. Due to the fact that we need to find the minimum sum AND the maximum sum, we need to touch the array twice, thus mutation is not an option. And aside from that, mutation is against the rules of functional programming. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Is there a different &lt;code&gt;Array&lt;/code&gt; function I could have used for this?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;4) To get the sum of the other values in the array, we can use &lt;code&gt;Array.reduce&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;5) The processes of getting the minimum and maximum sum are equal exact for one function (&lt;code&gt;Math.max&lt;/code&gt; for minimum and &lt;code&gt;Math.min&lt;/code&gt; for maximum (opposite because the functions are used to &lt;strong&gt;remove&lt;/strong&gt; the number that should be excluded from the calculation)). This calls for a curried function, meaning a function that receives as a parameter either &lt;code&gt;Math.max&lt;/code&gt; or &lt;code&gt;Math.min&lt;/code&gt; and returns the function that calculates the minimum or maximum sum, depending on the passed function. &lt;/p&gt;

&lt;p&gt;This resulted in the following solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const removeNumberFromArrayAndSumRest = removalFn =&amp;gt; arr =&amp;gt; {
    const tmp = [].concat(arr);
    const toBeRemoved = removalFn(...tmp);
    tmp.splice(tmp.indexOf(toBeRemoved), 1);
    return tmp.reduce((a, b) =&amp;gt; a + b);
}

// Complete the miniMaxSum function below.
const miniMaxSum = (arr) =&amp;gt; {    
    console.log(`${removeNumberFromArrayAndSumRest(Math.max)(arr)} ${removeNumberFromArrayAndSumRest(Math.min)(arr)}`);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This might not be the best approach, but I'm pretty satisfied with it. Functional programming really is a marvel. &lt;/p&gt;

&lt;p&gt;Does anyone here use HackerRank? What's your approach to this challenge?&lt;/p&gt;

</description>
      <category>functional</category>
      <category>node</category>
      <category>javascript</category>
      <category>hackerrank</category>
    </item>
    <item>
      <title>My ridiculously unreadable but concise solution to the Compare Tuples challenge on HackerRank</title>
      <dc:creator>Lukas Müller</dc:creator>
      <pubDate>Mon, 14 Dec 2020 18:44:24 +0000</pubDate>
      <link>https://dev.to/lukem/my-ridiculously-unreadable-but-concise-solution-to-the-compare-tuples-challenge-on-hackerrank-44hf</link>
      <guid>https://dev.to/lukem/my-ridiculously-unreadable-but-concise-solution-to-the-compare-tuples-challenge-on-hackerrank-44hf</guid>
      <description>&lt;p&gt;Here's the challenge: &lt;a href="https://www.hackerrank.com/challenges/compare-the-triplets/problem"&gt;https://www.hackerrank.com/challenges/compare-the-triplets/problem&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the definition of the challenge:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.&lt;/p&gt;

&lt;p&gt;The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).&lt;/p&gt;

&lt;p&gt;The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;If a[i] &amp;gt; b[i], then Alice is awarded 1 point.
If a[i] &amp;lt; b[i], then Bob is awarded 1 point.
If a[i] = b[i], then neither person receives a point.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Comparison points is the total points a person earned.&lt;/p&gt;

&lt;p&gt;Given a and b, determine their respective comparison points. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My initial approach was a simple if/else loop, filling the 2 positions of a previously defined array, and then returning that array.&lt;/p&gt;

&lt;p&gt;Something along the lines of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function compareTriplets(a, b) {
  let counterA = 0,
      counterB = 0;
  for (let i=0; i &amp;lt; a.length; i++){
      if (a[i] &amp;gt; b[i]) {
          counterA++;
      } else if (a[i] &amp;lt; b[i]) {
          counterB++;
      }
  }

  return [counterA, counterB];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is obviously a fine enough solution and what I would probably write in a real project. &lt;/p&gt;

&lt;p&gt;But I thought: "I bet can write this in one line". And so I did:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const compareTriplets = (a, b) =&amp;gt; [[a, b], [b, a]].map((c, i) =&amp;gt; c[0].reduce((acc, _curr, i) =&amp;gt; acc + (c[0][i] &amp;gt; c[1][i]), 0));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it works! And I'm sure there's ways to make it even more concise. And there's definitely a way to extend it to accept a hypothetically infinite (within the limits of JS) amount of arrays. &lt;/p&gt;

&lt;p&gt;As said before, I would never write something like this for a real project. The reason is that it's completely unreadable. It would probably take me 10 minutes to understand the code I produced if I came across it in a legacy project or I wouldn't even get that far because I would think to myself "which self-important twat wrote this?" and close my editor out of frustration. &lt;/p&gt;

&lt;p&gt;Anyway, it was a fun mind challenge and I think I might understand &lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.reduce()&lt;/code&gt; a bit better now :) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hackerrank</category>
      <category>arrays</category>
    </item>
    <item>
      <title>TIL: Adding a maintenance redirect to a Gatsby page hosted on Netlify</title>
      <dc:creator>Lukas Müller</dc:creator>
      <pubDate>Sun, 13 Dec 2020 16:32:17 +0000</pubDate>
      <link>https://dev.to/lukem/til-adding-a-maintenance-redirect-to-a-gatsby-page-hosted-on-netlify-2ncp</link>
      <guid>https://dev.to/lukem/til-adding-a-maintenance-redirect-to-a-gatsby-page-hosted-on-netlify-2ncp</guid>
      <description>&lt;p&gt;Today, I needed to take a website offline, because it wasn't ready to be shown to the world yet. What I wanted was to show a "we're under maintenance" screen, when the user visits the site. It should also automatically redirect any traffic to that screen, so if the user tried to visit &lt;code&gt;/subpage&lt;/code&gt;, they would be redirected to &lt;code&gt;/maintenance&lt;/code&gt;. Here's how I did it with my Gatsby page hosted on Netlify.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1:
&lt;/h3&gt;

&lt;p&gt;Create a branch in my repository where I will add the maintenance screen. &lt;br&gt;
I called it &lt;code&gt;maintenance&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout -b maintenance&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;

&lt;p&gt;Make Netlify deploy this newly created branch. By default, Netlify only deploys your &lt;code&gt;production branch&lt;/code&gt;. That's usually &lt;code&gt;master&lt;/code&gt;, or it was in my case. To change this, I go to &lt;code&gt;Settings &amp;gt; Build &amp;amp; Deploy &amp;gt; Deploy contexts&lt;/code&gt;, click edit and choose &lt;code&gt;Let me add individual branches&lt;/code&gt; under &lt;code&gt;Branch deploys&lt;/code&gt;. Then I add my branch name &lt;code&gt;maintenance&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3wQFGeb6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rzuorkkaai8r2us8iig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3wQFGeb6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rzuorkkaai8r2us8iig.png" alt="Screenshot of the described process"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3:
&lt;/h3&gt;

&lt;p&gt;Add the screen to my Gatsby project. &lt;br&gt;
I did this by creating a new folder called &lt;code&gt;maintenance&lt;/code&gt; in &lt;code&gt;pages&lt;/code&gt; and added an &lt;code&gt;index.tsx&lt;/code&gt; and a &lt;code&gt;styles.scss&lt;/code&gt;. This page pretty much just says "Here's going to be the new home of this project", has links to social media and basic styling. Gatsby will automatically create a route for this, so when I visit &lt;code&gt;page-name.com/maintenance&lt;/code&gt;, I get the screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mYnVieVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wfyer8u8dqfszrns3lp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mYnVieVS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wfyer8u8dqfszrns3lp1.png" alt="Screenshot of file structure"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 4:
&lt;/h3&gt;

&lt;p&gt;Add the redirect.&lt;br&gt;
One way of getting Netlify to do the redirect I want is to add a file called &lt;code&gt;_redirects&lt;/code&gt; to the directory that Netlify uses to serve the website and put the redirects in there. In the case of Gatsby which has its own build process, it's not trivial to get the file into the &lt;code&gt;public&lt;/code&gt; folder, where it needs to be. &lt;br&gt;
To get Gatsby to put the file there, I created a folder called &lt;code&gt;static&lt;/code&gt; (on the root of my project) and put the file in there. Gatsby takes all these files and puts them into &lt;code&gt;public&lt;/code&gt; during the build process. &lt;br&gt;
The content of the file is very simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* /maintenance 301!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This redirects any traffic on the page to &lt;code&gt;/maintenance&lt;/code&gt; with the status code &lt;code&gt;301&lt;/code&gt;. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DrtQKYFo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5epmcfdar6b5zku86ohm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DrtQKYFo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5epmcfdar6b5zku86ohm.png" alt="Screenshot of the location of the redirects file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These changes are pushed to the &lt;code&gt;maintenance&lt;/code&gt; branch.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5:
&lt;/h3&gt;

&lt;p&gt;Deploy the published branch. &lt;br&gt;
I open the deployment status page with the deploy log. There, I can find a button that says "publish deploy".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KtFLH4o---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8tg1s9otc348g3zodu8o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KtFLH4o---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8tg1s9otc348g3zodu8o.png" alt="Screenshot of Netlify's UI &amp;quot;publish deploy&amp;quot;"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will make the branch &lt;code&gt;maintenance&lt;/code&gt; the base of my production site. Now, when I visit the main URL e.g. &lt;code&gt;https://page-name.com&lt;/code&gt;, I see the maintenance screen.&lt;/p&gt;

&lt;p&gt;To undo this, I can just go to one of the older deploys and click "publish deploy" there. This will bring back the master branch to the production site. I can obviously also just make a new commit to the master branch, go into that deploy log and press "publish deploy" there. &lt;/p&gt;

&lt;h3&gt;
  
  
  Further Notes:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;My site is connected to a Contentful instance. When content is updated, a webhook is triggered and a new deployment starts. In Netlify, by default, auto-publishing for the &lt;code&gt;master&lt;/code&gt; branch (or whatever you've set as your production branch) is enabled. This will, after having built the site, set the production site to the &lt;code&gt;master&lt;/code&gt; branch again, effectively undoing Step 5. To stop Netlify from doing this, just press the "Stop auto publishing" button in the deploys list page in the Netlify UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IB3qubxD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oeo8vdu9bdzdul1xg3nx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IB3qubxD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oeo8vdu9bdzdul1xg3nx.png" alt='Screenshot of UI containing "Stop auto publishing" button'&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://community.netlify.com/t/support-guide-what-s-the-easiest-way-to-create-a-temporary-maintenance-page-for-my-site/338"&gt;https://community.netlify.com/t/support-guide-what-s-the-easiest-way-to-create-a-temporary-maintenance-page-for-my-site/338&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file"&gt;https://docs.netlify.com/routing/redirects/#syntax-for-the-redirects-file&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.netlify.com/site-deploys/overview/#branch-deploy-controls"&gt;https://docs.netlify.com/site-deploys/overview/#branch-deploy-controls&lt;/a&gt;&lt;br&gt;
&lt;a href="https://levelup.gitconnected.com/how-to-add-netlify-redirects-to-a-gatsby-site-ae678518cc91?gi=f922113a6c6f"&gt;https://levelup.gitconnected.com/how-to-add-netlify-redirects-to-a-gatsby-site-ae678518cc91?gi=f922113a6c6f&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>netlify</category>
    </item>
  </channel>
</rss>
