<?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: Sebastian Stamm</title>
    <description>The latest articles on DEV Community by Sebastian Stamm (@sebastianstamm).</description>
    <link>https://dev.to/sebastianstamm</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%2F204973%2Fff4a9601-76d4-46f5-a785-b8b7287a9094.jpg</url>
      <title>DEV Community: Sebastian Stamm</title>
      <link>https://dev.to/sebastianstamm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sebastianstamm"/>
    <language>en</language>
    <item>
      <title>I made an outdoor adventure game to motivate me to go outside more</title>
      <dc:creator>Sebastian Stamm</dc:creator>
      <pubDate>Sat, 03 Sep 2022 09:25:05 +0000</pubDate>
      <link>https://dev.to/sebastianstamm/i-made-an-outdoor-adventure-game-to-motivate-me-to-go-outside-more-1c8g</link>
      <guid>https://dev.to/sebastianstamm/i-made-an-outdoor-adventure-game-to-motivate-me-to-go-outside-more-1c8g</guid>
      <description>&lt;p&gt;I was bored with the usual routes for my daily walk, so I made a "Random Adventure Generator" that creates a bunch of random coordinates to reach. When you reach one of the coordinates you level up and then a new set of coordinates is generated. For the first level there are a bunch of coordinates nearby, but for later levels you need to travel farther and since it's all random, you will explore places you probably have never been to.&lt;/p&gt;

&lt;p&gt;Here is the link to the game: &lt;a href="https://earthquest.net"&gt;https://earthquest.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The locations of the randomly generated coordinates change every day so that you can go outside every day and have a new experience and discover new places. Also, all players have the same coordinates, so when playing with your friends, you all can go to the same places!&lt;/p&gt;

&lt;p&gt;I made a small trailer video (Animations and all are also pure Javascript):&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/OpxVnSCkEqI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Before building EarthQuest, I played some other geolocation games, but was always annoyed that they require a lot of interaction. Maybe it's just me, but I find it awkward to stand around outside and having to interact with my phone for several minutes before moving on to the next goal. So for EarthQuest it's just: "Go to this coordinate; touch the Level Up button; Go to the coordinate for the next level". This let's you focus on your surroundings and get moving.&lt;/p&gt;

&lt;p&gt;The game runs completely in the web browser. At first I was considering building a native app, but with the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API"&gt;Geolocation API&lt;/a&gt; I can just use plain old Javascript. And as an additional bonus I don't have to deal with the walled gardens that are the app stores. Plus, it's a PWA (Progressive Web App), so you can still install it on the Homescreen.&lt;/p&gt;

&lt;p&gt;This is one of my most ambitious projects so far, so please try it out and let me know what you think!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>Be careful with console.log when using React.StrictMode</title>
      <dc:creator>Sebastian Stamm</dc:creator>
      <pubDate>Sat, 05 Dec 2020 11:32:31 +0000</pubDate>
      <link>https://dev.to/sebastianstamm/be-careful-with-console-log-when-using-react-strictmode-ah9</link>
      <guid>https://dev.to/sebastianstamm/be-careful-with-console-log-when-using-react-strictmode-ah9</guid>
      <description>&lt;p&gt;I recently came across an interesting phenomenon that can be reproduced with this minimal example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;render count:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StrictMode&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/React.StrictMode&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;,
&lt;/span&gt;  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A novice developer might look at this code and think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Okay, we are rendering the App component, this increases the &lt;code&gt;i&lt;/code&gt; counter, prints it to the console and returns it. So the result is &lt;code&gt;"render count: 1"&lt;/code&gt; in the browser log and &lt;code&gt;1&lt;/code&gt; is rendered on screen.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A more experienced developer knows that &lt;code&gt;React.StrictMode&lt;/code&gt; causes the render function to be called twice. The idea is that this will help developers identify unintended side-effects in methods that should be side-effect free. You can read more about that here: &lt;a href="https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects"&gt;https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So they might say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;React intentionally calls the render function twice, so &lt;code&gt;i&lt;/code&gt; will be increased twice. We will see two entries in the browser console &lt;code&gt;"render count: 1"&lt;/code&gt; and then &lt;code&gt;"render count: 2"&lt;/code&gt; and the screen will render &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you run this code however, &lt;strong&gt;there is only one console log saying &lt;code&gt;"render count: 1"&lt;/code&gt;, but the screen shows &lt;code&gt;2&lt;/code&gt;&lt;/strong&gt;. You can see this for yourself here: &lt;a href="https://codesandbox.io/s/strict-mode-logs-pynlw?file=/src/index.js"&gt;https://codesandbox.io/s/strict-mode-logs-pynlw?file=/src/index.js&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is going on?
&lt;/h2&gt;

