ReviewOS

stacks/stx

Included non-signal `<script client>` (useStore-only / plain functions) registers an empty scope — bindings silently dead

#1766
Closed glennmichael123 opened this 22 days ago · 0 comments
22 days ago

Summary

An @included component whose <script client> contains no state( / derived( / effect( call — e.g. a component that only does useStore(...), or only declares plain functions for @click handlers — gets an empty scope registration. Every template binding that references the script's top-level declarations then throws a ReferenceError that the runtime swallows, so x-class / :text evaluate to '' and @click handlers do nothing. No console output — the component just renders dead.

Repro

<!-- components/ConfirmHost.stx — mounted from a layout -->
<script client>
  const confirmStore = useStore('confirm')
</script>

<div
  x-class="confirmStore.open() ? 'fixed inset-0 z-[1100] flex items-center justify-center p-4 bg-black/40' : 'hidden'"
  @click="confirmStore.dismiss()"
>
  <div role="dialog" class="w-full max-w-md p-6 bg-white rounded-2xl shadow-xl">
    <h2 :text="confirmStore.title()"></h2>
    ...
  </div>
</div>

Result: the wrapper's x-class never resolves, class stays "", and the dialog paints as a permanently-visible unstyled shell at the bottom of every page. :text labels stay empty; dismiss/accept clicks are inert.

The emitted scope script shows the problem directly (compare with a component that also calls derived()):

// useStore-only component — empty registration:
const confirmStore = useStore("confirm");
window.stx._scopes['stx_scope_ConfirmHost_102'] = { __destroyCallbacks: __destroyHooks };

// component with a derived() — full registration:
const toast = useStore("toast");
const items = derived(() => toast.items());
var __scopeRegistration = { __destroyCallbacks: __destroyHooks };
try { __scopeRegistration["toast"] = toast; } catch (e) {}
try { __scopeRegistration["items"] = items; } catch (e) {}
window.stx._scopes['stx_scope_ToastHost_101'] = __scopeRegistration;

Root cause

packages/stx/src/includes.ts:

  • hasSignalApis() (~line 149) gates which emitter runs: /\b(state|derived|effect)\s*(?:<[^<>()]*>)?\s*\(/
  • The signal branch runs extractExports() and registers each top-level declaration into __scopeRegistration.
  • The non-signal branch (~line 1070) registers only { __destroyCallbacks: __destroyHooks }. The comment says "The scope vars object can be empty (we have no top-level reactive declarations to register)" — but a useStore(...) handle (or any plain const/function referenced by a binding) is exactly such a declaration.

The failure is then silent because evalAttrExpr in signals.ts deliberately suppresses ReferenceError/TypeError and returns ''.

Suggested fix

In the non-signal branch, run the same extractExports() + try-assignment registration used by the signal branch (the try/catch already guards non-top-level names, and the useRef rationale documented in that branch is unaffected). That also fixes the function-only case (@click="submit()" with no reactive state). Merely adding useStore to hasSignalApis would fix the store case but not plain-function components.

Version: @stacksjs/stx 0.2.72; code identical on main at time of filing.

Sign in to comment on this issue.