Show HN: µJS, a 5KB alternative to Htmx and Turbo with zero dependencies
µJS is a new 5KB JavaScript library designed for lightweight AJAX navigation, offering a minimalist alternative to HTMX and Turbo by intelligently swapping DOM fragments without full page reloads. It champions a no-build-step, zero-dependency approach, appealing to developers seeking efficiency and simplicity in web development. The Hacker News community is abuzz, comparing its pragmatic feature set against other hypermedia-driven libraries and discussing its place in modern web architecture.
The Lowdown
µJS positions itself as a streamlined solution for modern web navigation, addressing the performance overhead of traditional full-page reloads. It intercepts link clicks and form submissions, fetching new content via AJAX and seamlessly replacing only the changed parts of the DOM. This results in an instant, single-page application-like experience without the complexity, integrating effortlessly with any backend technology.
Key features include:
- Fast: Offers instant navigation through prefetching, partial updates, and a built-in progress bar.
- Lightweight: At approximately 5KB gzipped with zero dependencies, it's significantly smaller than alternatives like HTMX or Turbo.
- Drop-in: Requires just a single script tag and
mu.init()call, working with any server-side language without backend modifications. - Patch mode: Enables updating multiple distinct DOM fragments from a single AJAX request, ideal for dynamic dashboards.
- Triggers: Allows any element to trigger actions on any event, supporting features like live search and polling with built-in debounce.
- HTTP verbs: Supports GET, POST, PUT, PATCH, and DELETE on links, buttons, and forms.
- SSE: Integrates Server-Sent Events for real-time updates, combinable with patch mode for multi-fragment streaming.
- Modern: Leverages the View Transitions API, DOM morphing via idiomorph, and the Fetch API for a contemporary user experience.
µJS aims to provide the benefits of dynamic, app-like navigation without requiring a client-side framework, a build step, or complex server-side changes, making it an attractive option for projects prioritizing simplicity and performance.
The Gossip
Comparative Catalogs
The discussion heavily features comparisons between µJS and other hypermedia-driven libraries like HTMX, Turbo, htmz, and Datastar. Commenters inquire about µJS's unique selling points and its position in this ecosystem. The author steps in to clarify philosophical differences, noting µJS's pure focus on lightweight AJAX navigation and content replacement versus Datastar's more reactive, state-managing approach.
Data Delivery Dilemmas
A common point of contention revolves around handling JSON responses, a frequent critique of HTML-over-the-wire libraries like HTMX. Users express a desire for built-in JSON parsing, especially when integrating with existing APIs that output JSON. The consensus seems to be that while these libraries excel in HTML-first architectures, they are less suited for applications built around JSON-spitting backends unless the backend can be adapted.
Semantic Standards and Custom Attributes
One thoughtful comment suggests that instead of introducing custom `mu-` attributes, the library could explore leveraging existing HTML semantics and microdata standards, such as RDFa's `resource` attribute. This highlights a desire for closer adherence to web standards and potentially more interoperability in declarative UI design.