Re-running scripts
Sparke keeps the JS context alive across swaps. Re-run page code
with data-sparke-rerun or an event.
Sparke keeps the JS context alive across navigations, so scripts in fetched pages don't re-run automatically. Most of the time that's exactly what you want. When a page needs its own script to run on each visit, mark it.
data-sparke-rerun
Mark a page's own script with data-sparke-rerun and
Sparke runs it on each swap. It must live in the swapped region
(inside <main>, or anywhere in
<body>).
<main>
<canvas id="chart"></canvas>
<!-- re-runs every time this page is swapped in -->
<script data-sparke-rerun>
initChart(document.getElementById("chart"));
</script>
<!-- run only on the first visit, then never again -->
<script data-sparke-rerun="once" src="/widgets.js"></script>
</main>
A marked script re-runs in the same live context, so keep it
idempotent: avoid top-level
const/let/class, and
clean up old listeners and timers.
Site-wide logic
For things that run on every page (analytics, re-binding widgets), listen once instead of repeating code on every page:
window.addEventListener("sparke:after-swap", (e) => {
initMyWidgets();
// e.detail.from / e.detail.to are the URLs
});
See Events for the full set, and Migrating a site for an init checklist.