Contentblocks stuck on rebuild content

I just want to leave here a solution for a problem I had on different sites with contentblocks.

The problem is when I click on Rebuild Content it get stuck in one point of the process but there is not errors of any kind on the hosting or ModX.

The solution is to do the process “manually” using the following code, any ideas or thoughts to improve this are welcome.

Create a Snnippet called:
rebuildContentBlocks

Code:

<?php
// Output a container for progress messages
echo '<div id="contentblocks-rebuild-progress" style="font-family: Arial, sans-serif; padding: 20px; background: #f9f9f9; border: 1px solid #ddd;">';
echo '<h2>ContentBlocks Rebuild Progress</h2>';
echo '<ul id="progress-list" style="list-style: none; padding: 0;">';
echo '</ul>';
echo '</div>';

// Get all resources with ContentBlocks data
$resources = $modx->getCollection('modResource');
$totalResources = count($resources);
$currentResource = 0;

foreach ($resources as $resource) {
    $currentResource++;
    $resourceName = $resource->get('pagetitle');
    $resourceId = $resource->get('id');

    // Force save to trigger ContentBlocks rebuild
    $resource->save();

    // Output progress message
    echo "<script>
          var progressList = document.getElementById('progress-list');
          var newItem = document.createElement('li');
          newItem.innerHTML = 'Processing Resource #$currentResource of $totalResources: <strong>$resourceName</strong> (ID: $resourceId)';
          progressList.appendChild(newItem);
          </script>";

    // Flush the output buffer to display progress in real-time
    ob_flush();
    flush();
}

// Final completion message
echo "<script>
      var progressList = document.getElementById('progress-list');
      var newItem = document.createElement('li');
      newItem.innerHTML = '<strong>Rebuild Complete!</strong> All $totalResources resources have been processed.';
      progressList.appendChild(newItem);
      </script>";

return '';

Just call the Snippet into a normal resource, that is it.

Enjoy! :grimacing: :+1:

Just calling $resource->save() does not trigger a ContentBlocks rebuild, so I don’t think this works the way you think it does.

If the rebuild gets stuck, that would mean a fatal error of sorts. If it consistently happens at the same time in the rebuild, that will point you to the resource that causes it. You may see something like “rebuilding resource #123” before it freezes - that means the problem is the next resource, e.g. #124.

Sometimes editing the resource that causes it and saving it manually can correct that. Sometimes there’s some custom code that breaks in the rebuild.

Hello @markh, it works for me to display new fields content in the front end, something that didn’t do “Rebuilding Content”.

I have the same stuck problem on different sites and that is creating problems when blocks needs to be updated.

I think a force rebuild option should be implemented or a guide to do it manually at least in the necessary resource I can’t find any information about that online.

Cheers!

What you’re running into that stops the rebuild from continuing is likely fatal errors. In my experience, I’d say either there’s a snippet on the page that tries to access something that doesn’t exist while rebuilding, or something gets in a loop until memory is exhausted.

We’re always happy to help to identify and fix such an issue, but there’s not typically a way to “force” through a fatal error. If it was something that could be ignored, it would not be a fatal error.

I would encourage treating these as different problems (unless they share the same exact setups) and emailing our support with the error from your PHP log so we can help with them.