Skip to main content

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 dispatches CustomEvent instances on window during its lifecycle. All events carry a detail object with handle (string) and url (string) properties.
All event names follow the pattern swappit:[handle]:[action]. For example, an instance with handle "app" emits swappit:app:update:after.

Events

EventWhen emitteddetail.url
swappit:[handle]:update:beforeBefore fetching startsThe target URL
swappit:[handle]:update:afterAfter DOM update succeedsThe target URL
swappit:[handle]:update:errorOn fetch or update errorThe target URL
swappit:[handle]:historyUpdate:beforeBefore back/forward navigation updateThe current pathname + search
swappit:[handle]:historyUpdate:afterAfter back/forward navigation updateThe current pathname + search
swappit:[handle]:historyUpdate:errorOn error during history navigationThe current pathname + search
swappit:[handle]:reinitAfter reinit() completes"" (empty string)
swappit:[handle]:destroyAfter destroy() completes"" (empty string)

Event detail shape

Every event’s detail object contains the following fields:
handle
string
The handle of the Swappit instance that emitted the event.
url
string
The URL involved in the operation. For update and historyUpdate events this is the relative URL being loaded. For reinit and destroy events this is an empty string "".

Complete usage example

const app = new Swappit('app', { log: true });

// Update lifecycle
window.addEventListener('swappit:app:update:before', (e) => {
  console.log('Starting update:', e.detail.url);
  showLoadingSpinner();
});

window.addEventListener('swappit:app:update:after', (e) => {
  console.log('Update complete:', e.detail.url);
  hideLoadingSpinner();
  // Re-run any inline scripts inside updated regions if needed
  const scripts = document.querySelectorAll('[data-app-update] script:not([src])');
  Swappit.updateScriptByContent(Array.from(scripts));
});

window.addEventListener('swappit:app:update:error', (e) => {
  console.error('Update failed:', e.detail.url);
  showErrorMessage();
});

// History navigation
window.addEventListener('swappit:app:historyUpdate:after', (e) => {
  console.log('Navigated to:', e.detail.url);
});

// Instance lifecycle
window.addEventListener('swappit:app:reinit', () => {
  console.log('Instance reinitialized');
});

window.addEventListener('swappit:app:destroy', () => {
  console.log('Instance destroyed');
});

Build docs developers (and LLMs) love