diff --git a/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php b/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
index 348f22f1046bcfeb7e028825a6c7d273558822c9..ccbe1a9d262d29b448afdde2224961b88b2c2029 100644
--- a/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
+++ b/core/modules/media_library/src/MediaLibraryFieldWidgetOpener.php
@@ -41,17 +41,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);
       }
     }
 
@@ -83,7 +76,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.
@@ -107,7 +103,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 e1fd6e2e8b9c8ef4a03add70974669ac6563d2d0..e1f0b4632dcd7206ce9dae7ebe974975bfda55fe 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;
@@ -37,7 +39,7 @@
  *
  * @see \Drupal\media_library\MediaLibraryOpenerInterface
  */
-class MediaLibraryState extends ParameterBag {
+class MediaLibraryState extends ParameterBag implements CacheableDependencyInterface {
 
   /**
    * {@inheritdoc}
@@ -269,4 +271,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 [];
+  }
+
 }
diff --git a/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php b/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php
index dc159ea43a862fa489695c6bf707633d06f945c1..ffc5dd27ee4825b509f215d17876e6e85a206b33 100644
--- a/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php
+++ b/core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\Tests\media_library\Kernel;
 
+use Drupal\Core\Cache\Cache;
+use Drupal\Core\Cache\CacheableDependencyInterface;
 use Drupal\KernelTests\KernelTestBase;
 use Drupal\media_library\MediaLibraryState;
 use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
@@ -104,6 +106,11 @@ public function testCreate($opener_id, array $allowed_media_type_ids, $selected_
     }
     $state = MediaLibraryState::create($opener_id, $allowed_media_type_ids, $selected_type_id, $remaining_slots);
     $this->assertInstanceOf(MediaLibraryState::class, $state);
+
+    // Ensure that the state object carries cache metadata.
+    $this->assertInstanceOf(CacheableDependencyInterface::class, $state);
+    $this->assertSame(['url.query_args'], $state->getCacheContexts());
+    $this->assertSame(Cache::PERMANENT, $state->getCacheMaxAge());
   }
 
   /**