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

Accumulation of drip calls with wire:navigate #152

Open
mauritskorse opened this issue Apr 17, 2024 · 1 comment
Open

Accumulation of drip calls with wire:navigate #152

mauritskorse opened this issue Apr 17, 2024 · 1 comment

Comments

@mauritskorse
Copy link

mauritskorse commented Apr 17, 2024

Expected Behavior

Regular drip calls to the server set by the config.

Actual Behavior

When using livewire with wire:navigate the number of drip calls start to increase after having visited a couple of pages. The script is apparently initiated again after navigating.

Environment

  • PHP Version: 8.2
  • Laravel Version: 10.x
  • LaravelCaffeine Version: 10

Stack Trace

@mauritskorse
Copy link
Author

mauritskorse commented Apr 17, 2024

It would be nice if the script.bade.php could be published to /resources/view/vendor to customize the javascript behaviour.
For now I have adjusted the script to this:

    window.timers = window.timers || [];

    function startInterval(key, callback, delay) {
        // If an interval with the same key already exists, clear it
        if (window.timers[key]) {
            clearInterval(window.timers[key]);
        }
        // Start a new interval and store its ID in the timers object
        window.timers[key] = setInterval(callback, delay);
    }

    var lastCheck = new Date();

    var caffeineSendDrip = function () {
        var ajax = window.XMLHttpRequest
            ? new XMLHttpRequest
            : new ActiveXObject('Microsoft.XMLHTTP');

        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4 && ajax.status === 204) {
                lastCheck = new Date();
            }
        };

        ajax.open('GET', '{{ $url }}');
        ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        ajax.send();
    };

    var caffeineReload = function () {
        if (new Date() - lastCheck >= {{ $ageCheckInterval + $ageThreshold }}) {
            setTimeout(function () {
                location.reload(true);
            },  Math.max(0, {{ $ageCheckInterval }} - 500) )
        }
    };

    startInterval('dripTimer', caffeineSendDrip, {{ $interval }});

    if ({{ $ageCheckInterval }} > 0) {
        startInterval('ageTimer', caffeineReload, {{ $ageCheckInterval }});
    }

see master...mauritskorse:laravel-caffeine:fix/livewire-navigate

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

No branches or pull requests

1 participant