diff --git a/modules/roblib_search_eds/js/eds_results_article.js b/modules/roblib_search_eds/js/eds_results_article.js index 489ebc8..cc29880 100644 --- a/modules/roblib_search_eds/js/eds_results_article.js +++ b/modules/roblib_search_eds/js/eds_results_article.js @@ -1,6 +1,8 @@ Drupal.behaviors.roblib_search_eds_articles = { attach: function (context, settings) { - if (context == document) { + // Run once, when the results container appears. The block may be + // rendered later by BigPipe, so don't rely on context being document. + if (once('roblib-search-eds-articles', '#roblib-search-content-eds-articles', context).length) { $query = drupalSettings.path.currentPath.replace('roblib_search/', ''); $url = settings.eds_article_search_url + $query; article_profile = settings.eds_article_profile; diff --git a/modules/roblib_search_eds/js/eds_results_book.js b/modules/roblib_search_eds/js/eds_results_book.js index 61fe43c..f33d076 100644 --- a/modules/roblib_search_eds/js/eds_results_book.js +++ b/modules/roblib_search_eds/js/eds_results_book.js @@ -1,6 +1,8 @@ Drupal.behaviors.roblib_search_eds = { attach: function (context, settings) { - if (context == document) { + // Run once, when the results container appears. The block may be + // rendered later by BigPipe, so don't rely on context being document. + if (once('roblib-search-eds-books', '#roblib-search-content-eds', context).length) { $query = drupalSettings.path.currentPath.replace('roblib_search/', ''); $url = settings.eds_book_search_url + $query; book_profile = settings.eds_book_profile; diff --git a/modules/roblib_search_eds/roblib_search_eds.libraries.yml b/modules/roblib_search_eds/roblib_search_eds.libraries.yml index e6f9059..cdc10fe 100644 --- a/modules/roblib_search_eds/roblib_search_eds.libraries.yml +++ b/modules/roblib_search_eds/roblib_search_eds.libraries.yml @@ -9,6 +9,7 @@ eds-book: - core/jquery - core/drupal - core/drupalSettings + - core/once eds-article: version: 1.x css: @@ -20,4 +21,5 @@ eds-article: - core/jquery - core/drupal - core/drupalSettings + - core/once diff --git a/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php b/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php index 483760e..48e3806 100644 --- a/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php +++ b/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php @@ -3,6 +3,7 @@ namespace Drupal\roblib_search_eds\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; /** * Provides EDS Article search results. @@ -40,10 +41,21 @@ class RoblibSearchEdsArticles extends BlockBase { /** * {@inheritdoc} + * + * The block only renders a placeholder and settings; results are fetched + * client side from uncached JSON endpoints, so the markup can be cached. + * Setting max-age 0 here would make BigPipe stream the block in later. */ - public function getCacheMaxAge() { - // Reading dynamic content from external source so no caching. - return 0; + public function getCacheTags() { + return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_eds.settings']); + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + // The search URL in drupalSettings includes the scheme and host. + return Cache::mergeContexts(parent::getCacheContexts(), ['url.site']); } } diff --git a/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsBooks.php b/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsBooks.php index 2b3096f..1b371cd 100644 --- a/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsBooks.php +++ b/modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsBooks.php @@ -3,6 +3,7 @@ namespace Drupal\roblib_search_eds\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; /** * Provides EDS book search results. @@ -40,10 +41,21 @@ class RoblibSearchEdsBooks extends BlockBase { /** * {@inheritdoc} + * + * The block only renders a placeholder and settings; results are fetched + * client side from uncached JSON endpoints, so the markup can be cached. + * Setting max-age 0 here would make BigPipe stream the block in later. */ - public function getCacheMaxAge() { - // Reading dynamic content from external source so no caching. - return 0; + public function getCacheTags() { + return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_eds.settings']); + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + // The search URL in drupalSettings includes the scheme and host. + return Cache::mergeContexts(parent::getCacheContexts(), ['url.site']); } diff --git a/modules/solr_site/js/solr_site_bestbet.js b/modules/solr_site/js/solr_site_bestbet.js index 84da7b8..72f7467 100644 --- a/modules/solr_site/js/solr_site_bestbet.js +++ b/modules/solr_site/js/solr_site_bestbet.js @@ -1,6 +1,8 @@ Drupal.behaviors.roblib_search_solr_bestbet_results = { attach: function (context, settings) { - if (context == document) { + // Run once, when the results container appears. The block may be + // rendered later by BigPipe, so don't rely on context being document. + if (once('roblib-search-solr-bestbet', '#roblib-search-content-solr-site-bestbet', context).length) { query = drupalSettings.path.currentPath.replace('roblib_search/', ''); solrUrl = settings.solr_site_search_url + query; baseUrl = settings.solr_bestbet_base_url + query; diff --git a/modules/solr_site/js/solr_site_results.js b/modules/solr_site/js/solr_site_results.js index d0c9a14..38281d6 100644 --- a/modules/solr_site/js/solr_site_results.js +++ b/modules/solr_site/js/solr_site_results.js @@ -1,6 +1,8 @@ Drupal.behaviors.roblib_search_solr_site_results = { attach: function (context, settings) { - if (context == document) { + // Run once, when the results container appears. The block may be + // rendered later by BigPipe, so don't rely on context being document. + if (once('roblib-search-solr-results', '#roblib-search-content-solr-site-results', context).length) { unencoded_query = drupalSettings.path.currentPath.replace('roblib_search/', ''); query = encodeURIComponent(unencoded_query); solrUrl = settings.solr_site_search_url + query; diff --git a/modules/solr_site/roblib_search_solr_site.libraries.yml b/modules/solr_site/roblib_search_solr_site.libraries.yml index f28ebce..de26ff3 100644 --- a/modules/solr_site/roblib_search_solr_site.libraries.yml +++ b/modules/solr_site/roblib_search_solr_site.libraries.yml @@ -9,6 +9,7 @@ solr-site: - core/jquery - core/drupal - core/drupalSettings + - core/once solr-bestbet: version: 1.x js: @@ -17,3 +18,4 @@ solr-bestbet: - core/jquery - core/drupal - core/drupalSettings + - core/once diff --git a/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php b/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php index 89b23ef..3993fa4 100644 --- a/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php +++ b/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php @@ -3,6 +3,7 @@ namespace Drupal\roblib_search_solr_site\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; /** * Provides EDS Article search results. @@ -39,10 +40,13 @@ class RoblibSearchSolrSiteBestbet extends BlockBase { /** * {@inheritdoc} + * + * The block only renders a placeholder and settings; results are fetched + * client side from uncached JSON endpoints, so the markup can be cached. + * Setting max-age 0 here would make BigPipe stream the block in later. */ - public function getCacheMaxAge() { - // Reading dynamic content from external source so no caching. - return 0; + public function getCacheTags() { + return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_solr_site.settings']); } } diff --git a/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php b/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php index 1af9949..41bf642 100644 --- a/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php +++ b/modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php @@ -3,6 +3,7 @@ namespace Drupal\roblib_search_solr_site\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Cache\Cache; /** * Provides EDS book search results. @@ -41,10 +42,13 @@ class RoblibSearchSolrsiteGeneral extends BlockBase { /** * {@inheritdoc} + * + * The block only renders a placeholder and settings; results are fetched + * client side from uncached JSON endpoints, so the markup can be cached. + * Setting max-age 0 here would make BigPipe stream the block in later. */ - public function getCacheMaxAge() { - // Reading dynamic content from external source so no caching. - return 0; + public function getCacheTags() { + return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_solr_site.settings']); } }