| 1 | --- |
| 2 | title: "Next.js to htmx — A Real World Example" |
| 3 | description: "In this case study, Pouria Ezzati examines how migrating a URL shortener from Next.js to htmx resulted in significant improvements in codebase size, dependencies, and developer experience while challenging assumptions about modern web frameworks." |
| 4 | created: 2024-11-07 |
| 5 | modified: 2024-11-07 |
| 6 | authors: [ "Pouria Ezzati" ] |
| 7 | tags: ["case-studies"] |
| 8 | --- |
| 9 | |
| 10 | Over 6 years ago, I created [an open source URL shortener](https://github.com/thedevs-network/kutt) with Next.js and |
| 11 | after years of working on it, I found Next.js to be much more of a burden than a help. Over the years, Next.js has |
| 12 | changed, and so did my code so it can be compatible with those changes. |
| 13 | |
| 14 | My Next.js codebase grew bigger, and its complexity increased by greater size. I had dozens of components and a list of |
| 15 | dependencies to manage. I ended up maintaining the code constantly just to keep it alive. Sure, Next.js helped here and |
| 16 | there, but at what cost? |
| 17 | |
| 18 | I asked myself, what am I doing on my website that is so complex that needs all that JavaScript code to decide what to |
| 19 | render and how to render on my webpage? Next.js was trying to render the webpage from the server side, so why won't I |
| 20 | send the HTML directly myself? |
| 21 | |
| 22 | So I decided to try a new route—some might say the good ol' route—and choose plain HTML and use the help of htmx for |
| 23 | that. |
| 24 | |
| 25 | ## Video |
| 26 | |
| 27 | Watch me go full in details here: |
| 28 | |
| 29 | <iframe style="max-width: 100%" width="618" height="352" src="https://www.youtube.com/embed/8RL4NvYZDT4" title=" Next.js to htmx – A Real World Example " frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> |
| 30 | |
| 31 | ## The process |
| 32 | |
| 33 | Replacing my components with the equivalent HTML elements powered by htmx wasn't exactly an easy task, but one that was |
| 34 | worth the time. I had to view things from a different angle, and I sometimes felt strict in what user interactions I can |
| 35 | implement, but what I created was reliable and fast. |
| 36 | |
| 37 | All the build steps were gone; no more transpiling and compiling the code. What you see is what you get. Most of the |
| 38 | dependencies became redundant and have been removed. All the main logic of the website was moved to the server side, |
| 39 | holding one source for the truth. |
| 40 | |
| 41 | In the Next.js version I had isolated components, global states, and all that JavaScript to handle the forms or update |
| 42 | the content, and yet, everything was more intuitive with htmx. After trying it, sending and receiving HTML suddenly made |
| 43 | sense. |
| 44 | |
| 45 | ## Summary |
| 46 | |
| 47 | - Dependencies are **reduced by 87%** (**24** to **3**!) |
| 48 | - I wrote **less code by 17%** (**9500 LOC** to **7900 LOC**.) In reality the total LOC of the code base is **reduced |
| 49 | by more than 50%**, since much less code is imported from the dependencies. |
| 50 | - Web build time was **reduced by 100%** (there's **no build step** anymore.) |
| 51 | - Size of the website **reduced by more than 85%** (**~800KB** to **~100KB**!) |
| 52 | |
| 53 | These numbers signify a great improvement, however, what is important for me at the end is the user and the developer |
| 54 | experience, which to me htmx won at both. |