Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

pages always come from cache, regardless of backend login or frontend preview #8249

Closed
fritzmg opened this issue Mar 3, 2016 · 6 comments
Closed
Assignees
Labels
Milestone

Comments

@fritzmg
Copy link
Contributor

fritzmg commented Mar 3, 2016

Problem description

Even if you log into the backend and/or use the frontend preview, pages are always loaded from the cache, when present, under certain circumstances. (More apt description: pages are not loaded from the cache under certain circumstances, when logged into the backend - see below.)

Reproduction

  1. add var_dump($_SESSION['DISABLE_CACHE']); here (or echo 'foo'; after the condition, or whatever you like)
  2. create a page named "Foo" in your site structure
  3. enable caching on this page
  4. log out of the backend
  5. open the page "Foo" in the frontend (this puts it into the cache)
  6. restart your browser
  7. log into the backend again
  8. open the page "Foo" directly in the Frontend. Do not open any other page in the Frontend before that.

The page "Foo" will always come from the cache, no matter how many times you reload.

If you want to check the same thing with the frontend preview, you need to do the following:

  1. add var_dump($_SESSION['DISABLE_CACHE']); here (or echo 'foo'; after the condition, or whatever you like)
  2. create one or more pages in your site structure
  3. enable caching on the website root
  4. log out of the backend
  5. open the website in the frontend and open all pages (so that all pages reside in the page cache)
  6. restart your browser
  7. log into the backend again
  8. open any page in the frontend or use the preview mode - pages will always come from the cache

Note: You need at least Contao 3.5.0 (I think) for the second reproduction steps to work, because in older versions the index page might not get properly cached.

Cause

I have outlined the cause already here in german: #8233 (comment)

Usually, pages won't be loaded from the cache under a number of conditions: FrontendIndex.php#L295

// Build the page if a user is (potentially) logged in or there is POST data
if (!empty($_POST) || \Input::cookie('FE_USER_AUTH') || \Input::cookie('FE_AUTO_LOGIN') || $_SESSION['DISABLE_CACHE'] || isset($_SESSION['LOGIN_ERROR']) || \Config::get('debugMode'))
{
    return;
}

One of these conditions is the session variable

$_SESSION['DISABLE_CACHE']

This variable will only be set to true in the function Frontend::getLoginStatus

protected function getLoginStatus($strCookie)

and this function is only called by FrontendIndex::__construct, but only AFTER (!)

$this->outputFromCache();

Thus, if you never, ever visit a page in the frontend which is not already present in the page cache, the variable $_SESSION['DISABLE_CACHE'] will never be set to true and thus you will always receive the page from the cache, even when logged into the backend.

@fritzmg fritzmg changed the title pages always come from cache, even with backend login and with frontend preview pages always comes from cache, regardless of backend login or frontend preview Mar 3, 2016
@fritzmg fritzmg changed the title pages always comes from cache, regardless of backend login or frontend preview pages always come from cache, regardless of backend login or frontend preview Mar 3, 2016
@leofeyer
Copy link
Member

leofeyer commented Mar 8, 2016

Excellent summary of a well-known problem: #3184

@fritzmg
Copy link
Contributor Author

fritzmg commented Mar 9, 2016

I tried to fix it myself, by setting

$_SESSION['DISABLE_CACHE'] = true;

right when the user logs into the backend. However, it did not work due to the Contao\Session singleton overwriting the session data (and splitting the session data for frontend and backend).

Thus, the easiest fix is simply adding

\Input::cookie('BE_USER_AUTH')

to these conditions.

This works as expected: whenever you are logged into the backend, you never get a page from the page cache. As soon as you are logged out of the backend, pages come from the cache again (when present, otherwise they are generated for the cache first).

With this you can also remove $_SESSION['DISABLE_CACHE'] from the conditions and also remove it here, since it will be redundant, as far as I can see.

@fritzmg
Copy link
Contributor Author

fritzmg commented Mar 14, 2016

@leofeyer

Excellent summary of a well-known problem: #3184

That issues does not seem to be related. Georg was originally talking about discrepancies that can happen, if pages comes from the cache and others do not. Like main menus not displaying the complete site structure, if a page is loaded from the cache (after a new pages had been added to the site structure). This particualy problem can be solved easily with insert tags (or in other more complicated ways).

This issue is about something else - this issue is about a page always coming from the page cache, even if you are logged into the backend and even if you are using the frontend preview.

The intended behavior is that pages are not served from the page cache, if you are logged into the backend, which is what the $_SESSION['DISABLE_CACHE'] originally is for (it is set, if the backend login status is true) - however, as I have pointed out here, the present code is flawed.

@fritzmg
Copy link
Contributor Author

fritzmg commented Mar 22, 2016

Is there any reason not to add

\Input::cookie('BE_USER_AUTH')

to FrontendIndex.php#L313 and remove the $_SESSION['DISABLE_CACHE'] variable entirely?

@leofeyer
Copy link
Member

As discussed in Mumble on June 16th, we want to add \Input::cookie('BE_USER_AUTH').

@leofeyer
Copy link
Member

Added in c1d401a.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants