<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Javascript — Bojan Vidanovic</title><link>https://bojanvidanovic.com/tags/javascript/</link><description>Notes on web development, technology, books, and life in Italy. The independent personal website of Bojan Vidanovic.</description><language>en</language><atom:link href="https://bojanvidanovic.com/tags/javascript/index.xml" rel="self" type="application/rss+xml"/><item><title>Love these interactive articles, and this one is by far the best on explaining …</title><link>https://bojanvidanovic.com/status/1757411287400/</link><guid isPermaLink="true">https://bojanvidanovic.com/status/1757411287400/</guid><pubDate>Tue, 09 Sep 2025 11:44:52 +0200</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>status</category><description>&lt;p&gt;Love these interactive articles, and this one is by far the best on explaining how to recreate Apple&amp;rsquo;s new Liquid Glass UI in the browser. &lt;a href="https://kube.io/blog/liquid-glass-css-svg/"&gt;Liquid Glass in the Browser: Refraction with CSS and SVG&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/status/1757411287400/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>&lt;p&gt;Love these interactive articles, and this one is by far the best on explaining how to recreate Apple&amp;rsquo;s new Liquid Glass UI in the browser. &lt;a href="https://kube.io/blog/liquid-glass-css-svg/"&gt;Liquid Glass in the Browser: Refraction with CSS and SVG&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/status/1757411287400/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Handling Service Worker updates</title><link>https://bojanvidanovic.com/bookmarks/handling-service-worker-updates/</link><guid isPermaLink="true">https://bojanvidanovic.com/bookmarks/handling-service-worker-updates/</guid><pubDate>Tue, 04 Mar 2025 17:28:09 +0100</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>bookmarks</category><description>&lt;p&gt;Updating PWAs is trickier than I thought, this article explains very well why and how to implement a proper updating.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://whatwebcando.today/articles/handling-service-worker-updates/"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/handling-service-worker-updates/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>&lt;p&gt;Updating PWAs is trickier than I thought, this article explains very well why and how to implement a proper updating.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://whatwebcando.today/articles/handling-service-worker-updates/"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/handling-service-worker-updates/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Don't Sleep on AbortController</title><link>https://bojanvidanovic.com/bookmarks/dont-sleep-on-abortcontroller/</link><guid isPermaLink="true">https://bojanvidanovic.com/bookmarks/dont-sleep-on-abortcontroller/</guid><pubDate>Fri, 28 Feb 2025 16:43:40 +0100</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>bookmarks</category><description>&lt;p&gt;Well written article on AbortController superpowers.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you are creating libraries in JavaScript where aborting or cancelling operations makes sense, I highly encourage you to look no further than the AbortController API. It&amp;rsquo;s incredible! And if you are building applications, you can utilize the abort controller to a great effect when you need to cancel requests, remove event listeners, abort streams, or teach any logic to be abortable.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="https://kettanaito.com/blog/dont-sleep-on-abort-controller"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/dont-sleep-on-abortcontroller/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>&lt;p&gt;Well written article on AbortController superpowers.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you are creating libraries in JavaScript where aborting or cancelling operations makes sense, I highly encourage you to look no further than the AbortController API. It&amp;rsquo;s incredible! And if you are building applications, you can utilize the abort controller to a great effect when you need to cancel requests, remove event listeners, abort streams, or teach any logic to be abortable.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="https://kettanaito.com/blog/dont-sleep-on-abort-controller"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/dont-sleep-on-abortcontroller/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Most developers when using HTML inputs with type="number" assume the input value …</title><link>https://bojanvidanovic.com/status/1682842843659/</link><guid isPermaLink="true">https://bojanvidanovic.com/status/1682842843659/</guid><pubDate>Sun, 30 Apr 2023 10:02:25 +0200</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>status</category><description>&lt;p&gt;Most developers when using HTML inputs with &lt;code&gt;type=&amp;quot;number&amp;quot;&lt;/code&gt; assume the input value will be a number, and get confused when the value is a string instead. This is because the type=&amp;ldquo;number&amp;rdquo; actually tells the browser that the input is used for numbers and to provide a number input UI with included built-in validation to reject non-numerical entries.&lt;/p&gt;
&lt;p&gt;Now to get the number almost everyone will use the good old parser functions &lt;code&gt;parseInt&lt;/code&gt;/&lt;code&gt;parseFloat&lt;/code&gt;, like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;parseInt(&lt;span style="color:#a6e22e"&gt;input&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;10&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But, there is a less-known property called .valueAsNumber which allows us to avoid parseInt/parseFloat functions, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;input&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;valueAsNumber&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;How cool is that!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/status/1682842843659/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>&lt;p&gt;Most developers when using HTML inputs with &lt;code&gt;type=&amp;quot;number&amp;quot;&lt;/code&gt; assume the input value will be a number, and get confused when the value is a string instead. This is because the type=&amp;ldquo;number&amp;rdquo; actually tells the browser that the input is used for numbers and to provide a number input UI with included built-in validation to reject non-numerical entries.&lt;/p&gt;
&lt;p&gt;Now to get the number almost everyone will use the good old parser functions &lt;code&gt;parseInt&lt;/code&gt;/&lt;code&gt;parseFloat&lt;/code&gt;, like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;parseInt(&lt;span style="color:#a6e22e"&gt;input&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;value&lt;/span&gt;, &lt;span style="color:#ae81ff"&gt;10&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But, there is a less-known property called .valueAsNumber which allows us to avoid parseInt/parseFloat functions, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;input&lt;/span&gt;.&lt;span style="color:#a6e22e"&gt;valueAsNumber&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;How cool is that!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/status/1682842843659/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Stop Using Conditional Statements Everywhere in JavaScript, Use an Object Literal instead</title><link>https://bojanvidanovic.com/bookmarks/stop-using-conditional-statements-everywhere-in-javascript-use-an-object-literal-instead/</link><guid isPermaLink="true">https://bojanvidanovic.com/bookmarks/stop-using-conditional-statements-everywhere-in-javascript-use-an-object-literal-instead/</guid><pubDate>Fri, 11 Mar 2022 09:55:35 +0000</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>bookmarks</category><description>
&lt;p&gt;&lt;a href="https://neelesh-arora.medium.com/stop-using-conditional-statements-everywhere-in-javascript-use-an-object-literal-instead-e780debcda18"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/stop-using-conditional-statements-everywhere-in-javascript-use-an-object-literal-instead/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>
&lt;p&gt;&lt;a href="https://neelesh-arora.medium.com/stop-using-conditional-statements-everywhere-in-javascript-use-an-object-literal-instead-e780debcda18"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/stop-using-conditional-statements-everywhere-in-javascript-use-an-object-literal-instead/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Good advice on JSX conditionals</title><link>https://bojanvidanovic.com/bookmarks/good-advice-on-jsx-conditionals/</link><guid isPermaLink="true">https://bojanvidanovic.com/bookmarks/good-advice-on-jsx-conditionals/</guid><pubDate>Wed, 09 Mar 2022 21:28:31 +0000</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>bookmarks</category><description>
&lt;p&gt;&lt;a href="https://thoughtspile.github.io/2022/01/17/jsx-conditionals/"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/good-advice-on-jsx-conditionals/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>
&lt;p&gt;&lt;a href="https://thoughtspile.github.io/2022/01/17/jsx-conditionals/"&gt;Open bookmarked page&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://bojanvidanovic.com/bookmarks/good-advice-on-jsx-conditionals/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Speeding up static websites with PJAX</title><link>https://bojanvidanovic.com/blog/speeding-up-static-websites-with-pjax/</link><guid isPermaLink="true">https://bojanvidanovic.com/blog/speeding-up-static-websites-with-pjax/</guid><pubDate>Fri, 24 Jul 2020 16:27:25 +0200</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>blog</category><description>&lt;p&gt;With the fast-growing trend of static websites, many people are moving their blogs from &lt;a href="https://wordpress.org/"&gt;WordPress&lt;/a&gt;, &lt;a href="https://medium.com"&gt;Medium&lt;/a&gt;, etc, to static websites built with &lt;a href="https://gohugo.io"&gt;Hugo&lt;/a&gt; or &lt;a href="https://jekyllrb.com"&gt;Jekyll&lt;/a&gt;. And there is a good reason for that. One of the reasons is security, cutting down server costs, ease of maintenance, performances, and many others. But in this article, I would like to focus on performances.&lt;/p&gt;
&lt;p&gt;As everybody knows, static websites offer out-of-the-box very fast performances without any configuration. You have no database, no dynamically generated content, just serving HTML files from your hosting. As simple as that. But using the PJAX technique we can improve loading performances and enhance the user&amp;rsquo;s experience a lot!&lt;/p&gt;
&lt;ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-9401752794288868"
data-ad-slot="2055838653"&gt;&lt;/ins&gt;
&lt;h2 id="what-is-pjax-and-how-it-works"&gt;What is PJAX and how it works?&lt;/h2&gt;
&lt;p&gt;PJAX technique is using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History/pushState"&gt;PushState&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Ajax_%28programming%29"&gt;AJAX&lt;/a&gt;. Basically, on a page request, we will manually update the URL and inject the new content on the page, thus avoiding reloading your page&amp;rsquo;s layout or any resources (JS, CSS), giving a fast page load.&lt;/p&gt;
&lt;p&gt;Here are the steps of the process with &lt;a href="https://barba.js.org/"&gt;Barba.js&lt;/a&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check if the link is valid and eligible for PJAX. If yes, prevent normal browser behavior.&lt;/li&gt;
&lt;li&gt;Change the URL using the push state API.&lt;/li&gt;
&lt;li&gt;Start fetching the new page via an XMLHttpRequest.&lt;/li&gt;
&lt;li&gt;Create a new transition instance.&lt;/li&gt;
&lt;li&gt;As soon the new page is loaded, barba.js parses the new HTML (taking .barba-container) and puts the new content on the DOM inside #barba-wrapper.&lt;/li&gt;
&lt;li&gt;The transition instance will take care of hiding the old container and showing the new one.&lt;/li&gt;
&lt;li&gt;As soon as the transition is finished, the old container is removed from the DOM.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="how-to-implement-pjax-on-a-website"&gt;How to implement PJAX on a website?&lt;/h2&gt;
&lt;p&gt;There are many PJAX libraries, like &lt;a href="https://barba.js.org/"&gt;Barba.js&lt;/a&gt;, &lt;a href="https://sennajs.com/"&gt;Senna.js&lt;/a&gt;, &lt;a href="https://github.com/turbolinks/turbolinks"&gt;Turbolinks&lt;/a&gt;, &lt;a href="https://github.com/MoOx/pjax"&gt;Pjax&lt;/a&gt;, etc. Usually, they all offer extremely easy implementation, I prefer using Barba.js for simple websites and Senna.js for more complex ones as it has a better API. In this article, I will use Barba.js&lt;/p&gt;
&lt;p&gt;The implementation consists of 2 parts:
Setting up DOM markup
Script initialization.&lt;/p&gt;
&lt;h3 id="setting-up-dom-markup"&gt;Setting up DOM markup&lt;/h3&gt;
&lt;p&gt;Barba.js needs to know how your website is set up, so it will need a few HTML attributes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;body&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;data-barba&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;wrapper&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- put here content that will not change
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; between your pages, like &amp;lt;header&amp;gt; or &amp;lt;nav&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;main&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;data-barba&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;container&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- put here the content you wish to change
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; between your pages, like your main content &amp;lt;h1&amp;gt; or &amp;lt;p&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;main&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- put here content that will not change
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; between your pages, like &amp;lt;footer&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;/&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The wrapper is the main Barba section that contains all your page structure and the Barba container.
The container defines a section in which content is updated automatically when you navigate between your pages.&lt;/p&gt;
&lt;h3 id="script-initialization"&gt;Script initialization&lt;/h3&gt;
&lt;p&gt;After setting up the DOM attributes we simply include Barba.js script in the head of the document and initialize the script:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;script&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;type&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;text/javascript&amp;#34;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;src&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;https://cdn.jsdelivr.net/npm/@barba/core&amp;#34;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;async&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;onload&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;barba.init({})&amp;#34;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That&amp;rsquo;s it.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This approach will give a SPA-like feeling of the website since there are much fewer server requests.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/blog/speeding-up-static-websites-with-pjax/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>&lt;p&gt;With the fast-growing trend of static websites, many people are moving their blogs from &lt;a href="https://wordpress.org/"&gt;WordPress&lt;/a&gt;, &lt;a href="https://medium.com"&gt;Medium&lt;/a&gt;, etc, to static websites built with &lt;a href="https://gohugo.io"&gt;Hugo&lt;/a&gt; or &lt;a href="https://jekyllrb.com"&gt;Jekyll&lt;/a&gt;. And there is a good reason for that. One of the reasons is security, cutting down server costs, ease of maintenance, performances, and many others. But in this article, I would like to focus on performances.&lt;/p&gt;
&lt;p&gt;As everybody knows, static websites offer out-of-the-box very fast performances without any configuration. You have no database, no dynamically generated content, just serving HTML files from your hosting. As simple as that. But using the PJAX technique we can improve loading performances and enhance the user&amp;rsquo;s experience a lot!&lt;/p&gt;
&lt;ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-9401752794288868"
data-ad-slot="2055838653"&gt;&lt;/ins&gt;
&lt;h2 id="what-is-pjax-and-how-it-works"&gt;What is PJAX and how it works?&lt;/h2&gt;
&lt;p&gt;PJAX technique is using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History/pushState"&gt;PushState&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Ajax_%28programming%29"&gt;AJAX&lt;/a&gt;. Basically, on a page request, we will manually update the URL and inject the new content on the page, thus avoiding reloading your page&amp;rsquo;s layout or any resources (JS, CSS), giving a fast page load.&lt;/p&gt;
&lt;p&gt;Here are the steps of the process with &lt;a href="https://barba.js.org/"&gt;Barba.js&lt;/a&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check if the link is valid and eligible for PJAX. If yes, prevent normal browser behavior.&lt;/li&gt;
&lt;li&gt;Change the URL using the push state API.&lt;/li&gt;
&lt;li&gt;Start fetching the new page via an XMLHttpRequest.&lt;/li&gt;
&lt;li&gt;Create a new transition instance.&lt;/li&gt;
&lt;li&gt;As soon the new page is loaded, barba.js parses the new HTML (taking .barba-container) and puts the new content on the DOM inside #barba-wrapper.&lt;/li&gt;
&lt;li&gt;The transition instance will take care of hiding the old container and showing the new one.&lt;/li&gt;
&lt;li&gt;As soon as the transition is finished, the old container is removed from the DOM.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="how-to-implement-pjax-on-a-website"&gt;How to implement PJAX on a website?&lt;/h2&gt;
&lt;p&gt;There are many PJAX libraries, like &lt;a href="https://barba.js.org/"&gt;Barba.js&lt;/a&gt;, &lt;a href="https://sennajs.com/"&gt;Senna.js&lt;/a&gt;, &lt;a href="https://github.com/turbolinks/turbolinks"&gt;Turbolinks&lt;/a&gt;, &lt;a href="https://github.com/MoOx/pjax"&gt;Pjax&lt;/a&gt;, etc. Usually, they all offer extremely easy implementation, I prefer using Barba.js for simple websites and Senna.js for more complex ones as it has a better API. In this article, I will use Barba.js&lt;/p&gt;
&lt;p&gt;The implementation consists of 2 parts:
Setting up DOM markup
Script initialization.&lt;/p&gt;
&lt;h3 id="setting-up-dom-markup"&gt;Setting up DOM markup&lt;/h3&gt;
&lt;p&gt;Barba.js needs to know how your website is set up, so it will need a few HTML attributes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;body&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;data-barba&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;wrapper&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- put here content that will not change
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; between your pages, like &amp;lt;header&amp;gt; or &amp;lt;nav&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;&lt;span style="color:#f92672"&gt;main&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;data-barba&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;container&amp;#34;&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- put here the content you wish to change
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; between your pages, like your main content &amp;lt;h1&amp;gt; or &amp;lt;p&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &amp;lt;/&lt;span style="color:#f92672"&gt;main&lt;/span&gt;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;&amp;lt;!-- put here content that will not change
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt; between your pages, like &amp;lt;footer&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;/&lt;span style="color:#f92672"&gt;body&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The wrapper is the main Barba section that contains all your page structure and the Barba container.
The container defines a section in which content is updated automatically when you navigate between your pages.&lt;/p&gt;
&lt;h3 id="script-initialization"&gt;Script initialization&lt;/h3&gt;
&lt;p&gt;After setting up the DOM attributes we simply include Barba.js script in the head of the document and initialize the script:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&amp;lt;&lt;span style="color:#f92672"&gt;script&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;type&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;text/javascript&amp;#34;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;src&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;https://cdn.jsdelivr.net/npm/@barba/core&amp;#34;&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;async&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;onload&lt;/span&gt;&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;barba.init({})&amp;#34;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color:#f92672"&gt;script&lt;/span&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That&amp;rsquo;s it.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This approach will give a SPA-like feeling of the website since there are much fewer server requests.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/blog/speeding-up-static-websites-with-pjax/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>High Performance Javascript</title><link>https://bojanvidanovic.com/books/high-performance-javascript/</link><guid isPermaLink="true">https://bojanvidanovic.com/books/high-performance-javascript/</guid><pubDate>Thu, 23 Jul 2020 16:35:50 +0200</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>books</category><description>
&lt;div class="media-detail book-detail"&gt;
&lt;img src="https://covers.openlibrary.org/b/id/7262408-L.jpg" alt="High Performance JavaScript book cover" class="media-detail-cover" loading="lazy" decoding="async" width="150" height="225"&gt;
&lt;div class="media-detail-copy"&gt;
&lt;p class="eyebrow"&gt;On the bookshelf&lt;/p&gt;
&lt;p class="media-credit"&gt;Nicholas C. Zakas&lt;/p&gt;
&lt;dl class="media-facts"&gt;&lt;div&gt;&lt;dt&gt;Published&lt;/dt&gt;&lt;dd&gt;2010&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;
&lt;span class="rating-pill"&gt;
&lt;svg class="w-4 h-4" aria-hidden="true" focusable="false"&gt;
&lt;use href="https://bojanvidanovic.com/svg/sprite.symbol.a579f17d1fccf11659f4f442c869f7b3b47a82a77ce284063f042dd8d0a666cc.svg#star" /&gt;
&lt;/svg&gt;
&lt;strong&gt;9&lt;/strong&gt;&lt;span&gt;/ 10&lt;/span&gt;&lt;span class="sr-only"&gt;My rating&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/books/high-performance-javascript/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>
&lt;div class="media-detail book-detail"&gt;
&lt;img src="https://covers.openlibrary.org/b/id/7262408-L.jpg" alt="High Performance JavaScript book cover" class="media-detail-cover" loading="lazy" decoding="async" width="150" height="225"&gt;
&lt;div class="media-detail-copy"&gt;
&lt;p class="eyebrow"&gt;On the bookshelf&lt;/p&gt;
&lt;p class="media-credit"&gt;Nicholas C. Zakas&lt;/p&gt;
&lt;dl class="media-facts"&gt;&lt;div&gt;&lt;dt&gt;Published&lt;/dt&gt;&lt;dd&gt;2010&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;
&lt;span class="rating-pill"&gt;
&lt;svg class="w-4 h-4" aria-hidden="true" focusable="false"&gt;
&lt;use href="https://bojanvidanovic.com/svg/sprite.symbol.a579f17d1fccf11659f4f442c869f7b3b47a82a77ce284063f042dd8d0a666cc.svg#star" /&gt;
&lt;/svg&gt;
&lt;strong&gt;9&lt;/strong&gt;&lt;span&gt;/ 10&lt;/span&gt;&lt;span class="sr-only"&gt;My rating&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/books/high-performance-javascript/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item><item><title>Professional Javascript for Web Developers 3rd Edition</title><link>https://bojanvidanovic.com/books/professional-javascript-for-web-developers/</link><guid isPermaLink="true">https://bojanvidanovic.com/books/professional-javascript-for-web-developers/</guid><pubDate>Thu, 23 Jul 2020 16:22:39 +0200</pubDate><dc:creator>Bojan Vidanovic</dc:creator><category>books</category><description>
&lt;div class="media-detail book-detail"&gt;
&lt;img src="https://m.media-amazon.com/images/I/51TYgNA8Y-L.jpg" alt="Professional JavaScript for Web developers (3rd Edition) book cover" class="media-detail-cover" loading="lazy" decoding="async" width="150" height="225"&gt;
&lt;div class="media-detail-copy"&gt;
&lt;p class="eyebrow"&gt;On the bookshelf&lt;/p&gt;
&lt;h3&gt;Professional JavaScript for Web developers (3rd Edition)&lt;/h3&gt;
&lt;p class="media-credit"&gt;Nicholas C. Zakas&lt;/p&gt;
&lt;dl class="media-facts"&gt;&lt;div&gt;&lt;dt&gt;Published&lt;/dt&gt;&lt;dd&gt;2005&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;
&lt;span class="rating-pill"&gt;
&lt;svg class="w-4 h-4" aria-hidden="true" focusable="false"&gt;
&lt;use href="https://bojanvidanovic.com/svg/sprite.symbol.a579f17d1fccf11659f4f442c869f7b3b47a82a77ce284063f042dd8d0a666cc.svg#star" /&gt;
&lt;/svg&gt;
&lt;strong&gt;9&lt;/strong&gt;&lt;span&gt;/ 10&lt;/span&gt;&lt;span class="sr-only"&gt;My rating&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/books/professional-javascript-for-web-developers/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</description><content:encoded>
&lt;div class="media-detail book-detail"&gt;
&lt;img src="https://m.media-amazon.com/images/I/51TYgNA8Y-L.jpg" alt="Professional JavaScript for Web developers (3rd Edition) book cover" class="media-detail-cover" loading="lazy" decoding="async" width="150" height="225"&gt;
&lt;div class="media-detail-copy"&gt;
&lt;p class="eyebrow"&gt;On the bookshelf&lt;/p&gt;
&lt;h3&gt;Professional JavaScript for Web developers (3rd Edition)&lt;/h3&gt;
&lt;p class="media-credit"&gt;Nicholas C. Zakas&lt;/p&gt;
&lt;dl class="media-facts"&gt;&lt;div&gt;&lt;dt&gt;Published&lt;/dt&gt;&lt;dd&gt;2005&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;
&lt;span class="rating-pill"&gt;
&lt;svg class="w-4 h-4" aria-hidden="true" focusable="false"&gt;
&lt;use href="https://bojanvidanovic.com/svg/sprite.symbol.a579f17d1fccf11659f4f442c869f7b3b47a82a77ce284063f042dd8d0a666cc.svg#star" /&gt;
&lt;/svg&gt;
&lt;strong&gt;9&lt;/strong&gt;&lt;span&gt;/ 10&lt;/span&gt;&lt;span class="sr-only"&gt;My rating&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href="https://bojanvidanovic.com/books/professional-javascript-for-web-developers/"&gt;View the complete entry on Bojan Vidanovic&lt;/a&gt;&lt;/p&gt;</content:encoded></item></channel></rss>