Skip to content

Events

Handle SPF events and the navigation life cycle.

SPF is designed to give developers enough flexibility during navigation to both control application logic and provide UI updates for things like progress bars. It dispatches spf* events at each step of a navigation.

A navigation runs the pipeline in this order (entry point in brackets):

text
api|navigate   dom|click     history|traverse    prefetch
     |             |               |                |
     |        [ spfclick ]   [ spfhistory ]         |
     +-------------+---------------+----------------+
                   |
            [ spfrequest ]  ── cancel → fallback reload
                   |
     cache lookup → fetch (single JSON or NDJSON stream)
                   |
            [ spfprocess ] ── cancel → fallback reload
                   |
     apply: title → url → head → attr → body → foot
                   |
              [ spfdone ]
EventWhenCancel effect
spfreadyAfter init() wires up.
spfclickA valid spf-link was clicked.Browser navigates normally.
spfhistoryBack/forward to one of our entries.Browser handles it (full load).
spfrequestBefore the request is sent.Pipeline aborts → fallback reload.
spfprocessResponse received, before applying.Fallback reload.
spfdoneResponse applied.
spferrorRequest failure or reload: true.
spfreloadFalling back to a full navigation.
spfpartprocessA streamed part arrives, before aggregating.The part is skipped.
spfpartdoneA streamed part was aggregated.

Cancellation

Two dispatch mechanisms, with matching cancel semantics:

  • Promise-aware registry — handlers registered with spf.event.on(name, fn) run first, may be async, and are awaited in order; returning false cancels the event. Used for the pipeline events (spfrequest, spfprocess, spfdone, spferror, spfpartprocess, spfpartdone).
  • Document CustomEvent — a cancelable event is then dispatched on document, so document.addEventListener('spfrequest', …) works like classic SPF and external listeners cancel with preventDefault().

The click-time events (spfready, spfclick, spfhistory, spfreload) are dispatched synchronously and honor only the document CustomEvent — registering them with spf.event.on has no effect; cancel with document.addEventListener(name, e => e.preventDefault()).

Callbacks and cancellations

Instead of handling events, programmatic navigation may pass callbacks in a RequestOptions object — spf.navigate(url, { onRequest, onProcess, onDone }). Canceling aborts the pipeline; for a navigation this falls back to a full browser load, for a prefetch it simply aborts:

CallbackStateCancel
onRequestStarted; sending requestFallback reload (prefetch: abort).
onProcessProcessing; response receivedFallback reload (prefetch: abort).
onDoneDone

See the Events API reference for the registry methods and per-event detail fields.