Is it possible to initialize Alpine.data() in a script tag that is swapped in (via HTMX), for CSP reasons. #4064
Unanswered
kg-currenxie
asked this question in
1. Help
Replies: 1 comment 2 replies
-
You need to have the script tags to added to the page, wait a tick and then add the elements. You also cannot call Alpine.start multiple times. Just call it once at the end of the normal document. Basically, add Alpine to the window object in the head. You component script tags directly do Alpine.data, no event listening, or anything. The end of the body calls start. When you swap, you add the script tags first, then the elements. You can probably do this fine with an HTMX plugin. Just strip out the script tags, copy them to make them evaluatable, and then insert them into the head, wait a tick, replace the elements |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We are converting our app to the CSP build.
Our pages are swapped in with HTMX. To not increase our js-bundle size drastically, we want to initialize a new data directive in the HTML that coming in from the server that is being swapped in with HTMX.
Example:
Alpine part to reproduce, if you can simulate a swap 😅
So the flow is:
Alpine.start()
.x-data
is being processed immediately, because we already didAlpine.start()
, but thex-data="test"
doesn't exist yet. I'm guessing Alpine's MutationObserver runs before the newly inserted script tag? 🤔Is there another way to make sure this works? (without warnings), am I missing something?
or is putting everything in our bundle the only way? if yes, any form of code splitting will never work
Thanks 🙏
Beta Was this translation helpful? Give feedback.
All reactions