diff --git a/core/modules/system/src/Controller/SystemController.php b/core/modules/system/src/Controller/SystemController.php
index f744936dfe9995494305b59fb66fffb4d011c59f..73180a0876180f094f92d80667b510ab976cdeca 100644
--- a/core/modules/system/src/Controller/SystemController.php
+++ b/core/modules/system/src/Controller/SystemController.php
@@ -363,13 +363,19 @@ public static function setLinkActiveClass(array $element, array $context) {
       $pos_current_path = strpos($element['#markup'], $search_key_current_path, $offset);
       $pos_front = strpos($element['#markup'], $search_key_front, $offset);
 
-      // Determine which of the two values matched: the exact path, or the
-      // <front> special case.
+      // Determine which of the two values is the next match: the exact path, or
+      // the <front> special case.
       $pos_match = NULL;
-      if ($pos_current_path !== FALSE) {
+      if ($pos_front === FALSE) {
         $pos_match = $pos_current_path;
       }
-      elseif ($context['front'] && $pos_front !== FALSE) {
+      elseif ($pos_current_path === FALSE) {
+        $pos_match = $pos_front;
+      }
+      elseif ($pos_current_path < $pos_front) {
+        $pos_match = $pos_current_path;
+      }
+      else {
         $pos_match = $pos_front;
       }
 
diff --git a/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php b/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
index ab227a039309d28dc117542f260e52c1f96a6bdc..4e6e7917489842e61373579f6487493fc36a5d02 100644
--- a/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
+++ b/core/modules/system/tests/src/Unit/Controller/SystemControllerTest.php
@@ -305,6 +305,25 @@ public function providerTestSetLinkActiveClass() {
       2 => ['#markup' => '<a data-drupal-link-system-path="&lt;front&gt;" class="active">Once</a> <a data-drupal-link-system-path="&lt;front&gt;" class="active">Twice</a>'],
     ];
 
+    // Test cases to verify that the 'active' class is added when on the front
+    // page, and there are two different kinds of matching links on the page:
+    // - the matching path (the resolved front page path)
+    // - the special matching path ('<front>')
+    $front_special_link = '<a data-drupal-link-system-path="&lt;front&gt;">Front</a>';
+    $front_special_link_active = '<a data-drupal-link-system-path="&lt;front&gt;" class="active">Front</a>';
+    $front_path_link = '<a data-drupal-link-system-path="myfrontpage">Front Path</a>';
+    $front_path_link_active = '<a data-drupal-link-system-path="myfrontpage" class="active">Front Path</a>';
+    $data[] = [
+      0 => ['#markup' => $front_path_link . ' ' . $front_special_link],
+      1 => ['path' => 'myfrontpage', 'front' => TRUE, 'language' => 'en', 'query' => []],
+      2 => ['#markup' => $front_path_link_active . ' ' . $front_special_link_active],
+    ];
+    $data[] = [
+      0 => ['#markup' => $front_special_link . ' ' . $front_path_link],
+      1 => ['path' => 'myfrontpage', 'front' => TRUE, 'language' => 'en', 'query' => []],
+      2 => ['#markup' => $front_special_link_active . ' ' . $front_path_link_active],
+    ];
+
     return $data;
   }