Problem/motivation

The field on the search form appears to be render cached.

Steps to reproduce

  1. On a clean site, configure gcs. Place the gcs block in the content region, and give permission for anon to use the search
  2. In a browser (browser 1) as an anonymous user, navigate to a page with the gcs block, but do not submit.
  3. In a separate browser (browser 2), log in as an administrator and clear the caches
  4. In browser 1, perform multiple searches. The search results should always be correct, but the value in the input should be stale

Proposed resolution

This module uses Gitlab CI for automated testing of proposed code changes. To contribute code changes, create a merge request. Patch files will not trigger automated testing and are not sufficient for review.

Issue fork google_cse-3545822

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

miwayha created an issue. See original summary.

miwayha’s picture

I've worked around this issue with the following:

/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_google_cse_search_box_form_alter(
&$form,
FormStateInterface $form_state,
$form_id
) {
$form['#cache']['max-age'] = 0;
}

The problem with this approach is that it gets autoplaceholdered, which can cause issues with bigpipe.

mark_fullmer’s picture

Thanks for reporting this. I'm thinking that the appropriate fix here, based on what you've outlined, would be to add a cache context to the block so that the render caching is able to vary by user. Something along the lines of:

diff --git a/src/Plugin/Block/GoogleSearchBlock.php b/src/Plugin/Block/GoogleSearchBlock.php
index 39e7029..56c8f6a 100644
--- a/src/Plugin/Block/GoogleSearchBlock.php
+++ b/src/Plugin/Block/GoogleSearchBlock.php
@@ -178,6 +178,7 @@ class GoogleSearchBlock extends BlockBase implements ContainerFactoryPluginInter
       // This conditional handles search configurations that were deleted.
       return;
     }
+    $form['#cache']['contexts'][] = 'user.roles';
+    $form['#cache']['contexts'][] = 'url.query_args';
     $plugin = $entity->getPlugin();
     $google_markup = $plugin->buildResults();
     $config = $this->configFactory->get('search.page.' . $search_id);

Would you be able to see if the above method works for your use case?

mark_fullmer’s picture

Title: Search Form Input Appears to be Render Cached Undesirably » Search form submission is not consistently populated with user query
Assigned: Unassigned » mark_fullmer
mark_fullmer’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.