Single Page XHR - The essential enhancement for static SSR powered web applications. SPX is a lightening fast, lightweight (15kb gzip) over the wire (push~state) solution that employs an incremental (snapshot) morphing tactic with DOM driven component capabilities integrated at the core.
Documentation lives on spx.js.org
- Simple and painless drop-in integration.
- Supports components extendability.
- Pre-fetching capabilities using hover, intersection or proximity observers.
- Snapshot caching engine and per-page state control.
- Powerful pub/sub event driven lifecycle triggers.
- Gracefully handles script and style asset evaluation.
- Attribute driven programmatic control.
pnpm add spx
yarn add spx
npm install spx --save
https://unpkg.com/spx
SPX is distributed as an ESM module and designed for usage in browser. You need to establish a connection to invoke the module.
import spx from 'spx';
spx.connect({
fragments: [
'menu',
'main'
]
})(function (session) {
// Callback is invoked on DOMContentLoaded.
console.log(session)
});
An SPX connection will initialize an SPX session. In the above example we are targeting the <nav>
and <main>
element fragments. In your web application, ensure both elements exist for each page, for example:
<header>
<nav id="menu">
<a href="/">Home</a>
<a href="/about">About</a>
</nav>
</header>
<main id="main">
<h1>Hello World!</h1>
</main>
For more advanced cases, SPX provides component extendability. Register and connect components to DOM elements, and use attribute driven control for state, events, and element queries:
import spx from 'spx';
class Counter extends spx.Component {
static define = {
nodes: ['count'],
state: {
clicks: Number
}
}
increment () {
this.countNode.innerText = +this.state.clicks
}
}
spx.register(Counter);
Connect the component to DOM elements, add the following to a defined fragment
and allow SPX to do the rest:
<section spx-component="counter">
Clicked: <span spx-node="counter.count">0</span>
<button type="button" spx@click="counter.increment">
Increment
</button>
</section>
Contributions are welcome! This project uses pnpm for package management and is written in TypeScript.
- Ensure pnpm is installed globally
npm i pnpm -g
- Leverage
pnpm env
if you need to align node versions. - Clone this repository
git clone https://github.com/panoply/spx.git
- Run
pnpm i
in the root directory - Run
pnpm dev
for development mode
The project uses tsup for producing the distributed bundle. The test directory contains real-world web application that is spun up within via 11ty.
pnpm dev # Development in watch mode
pnpm build # Bundle distribution builds for production
pnpm docs # Documentation development environment
pnpm test # Spins up the testing web application
Licensed under CC BY-NC-ND 4.0