element-api
element-api copied to clipboard
Endpoint Caching While in Dev Mode
Description
I set up a simple endpoint that is outputting entry data as JSON, everything seems to be working correctly. The only problem is when I make a change to the endpoint in config/element-api.php
and refresh the endpoint page, the changes are not automatically updated even though I have devMode
enabled in my environment configuration. I've confirmed it isn't browser-side caching as I have tried manually clearing my browser cache. I have to manually go to Utilities > Caches > Invalidate Data Caches > Element API Responses in the control panel and clear the cache for the changes to show up.
Obviously in a development environment this isn't ideal, and Element API shouldn't be caching in Dev Mode.
Here's my code:
<?php
use craft\elements\Entry;
use craft\helpers\UrlHelper;
return [
'endpoints' => [
'recipes.json' => function() {
return [
'elementType' => Entry::class,
'elementsPerPage' => 12,
'criteria' => [
'section' => 'recipes'
],
'transformer' => function(Entry $entry) {
return [
'id' => $entry->id,
'title' => $entry->title,
'uri' => $entry->uri,
'recipeImg' => $entry->recipeImg->one()->url
];
},
'pretty' => true
];
}
]
];
Steps to reproduce
- Enable
devMode
in environment - Set up a JSON endpoint in
config/element-api.php
- Load the endpoint in the browser
- Edit the endpoint
- Refresh the endpoint in the browser
Additional info
- Craft version: 4.5.7
- PHP version: 8.1.23
- Database driver & version: MySQL 8.0.33
- Plugins & versions: Amazon S3 2.0.3, Asset Rev 7.0.0, Element API 3.0.1.1, Redactor 3.0.4, Super Table 3.0.12
👍 Also noticed this
For now I used 'cache' => App::devMode() ? false : 'PT1M',
, but haven't tested if this works in all cases
Same issue. Nothing helps even when cache is fully disabled. Only through ui when I clean the element-api cache or craft data-cache the response actually updates.
The weird thing is, the code is executed normally, and I see the updated data in the logs. But the response the browser receives is still the old one. Tested in Chrome and Firefox, both display the old reponse. devMode is also set.
return [
'defaults' => [
'elementType' => Entry::class,
'resourceKey' => 'elements',
'pretty' => Craft::$app->getConfig()->general->devMode,
'paginate' => false,
'cache' => false
],
'endpoints' => [
// ============================================================================================
// Globals
'globals' => function() {
if ($response = ApiHelper::assumeGet()) {
return $response;
}
Craft::warning('Globals requested');
$globals = GlobalSet::find()->all();
$result = new Item();
$result->setData($globals);
$r = rand(0, 1000);
$result->setTransformer(new GlobalTransformer($r));
Craft::$app->getResponse()->setNoCacheHeaders();
$r = $result->getTransformer()->transform($result->getData());
Craft::warning('Globals response' . print_r($r, true));
return $result;
},
/edit: maybe bit older version of the plugin: Craft edition & version Craft Pro 4.5.11.1 Yii version 2.0.48.1 Element API 3.0.1.1
Can anyone help on this?
You could set cache
dynamically based on whether Dev Mode is enabled:
'cache' => \craft\helpers\App::devMode(),