Skip to content

Types

The public TypeScript types exported by modern-spf.

SingleResponse

A single (non-streamed) SPF response. All fields optional.

FieldTypeMeaning
titlestringNew document title.
urlstringCorrect URL for this request; replaces the current history entry.
headstringHTML containing early CSS/JS, installed into <head>.
footstringHTML containing late JS/CSS, installed after body.
bodyRecord<string, string>Map of element IDs to HTML (the fragments).
attrRecord<string, Record<string, string>>Map of element IDs to attribute name/value maps.
redirectstringA URL to request instead of this one.
reloadbooleantrue → perform a full page reload.
cacheKeystringCache key the client used/stores for this response.
cacheTypestringCache eligibility hint: new | history | prefetch.
timingRecord<string, number>Arbitrary server timings, surfaced on spfdone.
dataunknownReserved for client data.

StreamPart

One line of a streamed (NDJSON) SPF response — a discriminated union:

ts
type StreamPart =
  | { part: 'title'; value: string }
  | { part: 'url'; value: string }
  | { part: 'head'; html: string }
  | { part: 'attr'; id: string; attrs: Record<string, string> }
  | { part: 'body'; id: string; html: string }
  | { part: 'foot'; html: string }
  | { part: 'done' };

RequestOptions

Options for requesting a URL.

FieldTypeMeaning
method'GET' | 'POST'HTTP method; defaults to GET.
headersRecord<string, string>Extra headers to send.
postDataBodyInitBody for POST requests.
withCredentialsbooleanInclude credentials (cookies) in the request.
refererstringSent as X-SPF-Referer — the URL of the page being left (history navigations).
currentstringSent as X-SPF-Previous — the URL being left.
onRequestSpfEventHandlerInvoked after the spfrequest event (navigate/load only); the return value is ignored — cancel via the event.
onProcessSpfEventHandlerInvoked after the spfprocess event (navigate/load only); the return value is ignored — cancel via the event.
onDoneSpfEventHandlerInvoked after processing is done (navigate/load only).
onPartProcessSpfEventHandlerNot invoked by the current implementation — use the spfpartprocess event.
onPartDoneSpfEventHandlerInvoked per streamed part after it is aggregated (navigate/load only).
onErrorSpfEventHandlerInvoked on request failure (navigate/load only).
noTransitionbooleanDisable view transitions for this navigation.

SpfEventDetail

Detail object passed to SPF event handlers and request callbacks.

FieldTypeMeaning
urlstringThe URL of the request.
previousstringThe URL of the page being left (spfhistory).
refererstringThe URL of the page being left (spfrequest).
targetElement | nullThe clicked element (spfclick).
responseSingleResponseThe (possibly partial) response (process/done/part events).
partStreamPartOne part of a streamed response (part events).
errErrorThe error that occurred (spferror, request failures only).
transitionedbooleanWhether view transitions were used for this navigation.

SpfEventHandler

ts
type SpfEventHandler = (detail: SpfEventDetail) => boolean | void | Promise<boolean | void>;

Returning false cancels the event; handlers may be async.

SpfEventName

The canonical SPF event names:

ts
type SpfEventName =
  | 'spfready'
  | 'spfclick'
  | 'spfhistory'
  | 'spfrequest'
  | 'spfprocess'
  | 'spfdone'
  | 'spferror'
  | 'spfreload'
  | 'spfpartprocess'
  | 'spfpartdone';

SpfRequestType / SpfNavigationType

ts
type SpfRequestType = 'navigate' | 'prefetch' | 'load'; // selects the URL identifier value
type SpfNavigationType = 'new' | 'history'; // drives cache eligibility

SpfConfig

The configuration shape — see the Configuration guide for the full key table.

Internal: SpfConfig is defined in src/config.ts but is not re-exported from the package entry — the public spf.config API is typed against it. The same applies to ApplyOptions (used by spf.process) and CacheType (used by spf.cache). They appear here for reference; the exported public types are SingleResponse, StreamPart, RequestOptions, SpfEventDetail, SpfEventHandler, SpfEventName, SpfRequestType and SpfNavigationType.