Problem/Motivation
If you call \Drupal\Core\Cache\RefinableCacheableDependencyTrait::addCacheableDependency with something that doesn't implement CacheableDependencyInterface, you end up with uncacheable pages.
Examples of bugs in core this has lead to include: #3227637: NodeController::revisionOverview is uncacheable and probably more.
I would argue that we should move towards deprecating calling this method with an object that doesn't implement CacheableDependencyInterface to prevent people from inadvertently introducing non-cacheable pages.
Steps to reproduce
Call \Drupal\Core\Cache\RefinableCacheableDependencyTrait::addCacheableDependency with \Drupal::currentUser or a render array - see your page becomes uncacheable
Proposed resolution
Trigger a deprecation error if called with an object that doesn't implement CacheableDependencyInterface
In the next major version remove that deprecation error and add a type-hint (hard break).
Remaining tasks
All of the above
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3232018
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:
- 3232018-trigger-deprecation
changes, plain diff MR !11466
- 11.x
changes, plain diff MR !7362
Comments
Comment #2
larowlanThis will fail on at least #3227637: NodeController::revisionOverview is uncacheable
Comment #3
acbramley commentedBig +1 on this. Someone accidentally did this in a formatter on one of our projects and it destroyed performance on some views listing/search pages. Took a long time to figure out what was causing it too!
Comment #7
larowlanComment #10
abhaypai commentedLanded on this issue from https://www.drupal.org/project/drupal/issues/3227637 and https://lendude.gitlab.io/bug-smash-initiative/ and i am still able to replicate this in D11
Comment #11
leksat commentedAnother +1. I spent quite some time with a debugger trying to find why a GraphQL query wasn't cached.
Comment #13
leksat commentedTried to fix
LocalActionManagerTest::testGetActionsForRoute.It fails because in the test we mock
LocalActionInterfacewhich does not extendCacheableDependencyInterface. And then it is passed toRefinableCacheableDependencyTrait::addCacheableDependency().The actual class (
LocalActionDefault) however does extendCacheableDependencyInterface.I see two options:
1. Make
LocalActionInterfaceextendCacheableDependencyInterface(would it be an API change?)2. Mock
LocalActionDefaultinstead (but AFAICS we always mock interfaces, not classes)Does any of them look good? Are there other options? I have no clue 😅
Comment #14
larowlanI think making LocalActionInterface extend is reasonable - there's a lot of code in LocalActionDefault that would make it feasible that anything custom would extend it rather than replace it
Comment #15
leksat commentedTests are green, but there are open questions. I've put them into the MR. See todos in the code.
Comment #16
smustgrave commentedLeft a comment on the MR
Comment #17
leksat commented@smustgrave Changes are ready.
What about the two other questions pointed in TODOs? Should the maintainers of the corresponding modules answer them? There's one in
field_uiand one inlayout_builder.Comment #18
smustgrave commentedFor the todos may be out of scope to remove the cache there. So to keep this one moving lets open a follow up and update the comment with a @todo to the issue created. Then should be good here.
Comment #19
leksat commentedCreated follow-ups
- #3446507: Adjust caching logic of FieldReuseAccessCheck::access()
- #3446509: Adjust caching logic of LayoutBuilderAccessCheck::access()
and linked them in TODOs ✅
Please note: The deprecation notice points to 10.3.0, but the MR still targets 11.x
Comment #20
leksat commentedThe test failures seems to be caused by
PDOException: SQLSTATE[HY000] [2002] Connection refused. So I guess they are random and not related.It looks like I have no permissions to re-run them 🤷
Comment #21
smustgrave commentedWould recommend rebasing the MR. Seems to be 300+ commits behind.
Comment #22
mondrakeComment #23
leksat commentedRebased. Tests are green now ✅
Comment #24
smustgrave commentedFeedback does appear to be addressed and deprecation seems correct.
LGTM
Comment #25
catchOne question on the MR.
Comment #27
prudloff commentedComment #28
smustgrave commentedLeft some comments on the MR.
If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!
Comment #32
acbramley commentedAddressed feedback and moved this to a new MR since the work was on the 11.x branch in the fork which is problematic for local development.
Comment #33
smustgrave commentedFeedback appears to be addressed.
Comment #35
catchCommitted/pushed to 11.x, thanks!
Comment #37
quietone commentedAsked in slack about how to remember to add the type hint in 12.0.0 when the trigger_error is deleted. @larowlan suggested a new issue. The issue is #3514554: Add type hint to RefinableCacheableDependencyTrait::addCacheableDependency.
Comment #38
wim leersVery nice to see we’ve come so far that we can deprecate that! 🥳