diff --git a/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php b/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
index b34b8d308b9ce346d89ad21eb909a6daefbdd086..753c86b2c47dd018a61714272e1a3a55812535a7 100644
--- a/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
+++ b/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
@@ -38,17 +38,10 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) {
   public function checkAccess(MediaLibraryState $state, AccountInterface $account) {
     $parameters = $state->getOpenerParameters() + ['entity_id' => NULL];
 
-    $process_result = function ($result) {
-      if ($result instanceof RefinableCacheableDependencyInterface) {
-        $result->addCacheContexts(['url.query_args']);
-      }
-      return $result;
-    };
-
     // Forbid access if any of the required parameters are missing.
     foreach (['entity_type_id', 'bundle', 'field_name'] as $key) {
       if (empty($parameters[$key])) {
-        return $process_result(AccessResult::forbidden("$key parameter is missing."));
+        return AccessResult::forbidden("$key parameter is missing.")->addCacheableDependency($state);
       }
     }
 
@@ -76,7 +69,10 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
 
     // If entity-level access is denied, there's no point in continuing.
     if (!$entity_access->isAllowed()) {
-      return $process_result($entity_access);
+      if ($entity_access instanceof RefinableCacheableDependencyInterface) {
+        $entity_access->addCacheableDependency($state);
+      }
+      return $entity_access;
     }
 
     // If the entity has not been loaded, create it in memory now.
@@ -100,7 +96,11 @@ public function checkAccess(MediaLibraryState $state, AccountInterface $account)
     }
 
     $field_access = $access_handler->fieldAccess('edit', $field_definition, $account, $items, TRUE);
-    return $process_result($entity_access->andIf($field_access));
+    $access = $entity_access->andIf($field_access);
+    if ($access instanceof RefinableCacheableDependencyInterface) {
+      $access->addCacheableDependency($state);
+    }
+    return $access;
   }
 
   /**
diff --git a/core/modules/media_library/src/MediaLibraryState.php b/core/modules/media_library/src/MediaLibraryState.php
index 258f19ded14f5114e2a6dfe14aa6b62d5bda9eb3..5de89162827c0e4791f98ed7aef58571cc45e6ca 100644
--- a/core/modules/media_library/src/MediaLibraryState.php
+++ b/core/modules/media_library/src/MediaLibraryState.php
@@ -3,6 +3,8 @@
 namespace Drupal\media_library;
 
 use Drupal\Component\Utility\Crypt;
+use Drupal\Core\Cache\Cache;
+use Drupal\Core\Cache\CacheableDependencyInterface;
 use Drupal\Core\Site\Settings;
 use Symfony\Component\HttpFoundation\ParameterBag;
 use Symfony\Component\HttpFoundation\Request;
@@ -42,7 +44,7 @@
  *   subject to change in minor releases. External code should not instantiate
  *   or extend this class.
  */
-class MediaLibraryState extends ParameterBag {
+class MediaLibraryState extends ParameterBag implements CacheableDependencyInterface {
 
   /**
    * {@inheritdoc}
@@ -274,4 +276,25 @@ public function getOpenerParameters() {
     return $this->get('media_library_opener_parameters', []);
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheContexts() {
+    return ['url.query_args'];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheMaxAge() {
+    return Cache::PERMANENT;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCacheTags() {
+    return [];
+  }
+
 }