Topics: #development
Client-side rendering has become the dominant paradigm for building web apps. But frameworks like React can lead to major JavaScript execution bottlenecks during page load. For many web developers there’s a clear solution for this: Ship less JavaScript to the browser.
Whether websites have to work without any JavaScript at all is a question almost as old as the web itself. By now, the answer is clear: No, they don’t. It’s firmly established that websites should be more than just structured and styled text. JavaScript execution is an integral part of (almost) every browser.
Still, there are benefits to shipping less JavaScript to the Browser. So I wanted to do a little case study and see how far I could take this with my online board gaming website. The answer: I could take it all the way.
My Goal: Fully optional JavaScript
The vast majority of users will experience my (or any other) website with full JavaScript functionality, including fast client side rendering and websocket connections. It is not an option to degrade the experience of these users for a little experiment. So my goal was to make JavaScript execution optional without giving up any of UX benefits for users who have JavaScript enabled.
These days one would call this “Progressive Enhancement,” meaning that just the HTML works on its own and CSS and JavaScript are not mandatory, but provide enhancements like fancy styling and a higher degree of interactivity. And while my gaming site is not usable without CSS, the principle stands.
The site is fairly complex: It has online multiplayer games with real-time updates and a login functionality to save running games, so it’s a good proving ground for this idea.
Look Mom, No JavaScript!
So, how did I do it? Let me walk you through the key components.
Server-Side Rendering
The basis for all of this is that the whole website and game can be rendered on demand on the server. But I can’t rely on Server-Side Rendering alone, or else my page would be transported back to the age of Java Server Pages or PHP Templates. It works well when JavaScript is disabled, but for all other users, I don’t want to give up the quick and efficient interaction after the initial page load that client-side rendering provides.
This is exactly what modern meta-frameworks like Next.js or SvelteKit promise: The combination of initially loading a page that was rendered on the server and (if JavaScript is available) switching to client-side rendering after that. In my case, I am using SvelteKit. For the site to be completely usable without JavaScript, there were some more steps necessary, that I’ll describe in the following paragraphs.
Interactive UI elements: Dropdown Menus
When searching for freely styled dropdown menus that can be implemented without the use of JavaScript, you can still find plenty of hacks like using a hidden checkbox and the :checked -pseudo selector to style the open and closed states. However, those are no longer required. The HTML standard provides us with the and
elements that can be used for exactly this purpose. bejofo.com uses these for the language menu in the top right corner, for example.
Interactive UI elements: Modals
One of the biggest limitations when working without JavaScript is that the ephemeral UI state is limited to a small set of elements provided by the browser, like:
Is the checkbox checked?
Is the