Swappit is a lightweight, zero-dependency JavaScript library that updates specific regions of your HTML page without a full reload. Instead of wiring upDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/soyleninjs/swappit/llms.txt
Use this file to discover all available pages before exploring further.
fetch calls and manual DOM manipulation — or reaching for an entire SPA framework — you mark the elements you want to update with data-* attributes and let Swappit handle the rest. The result is modern, SPA-like navigation with minimal setup and no build step required.
How It Works
Every Swappit setup revolves around a handle — a unique string you choose when creating an instance:data-* attribute you add to any HTML element you want to be swappable:
data-[handle]-update is a region name — a logical identifier, not a CSS selector. When you call app.update('./about.html'), Swappit:
- Fetches the HTML document at that URL.
- Parses it in memory (never rendering it to the page).
- Finds every element whose
data-app-updateattribute matches a region name that exists in the current DOM. - Replaces the live element with the new one — in place, with no page reload.
hidden class. If a region exists in the fetched document but not in the current page, it is ignored.
When to Use Swappit
✅ Good fit
- Multi-page sites that want SPA-like navigation without a full rewrite
- Server-rendered HTML stacks (Rails, Laravel, Django, plain HTML files) where the server already produces the markup
- Progressive enhancement — Swappit is purely additive; the site still works without JavaScript
- Projects that want to avoid a build pipeline or heavy framework dependency
- Sites where only specific page regions change between routes (header, sidebar, main content)
❌ Not a fit
- Applications that require client-side state management (shopping carts, form wizards, live dashboards)
- Pages driven by real-time data (WebSockets, Server-Sent Events, polling)
- Projects that already use a component-based framework (React, Vue, Svelte) — those handle DOM diffing natively
- Apps with complex client-side routing logic that goes beyond swapping HTML regions
Key Features
- Partial DOM updates — Replace only the elements you need using unique region names, leaving the rest of the page untouched.
- Three usage patterns — Programmatic JavaScript API, automatic link handling via
data-swappit-handle, or a fully declarative<swappit-instance>custom element. - Smart preloading — Configure instant preload (on page load) or hover preload (on
mouseenter/touchstart), globally per instance or individually per link. - History navigation — Optional
pushState/replaceStateintegration so browser back and forward buttons work as expected. - Cache system — Fetched documents are stored in memory; pass
falseas the second argument toupdate()to force a fresh download when needed. - DOM observer — A
MutationObserverwatches for dynamically added links and wires them up automatically. - Event system — Custom events fire on
windowbefore and after every update, on error, on history navigation, on reinit, and on destroy. - Colorized logging — Four-level debug logging (info, success, warning, error) activated with a single
log: trueoption. - Zero dependencies — No npm dependencies, no build step, no framework required.
Package Info
| Field | Value |
|---|---|
| npm | @soyleninjs/swappit |
| Version | 3.0.0 |
| License | MIT |