<?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: Paweł Mrowiec</title>
    <description>The latest articles on DEV Community by Paweł Mrowiec (@mrowa96).</description>
    <link>https://dev.to/mrowa96</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%2F414796%2Fadb90139-2227-4432-9b5b-b4db277c6650.jpg</url>
      <title>DEV Community: Paweł Mrowiec</title>
      <link>https://dev.to/mrowa96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrowa96"/>
    <language>en</language>
    <item>
      <title>Cookies - an immortal piece of Web</title>
      <dc:creator>Paweł Mrowiec</dc:creator>
      <pubDate>Thu, 21 Jan 2021 22:04:49 +0000</pubDate>
      <link>https://dev.to/mrowa96/cookies-an-immortal-piece-of-web-34l2</link>
      <guid>https://dev.to/mrowa96/cookies-an-immortal-piece-of-web-34l2</guid>
      <description>&lt;p&gt;After &lt;em&gt;localStorage&lt;/em&gt; was implemented in major browsers, I often saw that it was used as a replacement of plain old cookies. Even though developers had to handle e.g. expiration implementation on they own.&lt;/p&gt;

&lt;p&gt;Don't get me wrong, I'm very fond of &lt;em&gt;localStorage&lt;/em&gt; and I'm using it on daily basis, but cookies are also very useful and even more suited to some use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a cookie?
&lt;/h2&gt;

&lt;p&gt;But let's start from the beginning and explain what cookie is. Cookie is &lt;strong&gt;stored information&lt;/strong&gt;. It consists of &lt;em&gt;Name&lt;/em&gt;, &lt;em&gt;Value&lt;/em&gt; and additional attributes like &lt;em&gt;Expires&lt;/em&gt;, &lt;em&gt;Domain&lt;/em&gt;, &lt;em&gt;Path&lt;/em&gt; and more.&lt;/p&gt;

&lt;p&gt;It is attached to the http request (client -&amp;gt; server) and response (server -&amp;gt; client) in form of header(s). Requests use &lt;strong&gt;Cookie&lt;/strong&gt; header where all cookies are transferred. Responses on the other hand use multiple &lt;strong&gt;Set-cookie&lt;/strong&gt; headers (one per cookie). Let me visualise it to you in a form of plain objects:&lt;/p&gt;

&lt;h3&gt;
  
  
  Request
&lt;/h3&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;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://test.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keep-alive&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;Accept&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rodoStatus=accepted; uid=xxx;&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;h3&gt;
  
  
  Response
&lt;/h3&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;test@test.com&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;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;Connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keep-alive&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Set-cookie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rodoStatus=accepted; Max-Age=31536000; Domain=test.com; Path=/;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Set-cookie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;uid=xxx; HttpOnly;&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;As you can see, request cookies don't have any attributes declared, because they are only important for a sender (client), not for a receiver (server). &lt;/p&gt;

&lt;h2&gt;
  
  
  Cookies might be "customized"
&lt;/h2&gt;

&lt;p&gt;Before I describe use cases I have mentioned at the beginning, I want to do quick introduction to cookies attributes, because without knowledge about them it might be hard to understand cookies potential.&lt;/p&gt;

&lt;p&gt;Each and every cookie might have additional attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Domain&lt;/strong&gt; - it tells browser in which domain cookie has to be visible. Cookie with domain set to test.com, will be accessible only in pages under test.com domain. Sometimes is handy to make cookie available in subdomains. To do that, domain attribute has to be prefixed with a dot like .test.com.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path&lt;/strong&gt; - it points browser under what path cookie will be available. Cookie with path set to /page, will be accessible under /page and /page subpaths like /page/2 etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expires&lt;/strong&gt; - it declares date and time until when cookie will exist. After that, it will be automatically deleted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Max-Age&lt;/strong&gt; - it behaves similar to Expires attribute, but instead of date and time it uses number of seconds for how long cookie will exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secure&lt;/strong&gt; - it prevents cookie from being send to the server on other than &lt;em&gt;https&lt;/em&gt; protocol when declared.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HttpOnly&lt;/strong&gt; - it limits visibility of cookie. If declared, it's not possible to access cookie value through &lt;code&gt;document.cookie&lt;/code&gt;, but it's still send with requests made with e.g. &lt;code&gt;fetch()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SameSite&lt;/strong&gt; - it accepts three values - Strict, Lax (default in most browsers) and None. It's very complex topic which won't be covered in this article. If you want to know more here is a &lt;a href="https://web.dev/samesite-cookies-explained/"&gt;great article&lt;/a&gt; which should explain everything.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When you should use cookie?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Automatic expiration
&lt;/h3&gt;

&lt;p&gt;When you want to store some information for "forever", &lt;em&gt;localStorage&lt;/em&gt; is a match. But if you want to store data only for, let's say - 7 days, it might be problematic, because you will have to add some kind of &lt;em&gt;expires&lt;/em&gt; property and check it with &lt;code&gt;setInterval&lt;/code&gt; and after each page load. &lt;/p&gt;

