craftcms / gatsby-source-craft

Gatsby source plugin for Craft CMS.
MIT License
54 stars 13 forks source link

Moving elements in structures causes partial updates with incremental builds #48

Closed stephenfrank closed 3 years ago

stephenfrank commented 3 years ago

Description

I have a structure section used to create a sidebar and page hierarchy. The sidebar hierarchy doesn't update or gets out of order when moving nodes and doing incremental builds.

I've determined that moving a structure item does not update the Element's dateUpdated so it is not picked up in the nodesUpdatedSince list.

This is compounded by the fact that if I then edit one of the structure entries, only the edited entry will feed through to nodesUpdatedSince and I end up with invalid lft/rgt values in the structure in the Gatsby internal storage.

Here's my solution that I've used to get around this issue... you may have a different take on the issue or a different direction to take it so it's just here for reference:

https://github.com/flowsa/gatsby-helper/commit/93e94250b529a07f7082a67bf81d151ccbea8b69

    public function getUpdatedNodesSinceTimeStamp(string $timestamp): array
    {
        $structureUpdates = (new Query())
            ->select(['elementId', 'structureId'])
            ->from([CraftTable::STRUCTUREELEMENTS])
            ->andWhere(['>', 'dateUpdated', Db::prepareDateForDb($timestamp)])
            ->pairs();

        return (new Query())
            ->select(['id', 'type'])
            ->from([CraftTable::ELEMENTS])
            ->where(['dateDeleted' => null])
            ->andWhere(['not in', 'type', $this->_getIgnoredTypes()])
            ->andWhere(
                ['or',
                    ['>', 'dateUpdated', Db::prepareDateForDb($timestamp)],
                    ['IN', 'id', array_keys($structureUpdates)]
                ]
            )
            ->andWhere(['revisionId' => null])
            ->andWhere(['draftId' => null])
            ->pairs();
    }
andris-sevcenko commented 3 years ago

Thanks for bringing this up and providing a possible fix. We'll talk this over internally and figure if this is something that Craft should be doing (updating elements that are being moved) or should we proceed with your proposed fix.

andris-sevcenko commented 3 years ago

Sorry for taking the scenic route on this one! I implemented your fix in the Gatsby Helper plugin and that will be a part of the next release.

andris-sevcenko commented 3 years ago

Reference: craftcms/gatsby-helper@9b32c819ab80c03873f28acd9608b0b56c996fc4