Skip to content

v0.8.0 — fc() Snapshot partial updates + opt-in client-side snapshot persistence

Choose a tag to compare

@polidog polidog released this 20 Aug 13:21
· 99 commits to main since this release
fa9206c

Highlights

Two fixes for Snapshot-storage components, motivated by the /snapshot example counter that did not work: clicks were silently dropped, and even when they weren't, a reload reset the count.

fc() Snapshot components now get partial updates (#33)

handleAction() only answered partial (AJAX) requests for registry-registered class components. fc() function components always fell through to the PRG redirect, and on an Isolated route nothing carries a snapshot across that redirect — so the state change was lost.

The action handler now replays the page's GET route for partial requests targeting an unregistered Snapshot-storage component, picks the instance's data-usephp wrapper out of the rebuilt Element tree, and returns it via Renderer::renderPartial() with the refreshed data-usephp-snapshot-update field. Only Isolated routes take this path; Persistent/Session/Shared keep redirecting because the redirect is what writes the snapshot into the URL or the session (/cart is unchanged).

Opt-in client-side persistence — SnapshotPersist (#33)

Snapshot storage keeps the only copy of a component's state in the page (like React's useState), so a reload starts over. Components can now opt in to mirroring that snapshot into Web Storage while the server stays stateless:

use Polidog\UsePhp\Storage\SnapshotPersist;

$Counter = fc($render, 'counter', StorageType::Snapshot, persist: SnapshotPersist::SessionStorage);

#[Component(storage: 'snapshot', persist: 'sessionStorage')] // or 'localStorage'
class Counter extends BaseComponent { ... }
  • The wrapper gains data-usephp-persist="sessionStorage|localStorage".
  • usephp.js saves the snapshot after every partial update (keyed by pathname + instance id) and, on the next page load, POSTs it back as a restore action (Action::restore(); no mutation — the posted snapshot is the state). The component is swapped for the server's re-render, which HMAC-verifies the snapshot exactly like any other action. A rejected (400) or redirected restore drops the saved entry.
  • persist is rejected with non-Snapshot storage. New window.usePHP.restoreSnapshots() / forgetSnapshots().
  • Meant for Isolated routes; persistentSnapshot() / sessionSnapshot() routes already carry the snapshot elsewhere.

Docs & tests

  • README.md / README.ja.md: persist: option and the reload semantics of Snapshot storage (#33, #34).
  • New tests: FunctionComponentActionTest (partial update, multi-instance targeting, persist attribute/validation, restore round-trip without mutation, tampered restore rejected) and ComponentPersistTest (attribute, registry, render paths). PHPStan and php-cs-fixer pass.

Full Changelog: v0.7.3...v0.8.0