GOTTH+
GitHub

Building

Alpine.js without eval

Client-only state with Alpine's CSP build, and why components are registered by name.

Why the CSP build

The strict Content-Security-Policy forbids 'unsafe-eval', and Alpine's standard build evaluates expressions with it. The CSP build can't evaluate inline objects or statements, so every component is registered in static/js/app.js with Alpine.data and referenced by name.

Alpine.data('copyText', () => ({
    copied: false,
    async copy() { /* ... */ },
}))

<div x-data="copyText">
  <button type="button" @click="copy">Copy</button>
</div>

The CSP build reads property names and method references, not arbitrary expressions. Use a getter for a negation, such as idle, instead of writing !copied in the template.

Shared state

The globalState store is shared by every component, and the htmx hooks in app.js keep its pendingRequests count and online flag current. Read it with $store.globalState.

What Alpine is for

Keep Alpine for state that never needs the server: a copy button, a disclosure, a menu. Anything that changes data goes through htmx, so the server stays the source of truth.

Was this page helpful?

Be the first to answer.