&lt;p&gt;The only place where the value of &lt;code&gt;i&lt;/code&gt; is increased is directly followed by a &lt;code&gt;console.log&lt;/code&gt; statement, so if &lt;code&gt;i&lt;/code&gt; would be set to 2, it should show up in the console.&lt;/p&gt;

&lt;p&gt;It turns out that React is hijacking &lt;code&gt;console.log&lt;/code&gt; and is replacing it with a function that does nothing on the second render pass. &lt;a href="https://github.com/facebook/react/blob/0db61a08befe6406aa93568708224d1cca2aff7d/packages/shared/ConsolePatchingDev.js"&gt;Here is the code&lt;/a&gt; that does that, &lt;a href="https://github.com/facebook/react/pull/18547"&gt;this is the Pull Request&lt;/a&gt; that introduced this behavior and here is an open &lt;a href="https://github.com/facebook/react/pull/19710"&gt;Pull Request to add an opt-out option&lt;/a&gt; to the dev tools.&lt;/p&gt;

&lt;p&gt;In our example the render function is indeed executed twice, which leads to &lt;code&gt;i&lt;/code&gt; being 2 when it is rendered on screen, but only the first &lt;code&gt;console.log&lt;/code&gt; call actually logs to the console so it looks like the render function is only called once and &lt;code&gt;i&lt;/code&gt; is only set to 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personal Opinion
&lt;/h2&gt;

&lt;p&gt;My personal opinion is that the decision to disable the logs for the second render pass was wrong. Developers not familiar with StrictMode will try to use console.log debugging to figure out why their functions are executed twice. If the logs for the second pass don't show up in the console, developers might falsely assume that the function is only executed once.&lt;/p&gt;

&lt;p&gt;I would even argue that it undermines the concept of StrictMode. Printing to the console is a side-effect that StrictMode is supposed to expose. Intentionally obscuring it not only makes debugging harder, it prevents developers finding rogue log statements in render phase lifecycle methods that could cause problems in production once concurrent mode lands.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>This Image Is Also a Valid Javascript File</title>
      <dc:creator>Sebastian Stamm</dc:creator>
      <pubDate>Sun, 06 Sep 2020 08:39:12 +0000</pubDate>
      <link>https://dev.to/sebastianstamm/this-image-is-also-a-valid-javascript-file-5fol</link>
      <guid>https://dev.to/sebastianstamm/this-image-is-also-a-valid-javascript-file-5fol</guid>
      <description>&lt;p&gt;Images are usually stored as binary files, while a Javascript file is basically just text. Both have to follow their own rules: Images have a concrete file format, encoding the data in a certain way. Javascript files have to follow a specific syntax in order to be executable. I wondered: Can I create an image file that has valid Javascript syntax, so it is also executable?&lt;/p&gt;

&lt;p&gt;Before you read on, I highly recommend checking out this code sandbox with the result of my experimentation: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/executable-gif-8yq0j?file=/index.html" rel="noopener noreferrer"&gt;https://codesandbox.io/s/executable-gif-8yq0j?file=/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to check out the image and inspect it yourself, you can download it here: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://executable-gif.glitch.me/image.gif" rel="noopener noreferrer"&gt;https://executable-gif.glitch.me/image.gif&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Choosing the Right Image Type
&lt;/h1&gt;

