craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

Server Error on Live Preview #158

Closed mihob closed 2 years ago

mihob commented 2 years ago

Description

If x-craft-live-preview parameter is present in request and cache is present in element-api config for endpoint, the element api request results in an server error with the following log message:

[error][craft\elementapi\controllers\DefaultController::actionIndex] Error resolving Element API endpoint: Setting unknown property: craft\elementapi\resources\ElementResource::cache

This in src/DefaultController.php seems to be the problem:

// Before anything else, check the cache
if (!($this->request->getIsPreview() || $this->request->getIsLivePreview())) {
    $cache = ArrayHelper::remove($config, 'cache', true);
} else {
    $cache = false;
}

cache should probably always be removed from $config. The error was introduced in commit fb92fef

Steps to reproduce

  1. cache must be present in element-api config for endpoint
  2. Call element api endpoint with x-craft-live-preview parameter present in url

Additional info

FreekVR commented 2 years ago

We were running into the same issue unfortunately, I've created a pull request with a proposed solution @angrybrad @brandonkelly , would be great if this could be fixed soon, our client uses the preview functionality a lot :)

viooolDN commented 2 years ago

Hey element api team, could you take a look at this issue for us? Our client is using this preview function and would love to have it working again. Many thanks at forehand!

FreekVR commented 2 years ago

Hi all,

Was wondering if this plugin is actively maintained still, if so, would be great to have this fix included -- otherwise we can switch to a fork without much worry for a while and look into switching to GraphQL fully.

brandonkelly commented 2 years ago

Just released Element API 2.8.5 with the fix for this, provided by #159. Thanks a lot @FreekVR!