Cacheability is not respected when not using a lazy builder

Created on 15 November 2024, about 1 month ago

Problem/Motivation

If a computed field introduces some extra cache metadata, that metadata isn't used unless the field is displayed using a lazy builder.

Steps to reproduce

This can be demonstrated by the test_current_user plugin (enable the test_computed_field_plugins module). You also need two users with identical roles/permissions.

1. Add the "computed field as normal, create a node etc.
2. View the node as user A. Cache miss. Works as expected: user A's name is shown. Output is cached.
3. View the node as user B. Cache hit. User A's name is shown.

Although that plugin declares the user cache context in its getCacheability() function, this metadata doesn't make its way up to the node.

Proposed resolution

Apply the cache metadata when the computed field is displayed. Or, if this can't be done for some reason, we should document that.

Remaining tasks

TBC

πŸ› Bug report
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΈπŸ‡ͺSweden erik.erskine

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

Merge Requests

Comments & Activities

  • Issue created by @erik.erskine
  • πŸ‡ΈπŸ‡ͺSweden erik.erskine

    Did some digging, and found this in FieldFormatterBase::view():

        // Field item lists, in particular for computed fields, may carry cacheable
        // metadata which must be bubbled.
        if ($items instanceof CacheableDependencyInterface) {
          (new CacheableMetadata())
            ->addCacheableDependency($items)
            ->applyTo($elements);
        }
    

    So it looks as though ComputedFieldClass needs to implement CacheableDependencyInterface and pass on the cacheable metadata from the plugin. Patch coming up.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 142s
    #340033
  • πŸ‡ΈπŸ‡ͺSweden erik.erskine

    First attempt at seeing how we might solve this. Needs work for sure, but wanted to get feedback on the approach.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 229s
    #340068
  • Pipeline finished with Failed
    about 1 month ago
    Total: 139s
    #340296
  • πŸ‡¬πŸ‡§United Kingdom joachim

    > Did some digging, and found this in FieldFormatterBase::view():

    Nice find!

    I've fixed the broken tests -- could you rebase this please so the tests can run on it again?

  • Pipeline finished with Success
    24 days ago
    Total: 158s
    #350277
  • πŸ‡ΈπŸ‡ͺSweden erik.erskine

    Rebased!

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Thanks!

    I started looking at this, and figured I'd add test coverage. I've got something working (in branch 3487785-cacheability-tests) -- but I need to do a bit of clean-up on it as I had to try several approaches to get caching to kick in!

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Hmm on second thoughts, I can't get it to work :/
    Pushed everything to the branch.

Production build 0.71.5 2024