Skip to content

v0.4.0 - Component-side defer declaration

Choose a tag to compare

@polidog polidog released this 15 May 06:07
· 131 commits to main since this release
e637812

Defer metadata moves from the PSX call site onto the component itself, and usephp compile auto-discovers every endpoint. This is a breaking change to the defer API — the magic defer / fallback PSX attributes are gone and replaced with explicit wrapper components.

Breaking: defer config lives on the component (#17)

Before (v0.3.0):

<UserHeader defer="user-header" fallback={<HeaderSkeleton />} />
$app->registerDeferred(
    name: 'user-header',
    component: 'App\\Components\\Psx\\UserHeader',
    cacheControl: 'private, no-store',
);

After (v0.4.0): split into a base component and a deferred wrapper that carries the Defer config.

{/* UserHeaderDeferred.psx — the wrapper */}
use Polidog\UsePhp\Component\Defer;
use function Polidog\UsePhp\Runtime\fc;

return fc(
    fn(array $props) => <UserHeader />,
    defer: new Defer(name: 'user-header', cacheControl: 'private, no-store'),
);
{/* Page.psx — fallback is a normal prop */}
<UserHeaderDeferred fallback={<HeaderSkeleton />} />
// No registerDeferred() call — loadComponentManifest() picks up the
// deferred-manifest.php sidecar that `usephp compile` writes.
$app->loadComponentManifest($manifestPath);

Class components use the same idea via #[Defer(...)]:

#[Component(name: 'UserHeaderDeferred')]
#[Defer(name: 'user-header', cacheControl: 'private, no-store')]
final class UserHeaderDeferred extends BaseComponent
{
    public function render(): Element { /* the endpoint-side content */ }
}

$app->register(UserHeaderDeferred::class); // endpoint + PSX placeholder both wired

Why

  • Single source of truth for each defer endpoint — name and cache policy travel with the component, no parallel registerDeferred() list to keep in sync.
  • usephp compile enumerates every endpoint at build time, so the framework can auto-register them; manual wiring goes away.
  • Removes the PSX parser's special-casing of defer and fallback — every prop is now a normal prop.

Migration

  1. For each component that was used with defer="…" in PSX, create a wrapper component:
    • Closure form: new .psx returning fc(fn(\$props) => <Base />, defer: new Defer(name: '…', cacheControl: '…')).
    • Class form: new BaseComponent subclass annotated with #[Defer(name: '…', cacheControl: '…')].
  2. Replace <Base defer="…" fallback={…} /> with <BaseDeferred fallback={…} /> at every call site.
  3. Drop the corresponding \$app->registerDeferred(...) calls.
  4. Re-run usephp compile so the new deferred-manifest.php sidecar is generated.

Other changes

  • fc() now returns an invokable FunctionComponent (still callable, exposes defer for inspection).
  • loadComponentManifest() also picks up deferred-manifest.php next to manifest.php.
  • Defer::buildPlaceholder() is the shared materialiser used by both the closure wrapper and the class-component PSX bridge.

Full Changelog: v0.3.0...v0.4.0