&lt;p&gt;Unfortunately, images contain a lot of binary data which will throw an error if interpreted as Javascript. So my first idea was: What if we just put all the image data in a large comment, like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="cm"&gt;/*ALL OF THE BINARY IMAGE DATA*/&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That would be a valid Javascript file. However, image files need to start with a specific sequence of bytes; a file header that is specific to the image format. PNG files for example always have to start with the byte sequence &lt;em&gt;89 50 4E 47 0D 0A 1A 0A&lt;/em&gt;. If an image would start with &lt;code&gt;/*&lt;/code&gt;, it would not be a valid image file anymore.&lt;/p&gt;

&lt;p&gt;This file header lead to the next idea: What if we could use this byte sequence as variable name and have a huge string assignment like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;PNG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;`ALL OF THE BINARY IMAGE DATA`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are using template strings instead of the normal &lt;code&gt;"&lt;/code&gt; or &lt;code&gt;'&lt;/code&gt; strings because the binary data could contain linebreaks and template strings are better at dealing with those.&lt;/p&gt;

&lt;p&gt;Unfortunately, most byte sequences for image file headers contain unprintable characters that are not allowed in variable names. But there is one image format we can use: GIF. The GIF header block is &lt;em&gt;47 49 46 38 39 61&lt;/em&gt;, which conveniently spells &lt;strong&gt;GIF89a&lt;/strong&gt; in ASCII, a perfectly legal variable name!&lt;/p&gt;

&lt;h1&gt;
  
  
  Choosing the Right Image Dimensions
&lt;/h1&gt;

&lt;p&gt;Now that we found an image format that starts with a valid variable name, we need to add the equals and backtick characters. The next four bytes of the file are therefore: &lt;em&gt;3D 09 60 04&lt;/em&gt;&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%2Fi%2F0ogcfqgdgvlr7v0vw6kp.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%2Fi%2F0ogcfqgdgvlr7v0vw6kp.png" alt="First bytes of the image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the gif format, the four bytes following the header specify the dimensions of the image. We have to fit &lt;em&gt;3D&lt;/em&gt; (the equals sign) and &lt;em&gt;60&lt;/em&gt; (the backtick that opens the string) in there. GIF uses little endian so the second and fourth character have a huge influence on image dimensions. We want to keep them as small as possible to not end up with an image that is tens of thousands pixels wide. Therefore we store the big bytes &lt;em&gt;3D&lt;/em&gt; and &lt;em&gt;60&lt;/em&gt; in the least significant bytes.&lt;/p&gt;

&lt;p&gt;The second byte of the image width needs to be a valid whitespace character, because this would be the space between the equals sign and the beginning of the string &lt;code&gt;GIF89a= `...&lt;/code&gt;. Keep in mind that the hexcode of the characters should be as small as possible, or the image would be huge.&lt;/p&gt;

&lt;p&gt;The smallest whitespace character is &lt;em&gt;09&lt;/em&gt;, the horizontal tab. This gives us an image width of &lt;em&gt;3D 09&lt;/em&gt;, which in little endian translates to 2365; a bit wider than I would have liked, but still reasonable.&lt;/p&gt;

&lt;p&gt;For the second height byte, we can just choose something that produces a nice aspect ratio. I chose &lt;em&gt;04&lt;/em&gt;, which produces a height of &lt;em&gt;60 04&lt;/em&gt;, or 1120.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting our own script in there
&lt;/h1&gt;

&lt;p&gt;Right now, our executable gif does not really do anything. It just assigns a large string to the global variable &lt;code&gt;GIF89a&lt;/code&gt;. We want something interesting to happen! Most of the data inside the GIF is for encoding the image, so if we try to add Javascript in there, we would probably end up with a very corrupted image. But for some reason, the GIF format contains something called a &lt;strong&gt;Comment Extension&lt;/strong&gt;. This is a place to store some metadata that will not be interpreted by the GIF decoder - a perfect place for our Javascript logic.&lt;/p&gt;

&lt;p&gt;This comment extension comes right after the GIF color table. Since we can put any content in there, we can easily close the &lt;strong&gt;GIF89a&lt;/strong&gt; string, add all our Javascript and then start a multiline comment block, so the rest of the image does not interfere with the Javascript parser. &lt;/p&gt;

&lt;p&gt;All in all, our file could then look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;GIF89a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;` BINARY COLOR TABLE DATA ... COMMENT BLOCK:

`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Javascript!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="cm"&gt;/*

REST OF THE IMAGE */&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There is a small restriction: While the comment block itself can be of any size, it is composed of multiple sub-blocks, each of which has a maximum size of 255. Between the subblocks is a single byte that indicates the length of the next subblock. So in order to fit a larger script in there, it has to be divided into smaller chunks, like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Javascript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="cm"&gt;/*0x4A*/&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;another subblock&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="cm"&gt;/*0x1F*/&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The hexcodes in the comments are the bytes that indicate the size of the next subblock. They are irrelevant for the Javascript, but required for the GIF file format. In order to prevent them from interfering with the rest of the code, they have to be in comments. I wrote a small script that processes the script chunks and adds them to the image file: &lt;a href="https://gist.github.com/SebastianStamm/c2433819cb9e2e5af84df0904aa43cb8" rel="noopener noreferrer"&gt;https://gist.github.com/SebastianStamm/c2433819cb9e2e5af84df0904aa43cb8&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Cleaning up the Binary
&lt;/h1&gt;

