Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint Caching While in Dev Mode #185

Closed
kmgdevelopment opened this issue Oct 24, 2023 · 3 comments
Closed

Endpoint Caching While in Dev Mode #185

kmgdevelopment opened this issue Oct 24, 2023 · 3 comments
Labels

Comments

@kmgdevelopment
Copy link

kmgdevelopment commented Oct 24, 2023

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

  • 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
@mandrasch
Copy link

👍 Also noticed this

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

@julianschelker
Copy link

julianschelker commented Mar 19, 2024

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
Copy link
Member

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants