The declarative approaches —Documentation 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.
<swappit-instance> and data-swappit-handle links — cover most scenarios out of the box. But when you need programmatic control, the JavaScript API lets you call updates in response to anything: a button click, a form submission, a scroll event, an API response, or a timer. You create a Swappit instance directly in JavaScript and call its methods whenever your application logic demands it.
Creating an instance
Instantiate Swappit with a handle — a unique string identifier that becomes the prefix for alldata-* attributes Swappit manages on the page:
data-my-app-update="header" when it updates the DOM. You can pass a second argument to configure options:
| Option | Type | Default | Description |
|---|---|---|---|
log | boolean | false | Enables color-coded console logging |
updateUrl | boolean | false | Updates the browser URL bar on each update() call |
enableHistory | boolean | false | Enables back/forward browser navigation (requires updateUrl: true) |
preload | false | "hover" | "instant" | false | Default preload mode for data-swappit-handle links |
Every handle must be unique. If you try to create a second instance with the same handle, Swappit throws an error. To reuse an existing instance, retrieve it from the registry instead:
Calling update()
update(url, useCache?) fetches the HTML at url, finds all matching data-[handle]-update regions in the response, and replaces the corresponding elements in the current DOM — without a page reload.
URL validation: Swappit only accepts internal relative URLs for security. A valid URL must start with
/ or ./. External URLs, bare paths, and paths starting with ../ all throw an error.update() emits two lifecycle events you can listen to:
Preloading content
preloadContents(arrayUrls) fetches and caches a list of URLs in parallel without touching the DOM. Any subsequent update() call with useCache: true (the default) will then resolve instantly from the cache instead of making a network request.
preloadContents() deduplicates the array automatically, so passing the same URL more than once is safe.
Updating options with reinit()
reinit(options) merges new options into the existing configuration — only the keys you provide are overwritten — and then re-registers the history listener and DOM observer with the updated settings:
reinit() useful for toggling features dynamically at runtime without destroying and recreating the instance.
swappit:my-app:reinit event on window when complete.
Destroying an instance
destroy() tears down the instance completely and frees all its resources:
Marks as destroyed
Sets an internal flag that makes
update(), preloadContents(), and reinit() throw if called afterward.Clears the cache
Empties the content cache and cancels in-flight requests by advancing the request ID counter.
Removes the popstate listener
Deregisters the
popstate handler if history navigation was active, and releases the global __swappitNavigationController lock.Removes all link listeners
Strips
click, mouseenter, and touchstart listeners from every data-swappit-handle link that this instance was managing.Emits the destroy event
Dispatches
swappit:[handle]:destroy on window so listeners can react before the instance disappears.destroy(), you can create a brand-new instance with the same handle:
Accessing all instances
Swappit.instances is a static Map that holds every active Swappit instance, keyed by handle. You can inspect it, iterate over it, or retrieve a specific instance by name:
Only one instance at a time can control browser history. If you try to create (or
reinit()) a second instance with both updateUrl: true and enableHistory: true, Swappit throws:
"Swappit: Solo una instancia puede controlar el historial."
If you need to transfer history control, call destroy() on the current history-controlling instance first — or use reinit() to disable history on it before enabling it on another.