craftcms / element-api

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

Endpoint Caching While in Dev Mode #185

Closed kmgdevelopment closed 6 months ago

kmgdevelopment commented 11 months ago

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

  1. Enable devMode in environment
  2. Set up a JSON endpoint in config/element-api.php
  3. Load the endpoint in the browser
  4. Edit the endpoint
  5. Refresh the endpoint in the browser

Additional info

mandrasch commented 9 months ago

👍 Also noticed this

For now I used 'cache' => App::devMode() ? false : 'PT1M',, but haven't tested if this works in all cases

julianschelker commented 6 months ago

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?

brandonkelly commented 6 months ago

You could set cache dynamically based on whether Dev Mode is enabled:

'cache' => \craft\helpers\App::devMode(),