Skip to content

v0.7.0 — Optional localStorage TTL for deferred components

Choose a tag to compare

@polidog polidog released this 15 May 14:01
· 116 commits to main since this release
6a36430

Highlights

An optional time bound for the opt-in localStorage (L2) defer cache, via a new Defer::$localCacheTtl (seconds). The cache stays a deliberate per-component opt-in (the localCache boolean from v0.5.0 is unchanged); the TTL is an orthogonal, no-op-by-default refinement, so existing deferred components are byte-identical to before.

Opting in

#[Defer(name: 'feed', localCache: true, localCacheTtl: 60)]
// or
fc($render, defer: new Defer(name: 'feed', localCache: true, localCacheTtl: 60));

Renders a separate data-usephp-defer-cache-ttl="60" attribute next to the bare data-usephp-defer-cache opt-in (mirroring how reloadable added its own attribute, rather than overloading the bare one). localCacheTtl: 0 (the default) emits no attribute — byte-identical markup and behaviour to a plain localCache: true.

Semantics — hard discard, not stale-while-revalidate

Once the persisted entry is older than localCacheTtl seconds, the next read discards it and re-fetches from the network: the fallback shows briefly, then the fresh fragment. There is no stale paint and no background swap. The bound applies to the L2 localStorage tier only — L1 in-memory is per-page anyway. The TTL lives on the placeholder, not in the stored record, so re-tuning it needs no DEFER_CACHE_VERSION bump, and a missing/garbled storedAt is treated as infinitely old (fail-safe re-fetch).

Lenient input

Any localCacheTtl <= 0 means "no time bound": a negative is normalised to 0 and read as such rather than throwing. Only a genuinely positive TTL without localCache: true is rejected (it would bound an entry that's never written) — the real misconfiguration.

Why not the old localCacheTtl?

v0.5.0's predecessor coupled a mandatory TTL to the opt-in and was dropped per feedback in favour of a boolean. This reintroduces the time bound decoupled: boolean opt-in stays, TTL is optional and defaults to a true no-op.

Docs & tests

  • New "Optional time bound" / "時間で失効させる" subsection in README.md / README.ja.md.
  • Added tests: Defer default/propagation/normalisation and positive-without-localCache rejection; renderer markup contract (emit/omit/compose order, raw non-positive path); fc() propagation. Full suite (370 tests), php-cs-fixer (0/70), PHPStan pass; JS syntax checked.

Full PR: #22 (Copilot review addressed: <= 0 treated as 0 per maintainer call — deliberately lenient instead of throwing; plus $localCache doc, header-comment grammar, and enforcePersistedCap comment accuracy).

Full Changelog: v0.6.0...v0.7.0