&lt;p&gt;Now that we have the basic structure down, we need to make sure that the binary image data does not ruin our syntax. As explained in the previous section, the file has three sections: The first one is an assignment to the variable &lt;strong&gt;GIF89a&lt;/strong&gt;, the second one is the Javascript code and the third one is a multiline comment.&lt;/p&gt;

&lt;p&gt;Let's have a look at the first part, the variable assignment:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;GIF89a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;` BINARY DATA `&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the binary data would contain the character &lt;code&gt;`&lt;/code&gt; or the character combination &lt;code&gt;${&lt;/code&gt; we are in trouble because this would either end the template string or produce an invalid expression. The fix here is quite easy: Just change the binary data! E.g. instead of using the &lt;code&gt;`&lt;/code&gt; character (hexcode &lt;em&gt;60&lt;/em&gt;), we could use the character &lt;code&gt;a&lt;/code&gt; (hexcode &lt;em&gt;61&lt;/em&gt;). Since this part of the file contains the color table, it would result in some of the colors being slightly off, e.g. using the color &lt;code&gt;#286148&lt;/code&gt; instead of &lt;code&gt;#286048&lt;/code&gt;. It is unlikely that someone will notice the difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fighting the corruption
&lt;/h2&gt;

&lt;p&gt;At the end of the Javascript code, we opened a multiline comment in order to make sure the binary image data does not interfere with the Javascript parsing:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Script done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="cm"&gt;/*BINARY IMAGE DATA ...


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the image data would contain the character sequence &lt;code&gt;*/&lt;/code&gt;, the comment would end prematurely, which would result in an invalid Javascript file. Here again we can manually change one of the two characters so they no longer end the comment. However, since we are now in the encoded image section, this will result in a corrupted image, like this:&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%2Fi%2Fyyiadl1lq7y5pacghd7f.gif" 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%2Fi%2Fyyiadl1lq7y5pacghd7f.gif" alt="Corrupted Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In extreme cases the image could not be displayed at all. By carefully choosing which bit to flip I was able to minimize the corruption. Fortunately there were only a handful of instances of the harmful &lt;code&gt;*/&lt;/code&gt; combination to deal with. There is still a bit of corruption visible in the final image, e.g. at the bottom of the "Valid Javascript File" string, but overall I am pretty happy with the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ending the File
&lt;/h2&gt;

&lt;p&gt;The last operation we have to perform is at the end of the file. The file has to end with the bytes &lt;em&gt;00 3B&lt;/em&gt;. So we have to end our comment earlier. Since this is the end of the file and any potential corruption would not be very visible, I just ended the multi block comment and added a single line comment so that the end of the file would not cause any problems when parsing:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="cm"&gt;/* BINARY DATA*/&lt;/span&gt;&lt;span class="c1"&gt;// 00 3B&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Convincing the Browser to Execute an Image&lt;br&gt;
&lt;/h1&gt;

&lt;p&gt;Now, after all this, we finally have a file that is both an image as well as a valid Javascript file. But there is one last challenge we have to overcome: If we upload the image to a server and try to use it in a script tag, we would most likely see an error like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Refused to execute script from '&lt;a href="http://localhost:8080/image.gif" rel="noopener noreferrer"&gt;http://localhost:8080/image.gif&lt;/a&gt;' because its MIME type ('image/gif') is not executable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the browser rightfully says "That's an image! I am not going to execute that!". Which in most cases is a good mindset to have. But we want to execute it anyway. Our solution here is to just not tell the browser that it's an image. For that I wrote a small server that serves the image without any header information.&lt;/p&gt;

&lt;p&gt;Without the MIME type information from the header, the browser does not know that it is an image and just does what fits the context best: Display it as image in an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag, or execute it as Javascript in a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;h1&gt;
  
  
  But ... why?
&lt;/h1&gt;

