Swappit instances expose four public methods. All methods throw if called on a destroyed instance — the sole exception isDocumentation 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.
destroy() itself, which is a no-op when the instance is already destroyed.
update()
Fetches the HTML aturl, finds all elements in the fetched document that carry matching data-[handle]-update region names, and replaces the corresponding elements in the live DOM. If the updateUrl option is active, the browser address bar is updated before the DOM is modified.
The method emits three lifecycle events on window:
swappit:[handle]:update:before— fired immediately before the fetch starts.swappit:[handle]:update:after— fired after the DOM has been successfully updated.swappit:[handle]:update:error— fired if the fetch or DOM update fails.
Parameters
The relative URL of the page to fetch. Must start with
/ or ./. External URLs and relative paths starting with ../ are rejected with an error.Controls cache behavior for this specific call.
true(default) — if the URL was already fetched and stored, the cached document is used and no network request is made.false— always fetches fresh content from the network, even if a cached version exists.
Returns
Promise<void>
Example
preloadContents()
Fetches and caches content for an array of URLs in parallel without modifying the DOM. Use this to warm the cache before the user navigates so that subsequentupdate() calls resolve instantly from cache.
The URL list is deduplicated automatically. The underlying implementation uses Promise.allSettled(), so a failure on one URL does not abort the rest of the batch.
Syntax
Parameters
Array of relative URLs to preload. Each URL must follow the same rules as
update() — it must start with / or ./. Duplicates in the array are ignored. An empty array or a non-array value logs a warning and returns early without throwing.Returns
Promise<void>
Example
reinit()
Merges the providedoptions into the current options object (shallow merge), then re-initializes the history handler and the DOM observer. Only the keys you supply are overwritten; all other options retain their previous values.
After merging, reinit() emits the swappit:[handle]:reinit event on window.
Syntax
Parameters
Partial options object. Only the keys present in this object are overwritten on the instance. Omitted keys keep their current values.Accepts the same fields as the constructor
options parameter: log, updateUrl, enableHistory, and preload.Returns
void
Example
destroy()
Permanently destroys the instance. Performs all cleanup in this order:- Marks
instance.destroyed = trueto block future method calls. - Clears the content cache.
- Cancels in-flight requests by incrementing the internal request ID counter.
- Removes the
popstateevent listener (if history was enabled). - Disconnects the
MutationObserverthat watched for new links. - Removes
click,mouseenter, andtouchstartlisteners from all managed<a>elements. - Releases the global navigation controller lock if this instance held it.
- Emits
swappit:[handle]:destroyonwindow. - Removes the instance from
Swappit.instances.
destroy() returns, calling update(), reinit(), or preloadContents() on the same reference throws an error. Calling destroy() again on an already-destroyed instance is a safe no-op.
Syntax
Returns
void