Problem/Motivation
The change introduced in this issue https://www.drupal.org/project/rest_menu_items/issues/3039979 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
- stop using ModifiedResourceResponse - it's for POST/PUT requests, it's not intended for GET responses
- fix cache metadata to correct cache-invalidation
Remaining tasks
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | rest-menu-items-3039979.patch | 8.25 KB | fago |
| #5 | rest_menu_items-3482637-5.patch | 2.8 KB | useernamee |
| #2 | rest_menu_items-3482637-2.patch | 1.83 KB | useernamee |
Issue fork rest_menu_items-3482637
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
useernamee commentedI reverted the use of ModifiedResourceResponse.
Comment #3
useernamee commentedWrong patch. Fix coming soon.
Comment #4
useernamee commentedComment #5
useernamee commentedComment #8
fagoSo instead of using an uncacheable response, it shoudl have correct cache-metadata. added this.
attaching a copy of the change for composer-patches usage.
Comment #9
fagoThe MR has some phpstan fails which are unrelated. opened #3566606: Deprecations cause phpstan errors for that.
Then there is a phpunit test checking cache headers which needs to be updated. Done so. Note: I used AI to help with this.
Comment #10
ciprian.stavovei commentedThe fix is working correctly, the caching is added now: cache-control: max-age=60, public, s-maxage=2764800.
Comment #11
batigolixI tested this as well.
On the 3.0.x branch:
On the issue branch 3482637-response-of-the:
I ll merge this and close the issue.
Many thanks for your help.