Best technique to lazy load components #4246
Unanswered
gjcourtney
asked this question in
1. Help
Replies: 2 comments 2 replies
-
Using that same thing, instead of injecting a script tag, you can just dynamic import the file and wait for it to finish and then inject the html |
Beta Was this translation helpful? Give feedback.
2 replies
-
Use Async Alpine in combination with Alpine lazy load assets |
Beta Was this translation helpful? Give feedback.
0 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.
-
I wonder if anyone can offer the best approach to lazy load new components into
Alpine.data()
as they're required... I'll try to explain:I'm working on a large application where lots of pages/modals have an Alpine component that's specific to that page/modal. We have a bundle for all of the common/shared components, but I'm looking to improve the way that we deliver the JS for individual components over the wire. At the moment, when the content for a modal loads through an AJAX request, we deliver the HTML and JS (inline) alongside each other, e.g.
This works fine, but I'd like to try and lean into loading separate JS files (which would benefit from browser caching), rather than delivering the JS inline. In my head, I should be able to do something like this:
The problem with this approach is it creates a race where the browser attempts to load specificComponent.js over the network, but most likely won't succeed before Alpine attempts to re-paint the page and discovers the component doesn't exist in
Alpine.data()
.I feel like there's a lazy loading technique for this scenario I must be missing, but can't work it out... Anyone able to point me in the right direction?
Beta Was this translation helpful? Give feedback.
All reactions