Cache issue for "url_link" enhancer

Created on 16 October 2024, about 1 month ago

Problem/Motivation

I think there is a caching issue in the jsonapi_extras module when the “URL for link (link field only)” enhancer is enabled. Pathauto is assumed installed below :)

Steps to reproduce

  1. Add a link field to a node
  2. Create two nodes: “My first node” (so the pathauto is: /my-first-node) and “My second node” (so the pathauto is: /my-second-node)
  3. Populate the link field on /my-first-node with a link to /my-second-node
  4. Add the “URL for link (link field only)” enhancer to the link field (I don’t have “Absolute URLs” enabled but I don’t think it matters)
  5. See that the url in the jsonapi is pointing to “/my-second-node”
  6. Update /my-second-node with a new title so the pathauto is now “/my-second-node-with-new-title”
  7. See that the url in the jsonapi response still points to the “/my-second-node”.

If I drush cr manually, I see the correct (updated) url.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇩🇰Denmark blymemikkel Copenhagen

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

Merge Requests

Comments & Activities

  • Pipeline finished with Canceled
    about 2 months ago
    Total: 296s
    #291931
  • Pipeline finished with Failed
    about 2 months ago
    Total: 213s
    #291934
  • Issue created by @blymemikkel
  • Pipeline finished with Success
    about 1 month ago
    #311755
  • 🇺🇸United States mglaman WI, USA

    The generated URL has the associated metadata but doesn't capture it.

            $url = Url::fromUri($data['uri'], ['language' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)]);
    
            // Use absolute urls if configured.
            $configuration = $this->getConfiguration();
            if ($configuration['absolute_url']) {
              $url->setAbsolute(TRUE);
            }
    
            $data['url'] = $url->toString();
          }
    

    From the field item normalizer:

        $context = new Context($context);
        $context->offsetSet('field_item_object', $object);
        $context->offsetSet(CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY, $cacheability);
        $processed = $enhancer->undoTransform(
          $normalized_output->getNormalization(),
          $context
        );
    

    The fix is something like

            $generatedUrl = $url->toString(TRUE);
            $context->offsetGet(CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY)->addCacheableDependency($url);
    
            $data['url'] = $generatedUrl->getGeneratedUrl();
    
  • 🇩🇰Denmark blymemikkel Copenhagen

    Thanks, Matt! It seems to work when I replicate my steps. Created an MR with your changes.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 215s
    #311865
  • 🇺🇸United States mglaman WI, USA

    Great!

  • Pipeline finished with Failed
    about 1 month ago
    Total: 148s
    #314573
  • Pipeline finished with Skipped
    17 days ago
    #330582
Production build 0.71.5 2024