JosephSilber / page-cache

Caches responses as static files on disk for lightning fast page loads.
MIT License
1.21k stars 118 forks source link

Logged in users #14

Closed mbaierl closed 6 years ago

mbaierl commented 6 years ago

Hi! Is there any way of caching pages only if there is no user logged in? Or cache it for each user who is logged in? Reason being that there might be a custom header with the username....

Any advice on how to solve that?

JosephSilber commented 6 years ago

cache it for each user who is logged in?

Pages are cached to disk as static files. The file's name is the URL. On subsequent requests, PHP isn't even booted up, so there's no way to even know who the logged-in user is.

Is there any way of caching pages only if there is no user logged in?

You can extend the base middleware (as outlined in the docs), and check for that in your shouldCache method:

protected function shouldCache(Request $request, Response $response)
{
    if ($request->user()) {
        return false;
    }

    return parent::shouldCache($request, $response);
}

...but I don't think this will help you. This won't cache pages for logged-in users, but when a logged-in user visits a page that was previously cached, they won't see their username.

At the end of the day, you have to determine whether the page is a static page or not. This package is for caching static pages, not dynamic ones.

Any advice on how to solve that?

Depends on your app. For example, you may be able to add the user's username on the client side via JS. There's no way to know such things without having intimate knowledge of your app.

leobeal commented 6 years ago

@mbaierl if all you want to do is show the name in the header, fetch that using an ajax call.