&lt;p&gt;Cookies for a rescue! Just set &lt;em&gt;Expires&lt;/em&gt; attribute and you don't need to worry about it any more.&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;uid=xxx; Max-Age=604800&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 60 * 60 * 24 * 7 - 7 days&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Share data implicitly
&lt;/h3&gt;

&lt;p&gt;If you know, that specific piece of data should be transferred between a client and a server, it might be good idea to use a cookie, because every cookie will be automatically attached to a request and all cookies returned in a response will be saved without any manual work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hide data from a client
&lt;/h3&gt;

&lt;p&gt;Sometimes there is a need to store some information, but it shouldn't be available via JavaScript e.g. access token. Cookies are great for that. Just use HttpOnly attribute and that's it.&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;at=xxx; HttpOnly;&lt;/span&gt;&lt;span class="dl"&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookie&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns ""&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Access data from a server
&lt;/h3&gt;

&lt;p&gt;Let me show you real world example. When you want to implement dark theme on your website, you have to store chose theme somehow. And based on this stored value you will add &lt;em&gt;theme-dark&lt;/em&gt; or &lt;em&gt;theme-light&lt;/em&gt; class to &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You might store value with &lt;code&gt;localStorage.setItem("theme", "dark")&lt;/code&gt; after user toggle theme switcher. And after page load get value to &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; with&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`theme-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;theme&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But one problem could come up, especially for users with slower devices. For a moment, between DOM render and script execution there will be small time window, where &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; won't have any class (or it will have default one). And because of that, user might see flickering effect.&lt;/p&gt;

&lt;p&gt;Fortunately, cookies are also suited for this job, because you can access them on a server and set the right theme before page will be rendered by the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Potential problems with cookies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Browsers security systems
&lt;/h3&gt;

&lt;p&gt;Currently, all browsers are making cookies implementations more strict. Some examples below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In 2020 default value of &lt;em&gt;SameSite&lt;/em&gt; attribute was set to &lt;em&gt;Lax&lt;/em&gt;. Previously it was set to &lt;em&gt;None&lt;/em&gt;. It will be perfectly fine if browsers unified implementations, which they didn't and in each browser &lt;em&gt;SameSite&lt;/em&gt; attribute behaves differently. If you are interested in this topic, this &lt;a href="https://www.thinktecture.com/en/identity/samesite/samesite-in-a-nutshell/#the-chrome-vs-safari-issue"&gt;article&lt;/a&gt; is very thorough.&lt;/li&gt;
&lt;li&gt;In private windows, browsers usually block third-party cookies, so your web app might behave differently there.&lt;/li&gt;
&lt;li&gt;Safari and their ITP (Intelligent Tracking Prevention) might remove cookie after just 1 day, even when their &lt;em&gt;Expires/Max-Age&lt;/em&gt; attribute points to further date. More about that you can read in this &lt;a href="https://www.convert.com/blog/privacy/itp-2-2-impact-abtesting-users/"&gt;article&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Firefox with ETP (&lt;a href="https://blog.mozilla.org/blog/2019/06/04/firefox-now-available-with-enhanced-tracking-protection-by-default/"&gt;Enhanced Tracking Protection&lt;/a&gt;) might also block various cookies, but mostly third-party tracking ones as default.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Max cookie size
&lt;/h3&gt;

&lt;p&gt;Cookies are restricted in their size and quantity. Most browsers allow for 50 cookies per domain, 4096 bytes per cookie, 3000 cookies in total based on &lt;a href="https://datatracker.ietf.org/doc/rfc6265/?include_text=1"&gt;RFC 6265&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Outdated JavaScript API
&lt;/h3&gt;

&lt;p&gt;As you might see API for cookie manipulation in the browser is a bit... inaccessible.&lt;/p&gt;

&lt;p&gt;Setting a new cookie is fairly easy - &lt;code&gt;document.cookie = "uuid=xxx; SameSite=Strict; Secure;"&lt;/code&gt;, but nightmare comes when we need to get cookie value by name. It is not possible at the moment out of the box, because &lt;code&gt;document.cookie&lt;/code&gt; will return all available cookies in one string. So, developers have to include some libraries like &lt;a href="https://www.npmjs.com/package/js-cookie"&gt;js-cookie&lt;/a&gt; or write their own helper for that in &lt;strong&gt;every&lt;/strong&gt; project.&lt;/p&gt;

&lt;p&gt;But there is a light in a tunnel - &lt;a href="https://wicg.github.io/cookie-store/"&gt;Cookie Store API&lt;/a&gt; created by Google and implemented since Chrome 87. Sadly other browsers vendors aren't open for implementation yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies"&gt;Cookies basics on MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie"&gt;Cookie header on MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie"&gt;Set-Cookie header on MDN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PS This is my first published article, so I'm waiting for constructive criticism 🙄😬&lt;/p&gt;

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