&lt;p&gt;That is something I have yet to figure out. It was a nice mental challenge to make this stuff work, but if you can think of any scenarios where this might actually be useful, please let me know!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Screen Recording in 10 lines of Vanilla JS</title>
      <dc:creator>Sebastian Stamm</dc:creator>
      <pubDate>Sun, 08 Mar 2020 11:13:41 +0000</pubDate>
      <link>https://dev.to/sebastianstamm/screen-recording-in-10-lines-of-vanilla-js-3bo8</link>
      <guid>https://dev.to/sebastianstamm/screen-recording-in-10-lines-of-vanilla-js-3bo8</guid>
      <description>&lt;p&gt;Let’s have a look at how we can capture and record your users screen. Not just your page, but also other tabs of the users browser, the desktop and even other applications. And we will do that without browser plugins or huge libraries. Instead, we just need 10 lines of Vanilla Javascript.&lt;/p&gt;

&lt;p&gt;To achieve this, we will use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Media_Streams_API"&gt;Media Capture and Streams API&lt;/a&gt;. It’s related to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API"&gt;WebRTC API&lt;/a&gt;, but for now we ignore all the peer-to-peer streaming between browsers and just do very bare-bones recording.&lt;/p&gt;

&lt;h1&gt;
  
  
  Full Example
&lt;/h1&gt;

&lt;p&gt;While we could send the recording to a server to store or process it, for this blog post we just capture it and then play it back to the user in a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video"&gt;&amp;lt;video&amp;gt;&lt;/a&gt; tag. You can find the complete example here: &lt;a href="https://codesandbox.io/s/sharp-mestorf-qumzf"&gt;https://codesandbox.io/s/sharp-mestorf-qumzf&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;To try it out, click the "Start Recording" button, select which screen you want to share, perform some actions and then click the "Stop Recording" button.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/sharp-mestorf-qumzf"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You might notice that the example contains more than 10 lines of Javascript. This is because it also contains a bit more code to deal with the start and stop buttons. The recording logic can be found in the &lt;code&gt;startRecording&lt;/code&gt; function, starting from line 6. In summary, this function performs these three steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create&lt;/strong&gt; a video stream of the users desktop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record&lt;/strong&gt; this stream&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Convert&lt;/strong&gt; the recording to transmit it to the server or show it in the &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt; tag&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s look at each step in detail:&lt;/p&gt;

&lt;h1&gt;
  
  
  Create a Video Stream
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mediaDevices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getDisplayMedia&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;video&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;mediaSource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;screen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s just a single function: &lt;code&gt;getDisplayMedia&lt;/code&gt;. Calling this opens a dialog for the user to choose which screen to record from (if they have multiple displays). They can also choose to just record a specific application or browser tab. Two things to keep in mind here: The user has to actively allow the sharing, so you cannot use this feature to spy on your users. Also, it returns a promise, so make sure to &lt;code&gt;await&lt;/code&gt; it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Record the Stream
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;recorder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MediaRecorder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="nx"&gt;recorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ondataavailable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;recorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder"&gt;MediaRecorder API&lt;/a&gt; to capture the stream we got from the previous step. Since video streams can get quite big, the recorder can periodically call &lt;code&gt;ondataavailable&lt;/code&gt;. For now, we store each video chunk in an array and will deal with it in the next step. After setting up the data handling, we start the recording.&lt;/p&gt;

&lt;h1&gt;
  
  
  Convert the Recording to a Blob
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;recorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onstop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;completeBlob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createObjectURL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;completeBlob&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At some point, we need to call &lt;code&gt;recorder.stop();&lt;/code&gt; In the example, this happens when you click the "Stop Recording" button. This will call the &lt;code&gt;onstop&lt;/code&gt; event handler of the recorder. There we take the chunks from the previous step and convert them into a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"&gt;Blob&lt;/a&gt;. And then you can do whatever with it.&lt;/p&gt;

&lt;p&gt;You could send it to your server as part of your "Submit Feedback" functionality. You could upload it to Youtube. Here, we just play the recording back to the user by constructing an object url and using it as &lt;code&gt;src&lt;/code&gt; attribute for the video tag.&lt;/p&gt;




&lt;p&gt;And there we have it, with just 10 lines of Javascript (plus a little bit more for the recording controls), we were able to capture the users screen. I trust you to use this power for good, not evil.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>This blog post is written entirely in the &amp;lt;head&amp;gt;*</title>
      <dc:creator>Sebastian Stamm</dc:creator>
      <pubDate>Mon, 09 Sep 2019 18:36:01 +0000</pubDate>
      <link>https://dev.to/sebastianstamm/this-blog-post-is-written-entirely-in-the-head-1io6</link>
      <guid>https://dev.to/sebastianstamm/this-blog-post-is-written-entirely-in-the-head-1io6</guid>
      <description>&lt;p&gt; *Unfortunately, dev.to embeds blog posts in the body, so you need to click &lt;a href="https://head-blog.glitch.me/"&gt;here&lt;/a&gt; to enjoy the true &amp;lt;body&amp;gt;-less experience. I also embedded it below for your convenience:&lt;/p&gt;


