swup / livewire-plugin

A swup plugin for integrating Laravel Livewire ♻️
https://swup.js.org/plugins/livewire-plugin
MIT License
4 stars 2 forks source link

[Bug]: window.Livewire.restart() is not a function #2

Closed james2doyle closed 4 days ago

james2doyle commented 5 days ago

What did you expect? 🧐

Undefined methods should not be called

What actually happened? πŸ˜΅β€πŸ’«

image This function being called does not exist

Swup and plugin versions πŸ“

What browsers are you seeing the problem on? 🧭

No response

Relevant log output πŸ€“

Hooks.ts:440 Error in hook 'content:replace': TypeError: window.Livewire.restart is not a function
    at i.refreshLivewireComponents (index.js:47:19)
    at index.ts:38:27
    at new Promise (<anonymous>)
    at f (index.ts:37:13)
    at S.run (Hooks.ts:434:26)
    at S.call (Hooks.ts:358:14)
    at async j.T [as renderPage] (renderPage.ts:21:23)
    at async navigate.ts:196:6
run @ Hooks.ts:440
await in run (async)
call @ Hooks.ts:358
await in call (async)
T @ renderPage.ts:25
(anonymous) @ navigate.ts:196

URL to minimal reproduction πŸ”—

This would require a hosted Laravel livewire app

Checked all these? πŸ“š

james2doyle commented 5 days ago

Looks like the current solution to restarting all the components is:

Livewire.all().map((component) => {
    component.call('render'); // or 'restart'
});

https://github.com/livewire/livewire/issues/2334#issuecomment-759901196

If this sounds right, I can make a PR with the changes

james2doyle commented 5 days ago
if (typeof window.Livewire.restart !== 'function') {
    console.info('Patching restart function...');
    window.Livewire.restart = () =>
        Livewire.all().map((component) => {
            // not always defined, depending on the component
            if (typeof component.call === 'function') {
                return component.call('restart');
            }
            // will be called when the component is mounted again
            if (typeof component.$wire?.$refresh === 'function') {
                component.$wire.$refresh();
            }
        });
}

I wrote this function to patch this now removed restart function. Seems to be working, and I see the endpoints being called when the navigation occurs

daun commented 4 days ago

Yes, a PR would be fantastic! It would probably also be nice to have a conditional that supports both versions to avoid releasing a major version of the plugin.

james2doyle commented 4 days ago

@daun Ok cool. My code above has a check for the original function and then just replaces it. I think in the real code it would just be an if/else instead and then call each one natively.

How does that sound?

daun commented 4 days ago

Sounds perfect!

hirasso commented 4 days ago

Closed by #3