Skip to content

Events

SPF dispatches spf* events (concept-compatible names with the original SPF). See the Events guide for the navigation life cycle; this page is the registry API reference. Dispatch is Promise-aware for the pipeline events: handlers registered with spf.event.on run first, are awaited in order, and a handler returning false cancels the event; then a cancelable CustomEvent is dispatched on document (so document.addEventListener('spfrequest', …) also works, and external listeners may cancel via preventDefault()).

The click-time events (spfclick, spfhistory, spfready, spfreload) are dispatched synchronously via emitSync and only honor the document CustomEvent — they skip the spf.event registry entirely, so registering a handler for them with spf.event.on has no effect (cancel them with document.addEventListener(name, e => e.preventDefault()) instead).

spf.event

MethodSignatureEffect
on(name, handler) → () => voidRegister a handler; returns an unsubscribe function.
off(name, handler) → voidUnregister a handler.
clear() → voidRemove all handlers (tests/dispose).
emit(name, detail) → Promise<boolean>Dispatch (registry handlers, then document event); resolves false if cancelled.
emitSync(name, detail) → booleanSynchronous dispatch — document CustomEvent only (click-time events).

Handlers have the type (detail: SpfEventDetail) => boolean | void | Promise<boolean | void>.

Event reference

EventDetail fieldsWhenCancel effectRegistry?
spfreadyAfter init() wires up.no (sync)
spfclickurl, targetA spf-link was clicked.Browser navigates normally.no (sync)
spfhistoryurl, previousBack/forward to one of our entries.Browser handles it (full load).no (sync)
spfrequesturl, previous, refererBefore the request is sent.Pipeline aborts → fallback reload.yes
spfprocessurl, responseResponse received, before applying.Fallback reload.yes
spfdoneurl, response, transitionedResponse applied.yes
spferrorurl, err?Request failure (with err), or reload: true (without).yes
spfreloadurl (fallback target)Fallback to a full navigation.no (sync)
spfpartprocesspart, response (partial)A streamed part arrives, before aggregation.The part is skipped.yes
spfpartdonepart, response (partial)A streamed part was aggregated.yes

Example

js
const off = spf.event.on('spfdone', ({ url, response, transitioned }) => {
  console.log('navigated to', url, 'transitioned:', transitioned);
  return undefined; // do not cancel
});

// ...later
off(); // unregister