Caching issue in Modx 3

Hi All,

I have a slight problem with Modx3 wheras pages that are set to not cache are caching.

So for example I have a template that allows people to submit a form and the system displays the results in a table, they can do as many as they want and the existing table just adds another row each time, this all works and is called in with the following:

[[!+Th.DC:!empty=`[[!Th_DebtCalculatorEntries]]`]]

The content of Th_DebtCalculatorEntries is:

<?php
if (count($_SESSION['Th']['DC']['invoices']) > 0) {
	$modx->setPlaceholder('Th.DC', 'true');

	$output = '<table class="table table-striped custom-table">';
	$output .= '<tr>';
	$output .= '<td>Raised</td>';
	$output .= '<td>Due</td>';
	$output .= '<td width="20%" class="text-right">Amount</td>';
	// $output .= '<td width="20%" class="text-right">Interest</td>';
	// $output .= '<td width="20%" class="text-right">Compensation</td>';
	$output .= '<td width="10%" class="text-right">Remove</td>';
	$output .= '</tr>';

	foreach ($_SESSION['Th']['DC']['invoices'] as $k => $invoice) {
		$output .= '<tr>';
		$output .= '<td>'.date('jS F Y', strtotime($invoice['raiseddate'])).'</td>';
		$output .= '<td>'.date('jS F Y', strtotime($invoice['duedate'])).'</td>';
		$output .= '<td class="text-right">&pound;'.sprintf('%.2f', $invoice['balance']).'</td>';
		// $output .= '<td class="text-right">&pound;'.sprintf('%.2f', $invoice['interest']).'</td>';
		// $output .= '<td class="text-right">&pound;'.sprintf('%.2f', $invoice['compensation']).'</td>';
		$output .= '<td class="text-right"><a href="'.$_SERVER['REQUEST_URI'].'?remove='.$k.'">&times;</a></td>';
		$output .= '</tr>';
	}
	$output .= '</table>';
}

return $output;

Each row has a remove icon, when clicked it removes the row and recalculates the entries, this works well when removing 1 entry, but after that none are removed unless I cleare the cache on the system then all the ones clicked are removed.

In the latest 2 version everything works as expected but in 3.3 I have the above issue, could anyone advise ?

Just to add the 2 version is in the root and the 3.3 version is on /deploy ( so exactly the same server )

Thanks

Hey @paulp

I’m not sure what might cause that within the MODX environment - but might be worth checking if the opcache PHP extension or some other third-party caching system is enabled on your hosting account - especially if you’ve recently changed PHP version.

The page’s cacheable checkbox is unchecked, right?

Have you verified that the pages have cache files in the core/cache/resource/web/resources directory (the filenames will begin with the id of the resource).

opcache is on but the 2 instences of Modx are on the same account, 2 works but 3 doesn’t, both have opcache set to on

No the Page doesnt have cache files which is really strange because clearing the cache removes the element’s once they have been clicked and the page is reloaded, before clearing the cache reloading does nothing.

As I say, the first row clears as it should, the others dont.

Having looked into it further I dont think it is a caching issue, I’ve tested it in the following:

On latest 2 branch

Firefox: Works as expected
Chrome: Works as expected
Safari: Works as expected

On 3.0.3

Firefox: Doesn’t work
Chrome: Works as expected
Safari: Doesnt work

Theres nothing in any of the error logs and dev tools shows the same for both versions

Thanks for your time both

Did you try turning it off momentarily just in case? I’ve had some really weird results with opcache enabled in the past. No harm in trying :slightly_smiling_face:

Also - I take it there’s no AJAX involved here? The items in the table are just updated via a page reload?

Iv’e requested opcache to be disabled so just waiting for that to happen, no AJAX involved just the page reload and then it calculates the entries again.

:+1: At least you’ll have ruled it out if that’s not the culprit. Let us know how you get on.

Disabled opcache and restarted apache, cleared all caches and retried but with the same results I’m afraid :thinking:

I did a test with MODX 3.0.3 using the Firefox browser and this slightly adapted snippet code, but I couldn’t reproduce any issues and everything worked as expected.

<?php
// Snippet call = [[!SnippetName]]

if (!isset($_SESSION['invoices'])){
    $_SESSION['invoices'] = [];
}

if (isset($_GET['remove'])) {
    // Remove index from array
    $i = intval($_GET['remove']);
    array_splice($_SESSION['invoices'], $i, 1);
}

if (isset($_GET['add'])) {
    // Add a random value
    $_SESSION['invoices'][] = ['rnd' => substr(str_shuffle(MD5(microtime())), 0, 10)];
}

$output = '<a href="' . $modx->makeUrl($modx->resource->id, '', ['add' => '1']) . '">Add item</a>';

if (count($_SESSION['invoices']) > 0) {
	$output .= '<table class="table table-striped custom-table">';
	$output .= '<tr>';
	$output .= '<td>Rnd</td>';
	$output .= '<td>Remove</td>';
	$output .= '</tr>';

	foreach ($_SESSION['invoices'] as $k => $invoice) {
		$output .= '<tr>';
		$output .= '<td>'. $invoice['rnd'] . '</td>';
		$output .= '<td><a href="' . $modx->makeUrl($modx->resource->id, '', ['remove' => $k]) . '">&times;</a></td>';
		$output .= '</tr>';
	}
	$output .= '</table>';
}

return $output;

Thanks Harry, afraid I can’t edit the code as i’m not great with PHP for one, but this is quite a complex system that is using several snippets that all tie into an API for the calculator that works out interest ect.

It’s just very strange that it works without issue in the latest 2 but not in 3.0.3 for a 2 browsers.

If you use dev tools to disable the cache in Firefox [F12 / Network / Disable Cache] - does the problem still happen in Firefox?

Yes, exactly the same, they just wont go until I cleare the Modx cache