Response of the rest resource is uncacheable by browser

Created on 23 October 2024, 8 months ago

Problem/Motivation

The change introduced in this issue https://www.drupal.org/project/rest_menu_items/issues/3039979 Menu Items Extras fields also returned by the API Fixed in comments #24 and #25 made the response of the resource uncacheable by browser. If you check the headers of the response you will see "Cache-Control: must-revalidate, no-cache, private". This is expected because of ModifiedResourceResponse, see class web/core/modules/rest/src/ModifiedResourceResponse.php:

/**
 * A response that does not contain cacheability metadata.
 *
 * Used when resources are modified by a request: responses to unsafe requests
 * (POST/PATCH/DELETE) can never be cached.
 *
 * @see \Drupal\rest\ResourceResponse
 */
class ModifiedResourceResponse extends Response implements ResourceResponseInterface {

For this reason when calling the endpoint next time, browser tries to load the menu again. Also it is not possible to use CDN with such response headers.

Steps to reproduce

Install the module.
Enable rest resource.
Try to load the main menu like this: /api/menu_items/main .
Observe response headers, see that response is uncacheable.

Proposed resolution

Find the cause of cacheable metadata leaks that were identified in the issue https://www.drupal.org/project/rest_menu_items/issues/3039979 Menu Items Extras fields also returned by the API Fixed and add relevant metadata instead of using ModifiedResourceResponse.

Remaining tasks

Find cacheable metadata leaks, add relevant information to the response and use ResourceResponse.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇩🇪Germany a.dmitriiev

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024