-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
View Transitions Breaking AlpineJs alpine:init
Event
#11133
Comments
Hey @ZachHandley! Your example does not use view transitions. The warning and the error in the first screenshot are due to What is your issue and how can i help you? |
@martrapp Sorry, I suppose I should have advanced the example a bit to showcase what I mean. https://stackblitz.com/edit/github-zdycxn?file=src%2Fpages%2Findex.astro The issue is, alpine:init doesn't run the same as https://stackblitz.com/edit/github-zdycxn?file=src%2Fpages%2Findex.astro maybe this will explain better? |
Hi @ZachHandley thank you for updating the StackBlitz! I will take a look at right now. |
Hi @ZachHandley, could you please change the stackblitz a bit:
|
Hi @ZachHandley, if you want to use Astro's scripts (hoisted, bundled, module, typescript, ...) you must define the x-data entries before they are used in HTML elements. The simplest way to do this would be to move the scripts for all your pages into the Layout component and use the That way, they all get executed on the initial page load (no matter if that is page one or two) and are available when you navigate to another page using view transitions. If you dislike having all code in the layout, you could move it to separate .ts files as long as you import them all in a script tag in the layout. Now that I assume that you still want to go the other route having per page scripts embedded in the Astro files as shown in your example, you can try to add this script to your Layout: <script>
import Alpine from "alpinejs";
document.addEventListener("astro:after-preparation", () => {
Alpine.stopObservingMutations();
});
document.addEventListener("astro:page-load", () => {
document.dispatchEvent(new Event("alpine:init"));
Alpine.initTree(document.documentElement);
Alpine.startObservingMutations();
});
</script> Sadly, Alpine only offers a
It is important to stop the MutationObserver, because it will react to DOM modifications rather directly. If it is not disabled, the Let me know, which route you went and whether it worked 👍🏼 for you! |
That works, it's not a perfect solution so I ended up going to Vue, but thank you regardless it's really cool that you can even do that haha |
@martrapp Thank you for your contributions on this issue; they have been very helpful. I was able to implement the "per page scripts embedded in the Astro files" approach in my application. However, I ran into a couple of issues.
Therefore, on first page load, Alpine initiates on the entire page. However, when you switch pages, it only destroys and rebuilds the main slot in the layout. I have not noticed any issues with this approach, however, it could be problematic depending on the structure and necessary functionality of your application.
|
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
Basically, I prefer to write my AlpineJs code inside script tags. It's cleaner, I benefit from Astro's TypeScript support, and I don't have to look at ugly JS in HTML quotes haha. SO anyways, AlpineJs has the event that fires,
alpine:init
. The issue I'm having is that, even if I inline the script, Astro'spage-load
event fires first. Which it should(? maybe) but I'm unable to get this to work. Take this exampleThis will error, because Alpine won't be defined yet
What's the expected result?
It would be awesome if
astro:page-load
waited until integrations were loaded, but I'm unsure of the best way to resolve it. I think either way, it should be fixed so one can use AlpineJs inside script tags. I could maybe make a function that waits for it to be initialized? I'm not sure yetLink to Minimal Reproducible Example
https://stackblitz.com/edit/github-zdycxn?file=src%2Fpages%2Findex.astro
Participation
The text was updated successfully, but these errors were encountered: