Declare the dependency on the GET parameter with a cache context:
/mymodule/mymodule.module
function mymodule_preprocess_paragraph(&$variables) {
$foo = \Drupal::request()->query->get('foo');
$variables['content']['foo'] = [
'#markup' => "foo = $foo",
'#cache' => ['contexts' => ['url.query_args:foo']],
];
}
More info https://www.drupal.org/docs/drupal-apis/cache-api/cache-contexts
However, if you have more than a few possible values of foo
this is a problem with cache performance, especially for nested entities like paragraphs. Each value creates a new cache entry for the paragraph and its parent entities.
To avoid this put a placeholder in the paragaph:
/mymodule/mymodule.module
function mymodule_preprocess_paragraph(&$variables) {
$variables['content']['foo_placeholdered'] = [
'#lazy_builder' => ['\Drupal\mymodule\MymoduleLazyBuilders::getFoo', []],
'#create_placeholder' => TRUE,
];
}
and build the content which depends on the GET parameter in a lazy builder:
/mymodule/src/MymoduleLazyBuilders.php
<?php
namespace Drupal\mymodule;
use Drupal\Core\Render\Element\RenderCallbackInterface;
class MymoduleLazyBuilders implements RenderCallbackInterface {
public static function getFoo() {
$foo = \Drupal::request()->query->get('foo');
return [
'#markup' => "foo = $foo",
'#cache' => ['contexts' => ['url.query_args:foo']],
];
}
}
For time consuming calculations such placeholders can be delivered through BigPipe. So if you enable this module you can also improve page speed.