craftcms / element-api

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

Preview and Cache causes draft to go 'live' immediately #143

Closed Zae closed 2 years ago

Zae commented 3 years ago

Description

When using the cache parameter (which is now default, since 2.7.0), using preview in the CMS causes the API to always return the preview version.

I believe this is because element-api always caches the request and does not check if the request is a preview request. In subsequent requests the cache is found and simply returned.

In 2.6.0 the preview was broken, but because the published version kept being served instead of the draft, now with the changes in 2.7.0 the cache is cleared when a draft is saved which causes the draft to be cached.

Steps to reproduce

  1. Enable cache in element-api (default)
  2. Supply the request with the correct X-Craft-Token
  3. Element-api fills cache with current draft
  4. Element-api serves cached draft version when requested without preview token

Additional info

Zae commented 3 years ago

I'm having some trouble validating if this actually works on our testing server right now, also according to brandon only checking for token is not correct, so maybe isPreview is correct, I just need to add the preview query param to the api request as well from my frontend to get it to work?

Zae commented 3 years ago

OK, I changed to start usingisPreview again, people just need to remember to pass the x-craft-live-preview along.

Works correctly on our test server now :)

brandonkelly commented 2 years ago

Just released Element API 2.8.0, which no longer caches responses for Live Preview requests.