Comments (19)
Gotcha. Yeah, it's not the traditional use of x-cloak but i can support it, no problem. It will likely be tonight or tomorrow.
from alpine-turbo-drive-adapter.
Glad it solved it (again). It gets complicated when livewire is involved and I can't have unit testa for that. I still need to check how it behaves in ie11 and edge but I should be able to tag 0.2.1 tomorrow or Monday. 👍
from alpine-turbo-drive-adapter.
@heyfadii tonight/tomorrow.
from alpine-turbo-drive-adapter.
I'm using something like this in my project and can PR it if you think it's a good approach
document.addEventListener('turbolinks:before-render', (event) => {
event.data.newBody.querySelectorAll('[x-cloak]').forEach((el) => {
el.setAttribute('was-cloaked', '');
});
});
// Cloak elements again before caching
document.addEventListener('turbolinks:before-cache', () => {
document.body.querySelectorAll('[was-cloaked]').forEach((el) => {
el.setAttribute('x-cloak', '');
el.removeAttribute('was-cloaked');
});
});
This depends on using the x-cloak
attribute, but that seems to be common practice for cases like this.
from alpine-turbo-drive-adapter.
@jduff It seems to be a sensible approach, thanks for sharing.
Does it work on the very first visit when the page is loaded by the browser or do we also need to cover that case as well?
Happy to accept a PR, we can build on that if we need to.
Can we prefix the custom attribute with data-alpine-
please?
from alpine-turbo-drive-adapter.
Does it work on the very first visit when the page is loaded by the browser or do we also need to cover that case as well?
It doesn't look like it's running on the first page load. I've been trying to get that to work from a turbolinks:load
event but it looks like Alpine has already run at that point and removed the x-cloak
attribute. I came across the deferLoadingAlpine
function here which seems to get it to work.
So something like this:
window.deferLoadingAlpine = (startAlpine) => {
document.addEventListener('turbolinks:load', () => {
document.body.querySelectorAll('[x-cloak]').forEach((el) => {
el.setAttribute('was-cloaked', '');
});
startAlpine();
}, {once: true});
};
This sets up the turbolinks:load
in the defer callback to only execute once, it then does the x-cloak
magic and once that's done it starts Alpine.
Does that make sense or is there a better way to do this?
from alpine-turbo-drive-adapter.
do we need to wait for a turbolinks:load event? I assume it could just run.
deferLoadingAlpine is used by other libraries so I don't think we should just override it. We need something like
const init = window.deferLoadingAlpine || ((callback) => callback())
window.deferLoadingAlpine = (callback) => {
document.body.querySelectorAll('[x-cloak]').forEach((el) => {
el.setAttribute('was-cloaked', '');
});
init(callback)
}
To be fair, since Alpine waits for the DOM ready events anyway and the javascript engine runs as a single thread, I suspect it could just go inside the init function (https://github.com/alpinejs/alpine-turbolinks-adapter/blob/master/src/bridge.js#L15) without involving events.
init() {
document.body.querySelectorAll('[x-cloak]').forEach((el) => {
el.setAttribute('was-cloaked', '');
});
//etc etc
}
Any thoughts?
from alpine-turbo-drive-adapter.
That looks like it could work, I'll put together a PR tonight.
I was using the turbolinks:load
event as an alternative to ready
. I think we still want to have it happen after dom ready though, don't we? As far as I can tell init
is called right away and it's possible the DOM isn't fully loaded yet, or am I missing something here?
I think either way I need to put together a PR and we can continue the discussion there :).
from alpine-turbo-drive-adapter.
The idea is that the script is defered so init should run before domready but when all the html is there. I guess we can't force user to defer the script (expecially if they create a bundle by themselves).
I'm happy with the deferLoadinAlpine if it works.
Bonus point if we can write a test but it's not a blocker for this PR.
from alpine-turbo-drive-adapter.
@jduff Unfortunately the change conflicts with the way Livewire work (Livewire is not deferred and the order of events is not correct).
I've got a new version at https://cdn.jsdelivr.net/gh/simotod/alpine-turbolinks-adapter@fc19881d2dae4c2b8b3fd790383d890f26aa14a8/dist/alpine-turbolinks-adapter.min.js
Do you have any use cases I can use to test it works correctly?
@zepfietje can you confirm it fixes your issue as well?
from alpine-turbo-drive-adapter.
@SimoTod I just gave it a test in my project and it seems to be working correctly. I'm pretty sure the first test I added, https://github.com/SimoTod/alpine-turbolinks-adapter/blob/master/tests/integration/cloak.spec.js#L4, failed before I added the defer loading so if that's passing I think it covers the initial load case that I added the deferloading for.
I ran a quick test in my project with the script itself bother deferred and and not deferred and it worked in both cases for me.
from alpine-turbo-drive-adapter.
Yeah, your PR works in normal cases but in a project using Livewire they both tries to defer alpine using an event but they don't work well together. Ready state interactive should trigger before domready (so Alpine starting) but after the whole dom has been downloaded. Hopefully it will serve our purposes. I need to do some cross browsing tests over the weekend but I'm glad it looks like it could work. The workaround for livewire user for now is to remove the defer attributes from the adapter library.
from alpine-turbo-drive-adapter.
@zepfietje can you confirm it fixes your issue as well?
Seems to fix my issue!
from alpine-turbo-drive-adapter.
Closing as it was fixed in 0.2.0
from alpine-turbo-drive-adapter.
The merge #13 breaks the responsive x-cloak
functionality. As I see it, the x-cloak="mobile"
would be replaced by x-cloak=""
. Is there a way to opt out on this and write my own custom solution?
from alpine-turbo-drive-adapter.
Thanks @heyfadii, can you send me an example please? X-cloak is a boolean attribute and it does not support the syntax you posted if i remember correctly so you cannot decide to apply it conditionally based on a value.
We can keep the value but I'm not sure about the benefits and it wouldn't be standard alpine.
from alpine-turbo-drive-adapter.
Sure. What I want to do is hide something conditionally, depending on the resolution. I found this example: https://dev.to/hus_hmd/alpine-js-responsive-x-cloak-3blp
from alpine-turbo-drive-adapter.
@heyfadii Can you confirm that https://cdn.jsdelivr.net/gh/simotod/alpine-turbolinks-adapter@feature/x-cloak-value/dist/alpine-turbo-drive-adapter.min.js does what you need? Thanks.
from alpine-turbo-drive-adapter.
@SimoTod Sorry for the delay. Yes works! when can this be expected to be merged? Thank you for being fast as lightning!
from alpine-turbo-drive-adapter.
Related Issues (20)
- Missing readme
- Disappearing template loop items HOT 7
- x-for elements disappearing after Livewire rerender HOT 11
- v0.2.0 - x-for loop items disappear HOT 3
- Use Alpine.clone to avoid existing component being reset when navigating using the history. HOT 7
- IE 11 support HOT 4
- Turbo Drive compatibility HOT 12
- Issue with data-turbolinks-permanent combined with x-cloak HOT 3
- fail when render status: :unprocessable_entity HOT 32
- Figure out if the plugin is testable somehow HOT 9
- JS file always runs after the turbo:load event, causing x-data to throw a ReferenceError on first navigation HOT 6
- Turbo stream - not initialising HOT 5
- after fired turbo:render x-data not exists HOT 12
- Currently Breaks with Alpine.js & Livewire HOT 14
- Add github action to publish the library on npm when a new version is tagged. HOT 1
- Alpine v3 compatability HOT 36
- How can I keep the cdn link updated HOT 5
- Necessary if caching disabled? HOT 1
- Add npm instructions HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alpine-turbo-drive-adapter.