silverstripe / silverstripe-staticpublishqueue

This module allows you to build static HTML caches of every page (for increased security and performance)
BSD 3-Clause "New" or "Revised" License
46 stars 57 forks source link

None versioned DataObject does not trigger objectsToUpdate #197

Open Oliver996 opened 3 months ago

Oliver996 commented 3 months ago

Module version(s) affected

^6.2

Description

I did add everything like documented https://github.com/silverstripe/silverstripe-staticpublishqueue/blob/6/docs/en/basic_configuration.md#engines to the dataobject to trigger republish of the cache. When the dataobject is versioned the objectsToUpdate is triggered when I unpublish or/and archive the dataobject. But in a dataobject that is not versioned it only triggers on save, but not on delete. I could not get objectsToUpdate to trigger on delete. Is this a bug, not meant to work with not versioned dataobject or is there some documentation missing for not versioned dataobject? I did test it on my silverstripe 5.2 and also on a clean silverstripe 5.2 with only the staticpublishqueue both had the same problem. It also would be nice to add that you need the canPublish function for versioned dataobject in the documentation.

How to reproduce

Clean 5.2 Silverstripe with staticpublishqueue then added dataobject like https://github.com/silverstripe/silverstripe-staticpublishqueue/blob/6/docs/en/basic_configuration.md#engines

Test.php with objectsToUpdate and objectsToDelete (both simply for testing)

<?php

use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\DataObject;
use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger;

class Test extends DataObject implements StaticPublishingTrigger {

    private static $db = [
        'Name' => 'Text'
    ];

    private static $table_name = 'Test';

    function getCMSFields() {
        $fields = FieldList::create(TabSet::create('Root'));

        $fields->addFieldToTab('Root.Main',
            TextField::create('Name', _t('Downloads.NAME', 'Name'))
        );

        $this->extend('updateCMSFields', $fields);

        return $fields;
    }

    public function objectsToUpdate($context) {
        $page = Page::get()->First();
        $array[] = $page;

        return $array; 
    }

    public function objectsToDelete($context) {
        $page = Page::get()->First();
        $array[] = $page;

        return $array;
    }

}

yml

Test:
  extensions:
    - SilverStripe\StaticPublishQueue\Extension\Engine\SiteTreePublishingEngine

After saving, it creates both jobs, but after delete it creates no jobs for update/delete cache.

When adding versioned to the dataobject (add to Test class)

private static $extensions = [
    Versioned::class,
];

public function canPublish($member = null){
    return true;
}

Then it works with saving, publishing, unpublishing and archiving. It always creates both jobs

Possible Solution

No response

Additional Context

No response

Validations

andrewandante commented 1 week ago

Hi - it looks like the only hooks in the SiteTreePublishingEngine are related to publishing, so yes, I think this module currently doesn't work with non-versioned objects.

For your case, you should be able to work around it by replicating the onBefore/onAfter changes, and applying them to write rather than publish (and delete rather than unpublish).

I think there's a potential PR where the onBefore/onAfter methods are called with an "if this thing is not versioned" check? Potentially worth experimenting with