Skip to content
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

Merged
merged 2 commits into from
Sep 17, 2023
Merged

Handle x-teleport directive #50

merged 2 commits into from
Sep 17, 2023

Conversation

sukei
Copy link
Contributor

@sukei sukei commented Mar 9, 2022

x-teleport directives break things when navigate backward/forward. Cleaning those before caching seems to solve the issue.

Quentin Schuler and others added 2 commits March 9, 2022 14:52
`x-teleport` directives break things when navigate backward/forward. Cleaning those before caching seems to solve the issue.
@SimoTod
Copy link
Owner

SimoTod commented Mar 9, 2022

Thanks, any chance you could add a test for it?

@xiaohui-zhangxh
Copy link

This works, I have merge this patch to my local package, thanks, @SimoTod

@SimoTod SimoTod merged commit ca65389 into SimoTod:master Sep 17, 2023
@SimoTod
Copy link
Owner

SimoTod commented Sep 17, 2023

Thanks tagged

@xiaohui-zhangxh
Copy link

Thanks tagged

@SimoTod I found this only fixed a half, let's think there are 4 clicking paths:

Page 1 -> Page 2 -> Page 3 -> Page 4

When click browser backward button:

Page 1 -> Page 2 -> Page 3  => works
Page 1 -> Page 2  => works
Page 1 => boom, uncaught TypeError: Cannot read properties of undefined (reading '_x_refs')

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?

@SimoTod
Copy link
Owner

SimoTod commented Sep 18, 2023

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?

@xiaohui-zhangxh
Copy link

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()
})

@SimoTod
Copy link
Owner

SimoTod commented Sep 18, 2023

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

@xiaohui-zhangxh
Copy link

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

data-turbo-temporary doesn't work, it remove temporary dom on turbo:before-cache, we should remove them on turbo:before-render. The problem is first open URL, content won't cache, when click a link that targets to turbo frame, turbo:before-cache triggered but the cached content is the one AFTER clicking turbo frame link.

Maybe this is a bug, I will post this to Turbo repo

@SimoTod
Copy link
Owner

SimoTod commented Sep 19, 2023

@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.

@xiaohui-zhangxh
Copy link

@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

@xiaohui-zhangxh
Copy link

@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.

seems you are right, if dom is not cached, it shouldn't render on clicking backward. let's write more x-teleport example

@xiaohui-zhangxh
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants