 core/themes/bartik/bartik.libraries.yml |  9 ++++++
 core/themes/bartik/bartik.theme         | 28 -------------------
 core/themes/bartik/layout.js            | 49 +++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/core/themes/bartik/bartik.libraries.yml b/core/themes/bartik/bartik.libraries.yml
index fd82367..ad64f7e 100644
--- a/core/themes/bartik/bartik.libraries.yml
+++ b/core/themes/bartik/bartik.libraries.yml
@@ -59,6 +59,15 @@ global-styling:
     theme:
       css/colors.css: {}
       css/print.css: { media: print }
+  dependencies:
+    - bartik/layout
+
+layout:
+  version: VERSION
+  js:
+    layout.js: { }
+  dependencies:
+    core/drupal
 
 messages:
   version: VERSION
diff --git a/core/themes/bartik/bartik.theme b/core/themes/bartik/bartik.theme
index 03dc8b4..72f2ac4 100644
--- a/core/themes/bartik/bartik.theme
+++ b/core/themes/bartik/bartik.theme
@@ -9,34 +9,6 @@
 use Drupal\Core\Template\Attribute;
 
 /**
- * Implements hook_preprocess_HOOK() for HTML document templates.
- *
- * Adds body classes if certain regions have content.
- */
-function bartik_preprocess_html(&$variables) {
-  // Add information about the number of sidebars.
-  if (!empty($variables['page']['sidebar_first']) && !empty($variables['page']['sidebar_second'])) {
-    $variables['attributes']['class'][] = 'layout-two-sidebars';
-  }
-  elseif (!empty($variables['page']['sidebar_first'])) {
-    $variables['attributes']['class'][] = 'layout-one-sidebar';
-    $variables['attributes']['class'][] = 'layout-sidebar-first';
-  }
-  elseif (!empty($variables['page']['sidebar_second'])) {
-    $variables['attributes']['class'][] = 'layout-one-sidebar';
-    $variables['attributes']['class'][] = 'layout-sidebar-second';
-  }
-  else {
-    $variables['attributes']['class'][] = 'layout-no-sidebars';
-  }
-
-  if (!empty($variables['page']['featured_top'])) {
-    $variables['attributes']['class'][] = 'has-featured-top';
-  }
-
-}
-
-/**
  * Implements hook_preprocess_HOOK() for page templates.
  */
 function bartik_preprocess_page_title(&$variables) {
diff --git a/core/themes/bartik/layout.js b/core/themes/bartik/layout.js
index e69de29..63b1bb9 100644
--- a/core/themes/bartik/layout.js
+++ b/core/themes/bartik/layout.js
@@ -0,0 +1,49 @@
+/**
+ * @file
+ * Adds body classes if certain regions have content.
+ */
+
+(function (Drupal) {
+
+  "use strict";
+
+  function BartikLayoutUpdate() {
+    function regionHasContent(regionName) {
+      var element = document.getElementsByClassName(regionName)[0];
+      return element && element.childElementCount > 0;
+    }
+
+    var bodyClassList = document.getElementsByTagName('body')[0].classList;
+    var hasFirstSidebar = regionHasContent('region-sidebar-first');
+    var hasSecondSidebar = regionHasContent('region-sidebar-second');
+
+    // Add information about the number of sidebars.
+    if (hasFirstSidebar && hasSecondSidebar) {
+      bodyClassList.add('layout-two-sidebars');
+    }
+    else if (hasFirstSidebar) {
+      bodyClassList.add('layout-one-sidebar');
+      bodyClassList.add('layout-sidebar-first');
+    }
+    else {
+      bodyClassList.add('layout-one-sidebar');
+      bodyClassList.add('layout-sidebar-second');
+    }
+
+    if (regionHasContent('featured-top')) {
+      bodyClassList.add('has-featured-top');
+    }
+  }
+
+  // Update the Bartik layout <body> classes as soon as this is executed.
+  BartikLayoutUpdate();
+
+  Drupal.behaviors.bartikLayout = {
+    // Update the Bartik layout <body> classes again whenever behaviors are
+    // being attached, which signals that new content may have arrived.
+    attach: function () {
+      BartikLayoutUpdate();
+    }
+  }
+
+})(Drupal);
