-
The SetupI have components that combine Alpine.js for interactivity and HTMX for dynamic loading. Each component contains its Alpine.js initialization and markup as a self-contained unit. The ProblemWhen loading Alpine.js components dynamically via HTMX, the component initialization breaks:
Example component that breaks when loaded via HTMX: <div>
<script nonce="valid-nonce">
Alpine.data('toastComponent', () => ({
show: true,
// component lifecycle & state management
}))
</script>
<div
x-data="toastComponent"
x-show="show"
x-transition
>
<!-- component markup -->
</div>
</div> Current WorkaroundTo make it work, I had to break the component into two parts:
<!-- Must be loaded in advance -->
<script nonce="valid-nonce">
Alpine.data('toastComponent', () => ({
show: true,
// component logic
}))
</script>
<div x-data="toastComponent">
<!-- just markup, no initialization -->
</div> This works but feels wrong - it breaks component encapsulation and makes components harder to use. Questions
Would love to hear from the Alpine.js community on this! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
It's not strictly alpine but htmx. There should be an option to tell htmx to add the nonce for you if you look at their documentation (there are security rules in the browser and a script imported via js won't run by default so htmx does some parsing, patching, etc to make it runnable). |
Beta Was this translation helpful? Give feedback.
-
Do you have a complete example? You shouldn't need to manually re-init
Alpine
…On Tue, 17 Dec 2024 at 08:53, Axel Adrian ***@***.***> wrote:
Thank you very much. This works, but now I can't re-initialize Alpine to
recognize the handlers again! 🥴
—
Reply to this email directly, view it on GitHub
<#4478 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACAJRWKKXJFC7LWBQY5G45T2F7Q67AVCNFSM6AAAAABTWF5WIWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNJZGA2DKNQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
htmx.config.inlineScriptNonce