&lt;div class="glitch-embed-wrap"&gt;
  &lt;iframe src="https://glitch.com/embed/#!/embed/head-blog?previewSize=100&amp;amp;path=index.html" alt="head-blog on glitch"&gt;&lt;/iframe&gt;
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>html</category>
      <category>css</category>
    </item>
    <item>
      <title>Debugging Challenge: The cursed default clause</title>
      <dc:creator>Sebastian Stamm</dc:creator>
      <pubDate>Mon, 02 Sep 2019 16:41:29 +0000</pubDate>
      <link>https://dev.to/sebastianstamm/debugging-challenge-the-cursed-default-clause-1cnk</link>
      <guid>https://dev.to/sebastianstamm/debugging-challenge-the-cursed-default-clause-1cnk</guid>
      <description>&lt;p&gt;An evil witch cursed my switch statement so that no matter what I put in, it always executed the default clause instead of the case clauses.&lt;/p&gt;

&lt;p&gt;This is a problem that I encountered in real life and is still my favorite bug. Here is a minimal setup you can play around with: &lt;a href="https://codesandbox.io/s/wonderful-meadow-2ph63?expanddevtools=1&amp;amp;fontsize=14&amp;amp;module=%2Fsrc%2Findex.js"&gt;Check it out on codesandbox.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you find out what is going on? I had an enlightening aha-moment when I figured it out, so I encourage you to have a look! The rest of this post describes the problem and why it behaves this way. If you want to find it out yourself, stop reading here.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is going on?
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Document not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Got an unknown error code&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unknown Error Incident&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should log "Document not found", but logs “Unknown Error Incident”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No matter what you call the function with, it always executed the default clause. Which is weird because there are cases that cover the inputs for the other values, so in those cases it should not go to the default clause.&lt;/p&gt;

&lt;p&gt;You will notice however, that the code itself is fine. If you copy it into your browsers console and execute it, you will see that it goes into the case clause if necessary and only uses the default if none of the case clauses match.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem is not the code that is written, but the code that is executed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most projects use something called a transpiler. A transpiler takes Javascript code and compiles it to other Javascript code. This is so you can write fancy modern Javascript with all the cool new features and still have it run in Internet Explorer.&lt;/p&gt;

&lt;p&gt;Which is a really awesome concept, but it also adds another layer of abstraction that can cause subtle bugs in your code. This is exactly what happened here. This is the code that is actually executed after transpilation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Document not found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;_error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Got an unknown error code&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Unknown Error Incident&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// should log "Document not found", but logs “Unknown Error Incident”&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at this code it’s instantly clear that this can’t work as the switch uses a variable that is not initialized yet. This is an actual bug in the transpiler: &lt;a href="https://github.com/babel/babel/issues/5378"&gt;https://github.com/babel/babel/issues/5378&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we rename the error variable in the default clause, everything works as it should even in the transpiled code.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to debug this
&lt;/h1&gt;

&lt;p&gt;Fortunately bugs like this are quite rare. And now that you are aware that transpilers can cause such problems you are already one step closer to identify and fix them.&lt;/p&gt;

&lt;p&gt;One simple step you can take to &lt;strong&gt;run the untranspiled code in the browser console&lt;/strong&gt;. Most of the time you need to simplify your code to isolate it from your business application, but constructing a minimal test case is a good practice anyway.&lt;/p&gt;

&lt;p&gt;The other way is to &lt;strong&gt;inspect the transpiled source code&lt;/strong&gt;. In the chrome console you can click on the “sourcemapped from” link to see what is actually executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FWC-mG3x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uy505jr7p8bt47y2qkrd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FWC-mG3x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/uy505jr7p8bt47y2qkrd.png" alt="How to get to the transpiled code from the Chrome Console" width="880" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Did you ever encounter a bug that made you scratch your head before discovering a surprising solution? I’d love to read about them! Also, since this is my first blog post, if you have any thoughts or suggestions on how I could improve, please share them with me, too!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>challenge</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
