mikebronner / laravel-caffeine

Keeping Your Laravel Forms Awake.
https://genealabs.com/docs/laravel-caffeine/
MIT License
921 stars 84 forks source link

All my forms still expire #77

Closed warmwhisky closed 6 years ago

warmwhisky commented 6 years ago

I have set my 'drip-interval' => 59000 and my sessions 'lifetime' => env('SESSION_LIFETIME', 1), but my form still expires. I have a search box on every page.

How can I properly test this. I am not that experienced with phpunit so I do not know how to test using your tests.

Expected Behavior

Not expire

Acutal Behavior

Expire

Environment

Stack Trace

No stack trace. I don't know how to debug this

MarcosBL commented 6 years ago

+1, been using this package up to last week with no issues, today, in a new clean install, it doesn't inject the JS in the website, just doesn't work

Downgrading composer to "genealabs/laravel-caffeine": "^0.5.0", it works fine again

mikebronner commented 6 years ago

Thanks for reporting! Have either of you published the configuration file prior to updating to 0.6? The conf settings have changed, which might account for this problem. I will take a look and see what else might be affecting this. @MarcosBL can you provide all the version info as well (os, PHP, Laravel, package) to help give a better understanding? Thanks! Also, is there a CSRF token meta tag on the page you want to be caffeinated?

warmwhisky commented 6 years ago

Mine was a fresh 5.5 install yesterday. I had the issue before generating the config file. I only generated it so I could change the milliseconds for testing, but got the same result.

mikebronner commented 6 years ago

Try setting the drip interval to 5000 for testing, instead of 59000. Let me know how that goes.

MarcosBL commented 6 years ago

Same here, fresh 5.5 Laravel install, issue before publishing, tried to publish to debug, saw the new format. I have a meta with the csrf token, had no forms, but inyected an empty one in the layout just to be sure: same thing, no JS inyected at all before , no matter wich interval in config file.

As soon as I downgrade to 0.5.0, everything starts working fine

Sys info: Linux Mint 18.3 / Laravel 5.5 / PHP 7.0.22 / Apache 2.4.18

mikebronner commented 6 years ago

@MarcosBL can you post the page code (Blade?) that you are testing on? I want to see if I can replicate your exact situation. Thanks :)

mikebronner commented 6 years ago

Is your page continuously refreshing, instead of dripping?

mikebronner commented 6 years ago

I have tested it on my end in a brand new blank laravel project, and it is continuously dripping. Here is my blade file that I used to test:

@extends('layouts.app')

@section('content')
    <form method="post">
        {{ csrf_field() }}
        <input type="hidden" name="success" value="true">
        <input type="submit" value="Test Form Submit">
    </form>
@endsection

It is important that your session lifetime is greater than your drip interval, otherwise it wont work.

mikebronner commented 6 years ago

Also, are you still registering the package in the providers array of your app config? If so, it needs to be removed in L5.5 and up, as it will auto-register.

If you could link to or put up a repo where this is not working, that would really help.

warmwhisky commented 6 years ago

I just set 'drip-interval' => 5000 and 'lifetime' => env('SESSION_LIFETIME', 1) and it appears to be working now. I'm not sure if Chrome was caching anything?

Can you tell me how I can see this in console? Should it be seen in the network tab? Or is there any other way to monitor it firing?

mikebronner commented 6 years ago

I would recommend keeping the session at the default 120 (or something larger than 5). setting it to 1 is not a realistic (only useful for testing) value and can result in strange behavior.

To inspect the drips, I look at the resources tab. You should see a new drip request every 5 seconds with your current setup.

warmwhisky commented 6 years ago

No I wont be using those settings. I was just testing.

I am sorry but had a hectic day yesterday. I can no longer confirm I have it working. I am uninstalling to test without now.

I cannot see the resources tab in chrome. Where is that hidden? I thought it may be the network tab?

warmwhisky commented 6 years ago

So sorry for my mistake yesterday. It's still not working for me. I tried to make it expire, but for some reason I couldn't. I don't know if laravel or chrome was caching anything or what was going on, but today after uninstalling, I finally for it to expire (very confusing) After a fresh install of caffeine just now, I confirm it is not working for me.

I am looking in the network tab for the drip every 5 seconds but am not seeing it. Where exactly do I see the drip and what does it say? is it a js file? an url? I have no idea what to look for?

warmwhisky commented 6 years ago

Here are the other packages installed in Laravel. Maybe there is a conflict?

"require": { "php": ">=7.0.0", "barryvdh/laravel-debugbar": "^3.1", "barryvdh/laravel-ide-helper": "^2.4", "barryvdh/laravel-snappy": "^0.4.0", "fideloper/proxy": "~3.3", "genealabs/laravel-caffeine": "^0.6.2", "gloudemans/shoppingcart": "^2.4", "guzzlehttp/guzzle": "^6.3", "h4cc/wkhtmltopdf-amd64": "^0.12.3", "intervention/image": "^2.4", "laravel/framework": "5.5.*", "laravel/tinker": "~1.0", "maatwebsite/excel": "~2.1.0", "rydurham/sentinel": "^2.7", "sentry/sentry-laravel": "^0.8.0", "snowfire/beautymail": "^1.0", "spatie/laravel-backup": "^5.1" },

warmwhisky commented 6 years ago

Im having difficulties debugging this. Now when I set my session value back to 120 it still expires within the minute. Is this a chrome issue? I've noticed lately that I need to clear my cache more often when editing CSS. And even sometimes clearing the cache does not work, I have open the css file in the browser and then click refresh for the new css rules to be seen.

mikebronner commented 6 years ago

Are you checking for the drip on pages that have form? It will only drip if there is a form on the page, and the csrf-token is also on the page (either as meta tag or hidden field).

Sorry I might be wrong about the Resource tab (thats in Safari).

MarcosBL commented 6 years ago

Sorry for the late response, made a fast debug and found shouldRegisterGlobalMiddleware function returning false in my app.

All checks pass, but php_sapi_name (apache2handler in my situation) and app('env') (local in my situation. I think that's the problem, the logic in that function, that didn't exists in the previous release. Does this help ?

My session lifetime is the default 120, and muy config is:

return [
    'drip-interval' => 300000,
    'domain' => null,
    'route' => 'genealabs/laravel-caffeine/drip',
    'outdated-drip-check-interval' => 2000,
    'use-route-middleware' => false,
];

A simple return true; in that line injects the JS correctly in my template, so I assume the problem is on those checks. Injected code:


<script>
    let lastCheck = new Date();
    const caffeineSendDrip = function () {
        const 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', '/genealabs/laravel-caffeine/drip');
        ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        ajax.send();
    }

    setInterval(function () {
        caffeineSendDrip();
    }, 300000);

    if (2000 > 0) {
        setInterval(function () {
            if (new Date() - lastCheck >= 7082000) {
                location.reload(true);
            }
        }, 2000);
    }
</script>

Just my 2 cents

mikebronner commented 6 years ago

Ah!!!!! That's the missing link, I completely forgot to handle Apache. Could you try version 0.6.3 to see if the fix works?

Thanks!

MarcosBL commented 6 years ago

Composer-updated and working fine, first try :+1:

Thanks !

mikebronner commented 6 years ago

@warmwhisky did this fix it for you as well?

warmwhisky commented 6 years ago

Sorry I was busy... Yep all sorted now. I can see the drip!

Thanks so much!

mikebronner commented 6 years ago

Awesome, sorry I forgot about Apache use-case. Thanks for sticking with the debugging and helping get it sorted out :)