5 * Adds BigPipe no-JS detection.
8 use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
9 use Drupal\Core\Routing\RouteMatchInterface;
13 * Implements hook_help().
15 function big_pipe_help($route_name, RouteMatchInterface $route_match) {
16 switch ($route_name) {
17 case 'help.page.big_pipe':
18 $output = '<h3>' . t('About') . '</h3>';
19 $output .= '<p>' . t('The BigPipe module sends pages with dynamic content in a way that allows browsers to show them much faster. For more information, see the <a href=":big_pipe-documentation">online documentation for the BigPipe module</a>.', [':big_pipe-documentation' => 'https://www.drupal.org/documentation/modules/big_pipe']) . '</p>';
20 $output .= '<h3>' . t('Uses') . '</h3>';
22 $output .= '<dt>' . t('Speeding up your site') . '</dt>';
23 $output .= '<dd>' . t('The module requires no configuration. Every part of the page contains metadata that allows BigPipe to figure this out on its own.') . '</dd>';
31 * Implements hook_page_attachments().
33 * @see \Drupal\big_pipe\Controller\BigPipeController::setNoJsCookie()
35 function big_pipe_page_attachments(array &$page) {
36 // Routes that don't use BigPipe also don't need no-JS detection.
37 if (\Drupal::routeMatch()->getRouteObject()->getOption('_no_big_pipe')) {
41 $request = \Drupal::request();
42 // BigPipe is only used when there is an actual session, so only add the no-JS
43 // detection when there actually is a session.
44 // @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy.
45 $session_exists = \Drupal::service('session_configuration')->hasSession($request);
46 $page['#cache']['contexts'][] = 'session.exists';
47 // Only do the no-JS detection while we don't know if there's no JS support:
48 // avoid endless redirect loops.
49 $has_big_pipe_nojs_cookie = $request->cookies->has(BigPipeStrategy::NOJS_COOKIE);
50 $page['#cache']['contexts'][] = 'cookies:' . BigPipeStrategy::NOJS_COOKIE;
51 if ($session_exists) {
52 if (!$has_big_pipe_nojs_cookie) {
53 // Let server set the BigPipe no-JS cookie.
54 $page['#attached']['html_head'][] = [
56 // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
60 'http-equiv' => 'Refresh',
61 'content' => '0; URL=' . Url::fromRoute('big_pipe.nojs', [], ['query' => \Drupal::service('redirect.destination')->getAsArray()])->toString(),
64 'big_pipe_detect_nojs',
68 // Let client delete the BigPipe no-JS cookie.
69 $page['#attached']['html_head'][] = [
72 '#value' => 'document.cookie = "' . BigPipeStrategy::NOJS_COOKIE . '=1; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"',