Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Adds BigPipe no-JS detection.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;
|
Chris@0
|
9 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
10 use Drupal\Core\Url;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Implements hook_help().
|
Chris@0
|
14 */
|
Chris@0
|
15 function big_pipe_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
16 switch ($route_name) {
|
Chris@0
|
17 case 'help.page.big_pipe':
|
Chris@0
|
18 $output = '<h3>' . t('About') . '</h3>';
|
Chris@0
|
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>';
|
Chris@0
|
20 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
21 $output .= '<dl>';
|
Chris@0
|
22 $output .= '<dt>' . t('Speeding up your site') . '</dt>';
|
Chris@0
|
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>';
|
Chris@0
|
24 $output .= '</dl>';
|
Chris@0
|
25
|
Chris@0
|
26 return $output;
|
Chris@0
|
27 }
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Implements hook_page_attachments().
|
Chris@0
|
32 *
|
Chris@0
|
33 * @see \Drupal\big_pipe\Controller\BigPipeController::setNoJsCookie()
|
Chris@0
|
34 */
|
Chris@0
|
35 function big_pipe_page_attachments(array &$page) {
|
Chris@0
|
36 // Routes that don't use BigPipe also don't need no-JS detection.
|
Chris@0
|
37 if (\Drupal::routeMatch()->getRouteObject()->getOption('_no_big_pipe')) {
|
Chris@0
|
38 return;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 $request = \Drupal::request();
|
Chris@0
|
42 // BigPipe is only used when there is an actual session, so only add the no-JS
|
Chris@0
|
43 // detection when there actually is a session.
|
Chris@0
|
44 // @see \Drupal\big_pipe\Render\Placeholder\BigPipeStrategy.
|
Chris@0
|
45 $session_exists = \Drupal::service('session_configuration')->hasSession($request);
|
Chris@0
|
46 $page['#cache']['contexts'][] = 'session.exists';
|
Chris@0
|
47 // Only do the no-JS detection while we don't know if there's no JS support:
|
Chris@0
|
48 // avoid endless redirect loops.
|
Chris@0
|
49 $has_big_pipe_nojs_cookie = $request->cookies->has(BigPipeStrategy::NOJS_COOKIE);
|
Chris@0
|
50 $page['#cache']['contexts'][] = 'cookies:' . BigPipeStrategy::NOJS_COOKIE;
|
Chris@0
|
51 if ($session_exists) {
|
Chris@0
|
52 if (!$has_big_pipe_nojs_cookie) {
|
Chris@0
|
53 // Let server set the BigPipe no-JS cookie.
|
Chris@0
|
54 $page['#attached']['html_head'][] = [
|
Chris@0
|
55 [
|
Chris@0
|
56 // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
|
Chris@0
|
57 '#tag' => 'meta',
|
Chris@0
|
58 '#noscript' => TRUE,
|
Chris@0
|
59 '#attributes' => [
|
Chris@0
|
60 'http-equiv' => 'Refresh',
|
Chris@0
|
61 'content' => '0; URL=' . Url::fromRoute('big_pipe.nojs', [], ['query' => \Drupal::service('redirect.destination')->getAsArray()])->toString(),
|
Chris@0
|
62 ],
|
Chris@0
|
63 ],
|
Chris@0
|
64 'big_pipe_detect_nojs',
|
Chris@0
|
65 ];
|
Chris@0
|
66 }
|
Chris@0
|
67 else {
|
Chris@0
|
68 // Let client delete the BigPipe no-JS cookie.
|
Chris@0
|
69 $page['#attached']['html_head'][] = [
|
Chris@0
|
70 [
|
Chris@0
|
71 '#tag' => 'script',
|
Chris@0
|
72 '#value' => 'document.cookie = "' . BigPipeStrategy::NOJS_COOKIE . '=1; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"',
|
Chris@0
|
73 ],
|
Chris@0
|
74 'big_pipe_detect_js',
|
Chris@0
|
75 ];
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|