-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Handle x-teleport directive #50
Conversation
`x-teleport` directives break things when navigate backward/forward. Cleaning those before caching seems to solve the issue.
Thanks, any chance you could add a test for it? |
This works, I have merge this patch to my local package, thanks, @SimoTod |
Thanks tagged |
@SimoTod I found this only fixed a half, let's think there are 4 clicking paths:
I'm trying to figure out why the first page got error, it seems Turbo doesn't cache first page, so teleport target won't be tagged as 'data-alpine-generated-me' in the renderCacheCallback. @sukei , have you already fixed this issue? |
mmmh, that seems quite odd. Are you saying that if you navigate to page 2 and back, it works; navigate to page 3 and back twice, it works; but if you navigate to page 4 and back three times, it doesn't? |
Not exactly, just Page 2 back to Page 1 raise error. this happens when page has turbo frame, turbo frame will trigger turbo:render-cache, I'm confused on the frame caching logic, can't figure out what Turbo should do, or alpine-turbo-drive-adapter should do. Now, I use this dummy script to fix my own issue: document.addEventListener("turbo:before-render", (event) => {
window.Alpine.mutateDom(() => {
if (document.documentElement.hasAttribute('data-turbo-preview')) {
return
}
// I have only one teleport target to place all dropdowns/modals, so hard code it
event.detail.newBody.querySelectorAll('#x-teleport-target > *').forEach((el) => el.remove())
})
window.Alpine.deferMutations()
}) Also I found this update https://github.com/alpinejs/alpine/blob/main/packages/alpinejs/src/directives/x-teleport.js#L21 When this is released, I think we can easyly remove teleport targets by: document.addEventListener("turbo:before-render", (event) => {
window.Alpine.mutateDom(() => {
if (document.documentElement.hasAttribute('data-turbo-preview')) {
return
}
event.detail.newBody.querySelectorAll('[data-teleport-target]').forEach((el) => el.remove())
})
window.Alpine.deferMutations()
}) |
FYI, if you use the latest turbo version, without this library, you can add a data-turbo-temporary to your HTML to prevent Turbo from caching your template (https://turbo.hotwired.dev/handbook/building#preparing-the-page-to-be-cached). It may be better than trying to fix the HTML via JS on the fly |
Maybe this is a bug, I will post this to Turbo repo |
@xiaohui-zhangxh do you have a simple example so I can replicate it (I need the generated HTML)? If the page is not cached, it shouldn't even contain the teleported div which is kind of confusing me. |
Yes, I do. see this hotwired/turbo#1008 |
seems you are right, if dom is not cached, it shouldn't render on clicking backward. let's write more x-teleport example |
|
x-teleport
directives break things when navigate backward/forward. Cleaning those before caching seems to solve the issue.