diff --git a/core/lib/Drupal/Core/Session/ResponseKeepSessionOpenInterface.php b/core/lib/Drupal/Core/Session/ResponseKeepSessionOpenInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..4f4cdcc30d0a1c69d6ba17216fcdf20bf390d264 --- /dev/null +++ b/core/lib/Drupal/Core/Session/ResponseKeepSessionOpenInterface.php @@ -0,0 +1,16 @@ +<?php + +namespace Drupal\Core\Session; + +/** + * Indicates that sessions for this response should be kept open after sending. + * + * By default, Drupal closes sessions as soon as the response is sent. If + * a response implements this interface, Drupal will skip this behavior and + * assume that the session will be closed manually later in the request. + * + * @see Drupal\Core\StackMiddleware\Session + * @see Drupal\big_pipe\src\Render\BigPipeResponse + * @internal + */ +interface ResponseKeepSessionOpenInterface {} diff --git a/core/lib/Drupal/Core/StackMiddleware/Session.php b/core/lib/Drupal/Core/StackMiddleware/Session.php index 94841e1508dde7a62b2ebb85a9a5406b3600f2ff..6e8082b81e44f0936a038e763453775c460a985b 100644 --- a/core/lib/Drupal/Core/StackMiddleware/Session.php +++ b/core/lib/Drupal/Core/StackMiddleware/Session.php @@ -2,6 +2,7 @@ namespace Drupal\Core\StackMiddleware; +use Drupal\Core\Session\ResponseKeepSessionOpenInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -58,7 +59,7 @@ public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TR $result = $this->httpKernel->handle($request, $type, $catch); - if ($type === self::MAIN_REQUEST && PHP_SAPI !== 'cli' && $request->hasSession()) { + if ($type === self::MAIN_REQUEST && !$result instanceof ResponseKeepSessionOpenInterface && PHP_SAPI !== 'cli' && $request->hasSession()) { $request->getSession()->save(); } diff --git a/core/modules/big_pipe/src/Render/BigPipe.php b/core/modules/big_pipe/src/Render/BigPipe.php index 1dab69c4e3c0a0df900b6d235d817dcc4d3b6d87..1eab778c0c2106e1c393d4bd7c1dbceee3ff6351 100644 --- a/core/modules/big_pipe/src/Render/BigPipe.php +++ b/core/modules/big_pipe/src/Render/BigPipe.php @@ -220,16 +220,6 @@ public function __construct(RendererInterface $renderer, SessionInterface $sessi } } - /** - * Performs tasks before sending content (and rendering placeholders). - */ - protected function performPreSendTasks() { - // The content in the placeholders may depend on the session, and by the - // time the response is sent (see index.php), the session is already - // closed. Reopen it for the duration that we are rendering placeholders. - $this->session->start(); - } - /** * Performs tasks after sending content (and rendering placeholders). */ @@ -282,8 +272,6 @@ public function sendContent(BigPipeResponse $response) { $cumulative_assets = AttachedAssets::createFromRenderArray(['#attached' => $attachments]); $cumulative_assets->setAlreadyLoadedLibraries($attachments['library']); - $this->performPreSendTasks(); - // Find the closing </body> tag and get the strings before and after. But be // careful to use the latest occurrence of the string "</body>", to ensure // that strings in inline JavaScript or CDATA sections aren't used instead. diff --git a/core/modules/big_pipe/src/Render/BigPipeResponse.php b/core/modules/big_pipe/src/Render/BigPipeResponse.php index 573b7dcb5ef3e61c79a1e0baf1fd9edc469c59f8..0af6996170681ea613094cc6ababcaf20dc7dc40 100644 --- a/core/modules/big_pipe/src/Render/BigPipeResponse.php +++ b/core/modules/big_pipe/src/Render/BigPipeResponse.php @@ -3,6 +3,7 @@ namespace Drupal\big_pipe\Render; use Drupal\Core\Render\HtmlResponse; +use Drupal\Core\Session\ResponseKeepSessionOpenInterface; /** * A response that is sent in chunks by the BigPipe service. @@ -18,7 +19,7 @@ * created in https://www.drupal.org/node/2577631. Only code internal to * BigPipe should instantiate or type hint to this class. */ -class BigPipeResponse extends HtmlResponse { +class BigPipeResponse extends HtmlResponse implements ResponseKeepSessionOpenInterface { /** * The BigPipe service. diff --git a/core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryAuthenticatedPerformanceTest.php b/core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryAuthenticatedPerformanceTest.php index 8e66e7df19aee7bb26748ea81d39389f0ca441dd..2612ecf5bac5bc02d64fca29635cecf1e75efe5f 100644 --- a/core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryAuthenticatedPerformanceTest.php +++ b/core/profiles/demo_umami/tests/src/FunctionalJavascript/OpenTelemetryAuthenticatedPerformanceTest.php @@ -35,8 +35,7 @@ public function testFrontPageAuthenticatedWarmCache(): void { $performance_data = $this->collectPerformanceData(function () { $this->drupalGet('<front>'); }, 'authenticatedFrontPage'); - $this->assertGreaterThanOrEqual(10, $performance_data->getQueryCount()); - $this->assertLessThanOrEqual(12, $performance_data->getQueryCount()); + $this->assertCountBetween(9, 11, $performance_data->getQueryCount()); $this->assertSame(45, $performance_data->getCacheGetCount()); $this->assertSame(0, $performance_data->getCacheSetCount()); $this->assertSame(0, $performance_data->getCacheDeleteCount()); diff --git a/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php b/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php index c977aaf51d1cf4a0cb5df7a36eca679e56b0a93e..5fece5679a744582535178574cfca63e74c4d212 100644 --- a/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php +++ b/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php @@ -113,7 +113,7 @@ public function testLogin(): void { $this->submitLoginForm($account); }); - $this->assertCountBetween(26, 31, $performance_data->getQueryCount()); + $this->assertCountBetween(25, 30, $performance_data->getQueryCount()); $this->assertSame(64, $performance_data->getCacheGetCount()); $this->assertSame(1, $performance_data->getCacheSetCount()); $this->assertSame(1, $performance_data->getCacheDeleteCount()); @@ -146,7 +146,7 @@ public function testLoginBlock(): void { $performance_data = $this->collectPerformanceData(function () use ($account) { $this->submitLoginForm($account); }); - $this->assertCountBetween(31, 34, $performance_data->getQueryCount()); + $this->assertCountBetween(30, 33, $performance_data->getQueryCount()); $this->assertSame(85, $performance_data->getCacheGetCount()); $this->assertSame(1, $performance_data->getCacheSetCount()); $this->assertSame(1, $performance_data->getCacheDeleteCount());