Changing Media items via the Media Library pop-up does not immediately affect the CK Editor Preview

Created on 19 April 2023, almost 2 years ago
Updated 12 September 2024, 4 months ago

Problem/Motivation

When changing a Media item directly in the media library, the CK Editor 5 preview does not immediately change. It has a 5 minute cache lifetime in the user's browser cache.

Steps to reproduce

Preparation :

  1. Installation Standard + media library
  2. Add media library plugin in a text format

Reproduce :

  1. create content
  2. Add a media embed in a ckeditor 5
  3. Save content
  4. edit media, change image, save media
  5. edit content

Proposed resolution

Bust the CK Editor Preview cache by appending a version query string based on the media checksum.

Remaining tasks

Provide test coverage

User interface changes

CK Editor Preview immediately changes

Introduced terminology

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

Media item previews within CK Editor immediately change if changes are made via the Media Library pop-up.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
CKEditor 5 

Last updated about 7 hours ago

Created by

🇫🇷France DrDam

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Issue created by @DrDam
  • Issue was unassigned.
  • Status changed to Postponed: needs info over 1 year ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Thanks for the excellent, detailed write-up 🙏 😊

    Very interesting edge case!

    Metadata about the embedded media is cached up to 5 minutes in the client's browser — see \Drupal\ckeditor5\Controller\CKEditor5MediaController::mediaEntityMetadata():

        return (new JsonResponse($response, 200))
          // Do not allow any intermediary to cache the response, only the end user.
          ->setPrivate()
          // Allow the end user to cache it for up to 5 minutes.
          ->setMaxAge(300);
    

    So can you please confirm that after 5 minutes, the preview in CKEditor 5 does look accurate?

  • Status changed to Active over 1 year ago
  • 🇫🇷France DrDam

    I confirm that after 5min, the preview is refresh.
    The change can occur fastly if I desactivate caches in my browser

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Great, that at least works as expected then.

    I can try to think of a work-around, but this is definitely working as intended for now: great performance for the 99.9% case is more important.

  • 🇫🇷France DrDam

    Yes, this is a minor anomaly.
    I encountered it while testing the media plugin

  • 🇳🇿New Zealand danielveza Brisbane, AU

    I wonder if we should make a config form to manage this?

    Could add an option to enable this and number of seconds to cache. Defaults would be enabled and to cache for 300 seconds.

  • Status changed to Needs review over 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    29,354 pass, 3 fail
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    I thought of a reasonable work-around I think 🤓

    Can you give this a try?

  • Status changed to Needs work over 1 year ago
  • 🇫🇷France DrDam

    $cache_tags_checksum = \Drupal::service(CacheTagsChecksumInterface::class);
    you are sure of that ?

  • 🇺🇸United States joshuami Portland, OR

    I ran across this issue with a document media type that is configured to show translations on some display modes via an entity views attach. (I know it sounds complicated, but it was a pretty simple solution for a complex translation need. 😆) If the document was embedded, and then immediately translated, the preview does not update for a bit. I didn't time it, but the 5 minutes mentioned above sounds about right.

    So I tried the patch in #9. It did invalidate the cache for the preview upon applying the patch, but it didn't really fix the caching issue on subsequent tweaks to the display mode.

    The patch did have an unintended consequence after applying it. It showed all display modes as an option even if the media type did not have that display mode configured. I really don't understand how setting the cachetag checksum could have had that affect, but I thought I would report it for whomever might pick this up.

  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    but it didn't really fix the caching issue on subsequent tweaks to the display mode.

    That can be fixed by adding the relevant cache tags for those too.

    The patch did have an unintended consequence after applying it. It showed all display modes as an option even if the media type did not have that display mode configured. I really don't understand how setting the cachetag checksum could have had that affect, but I thought I would report it for whomever might pick this up.

    That should be literally impossible 😳

    Can you consistently reproduce this with the patch applied, and consistently NOT reproduce it without the patch applied? 🙏

  • 🇺🇸United States joshuami Portland, OR

    That should be literally impossible 😳

    I was equally confused by that result.

    To make sure it wasn't some oddity in the moderately complex site on which I was working, I tested the patch on a site closer to the standard install. The only additions I made were to add two new display modes for images that were not used by the other default media entity types.

    Before applying the patch, an embedded document showed only the "Default" view mode:

    After applying the patch, an embedded document showed all enabled view modes even though "Half width" and "Narrow" were not configured display modes for documents.

  • Status changed to Needs review 11 months ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    🤯 Without digging into the JS code, that seems an inexplicable mystery for sure 😄 I'm sure there is some logical explanation. There must be. Let's convert this patch to an MR to find out if the existing test coverage discovers this too. 🕵️

  • Pipeline finished with Failed
    11 months ago
    Total: 473s
    #107781
  • Status changed to Needs work 11 months ago
  • 🇺🇸United States smustgrave

    Seemed to cause test failure

    Also tagging for issue summary update for once solution is agreed.

  • 🇬🇧United Kingdom scott_euser
    1. Tested this out and it solves the problem, the media item automatically changes
    2. Multiple media display modes are not visible, so it seems like #15 is no longer an issue
    3. Updated issue summary to standard template
    4. Update the details in title & issue summary to reflect that this applies to any Media item preview, not just image.
    5. Updated to normal as its an editor user experience issue that makes the wysiwyg experience feel broken to an editor with no obvious workaround (until you explain to the editor they can wait 5 minutes or add a new media item/delete old)

    Still needs test coverage though so still needs work

  • 🇬🇧United Kingdom scott_euser

    Hiding patch in favour of MR

  • 🇬🇧United Kingdom scott_euser

    Hmmm actually this causes other issues. When using CK Editor 5, once you have this patch enabled, you can cause a console error which prevents selection of the image:

    1. Load CK Editor 5 Premium in Drupal
    2. Insert a media image
    3. Save the content
    4. Edit the media image, replacing it with a new image
    5. Click the image to select it within the editor
    6. See error in console
Production build 0.71.5 2024