Skip to content

Large publish forms got much slower in 6.44: a :has() rule added to the global header #16091

Done

Description

@jeremyvienney

Bug description

Since 6.44, on a large entry (a page builder with 30 sets, about 18,000 DOM nodes), every class change or node move anywhere in the publish form makes Chrome redo style or layout for the whole page: around 190 ms per change, against around 20 ms in 6.42.

Sorting is where you feel it first, because Draggable toggles classes and moves its placeholder at every swap: dragging a nested Replicator set now stutters, with frames of over 500 ms. Opening a Select field is slower too (about 400 ms for the frame, against about 200 ms in 6.42).

It comes from one rule added to resources/css/core/layout.css by #15039 (Forms 2):

.global-header-slot {
    /* When .global-header-slot has stuff */
    header:has(&:not(:empty)) {
        [data-global-header-search] {
            /* Hide everything except the search icon */
            > *:not(svg) {
                display: none;
            }
        }
    }
}

With only this rule disabled, 6.44 is back to 6.42 numbers. I don't know why this one triggers a full-page pass and its two neighbours in the same block don't.

Context: I'm building a CP addon, and on big forms the core sort had become unusable, so I ended up bypassing Draggable (no class written, no node moved during the gesture). That works for us, but the fix belongs in core: it would help every large entry, and even more on machines slower than mine.

As a front-end developer I love :has(), but with great power comes great responsibility. As far as I can see the slot is only filled by the forms layout (<Teleport to="#global-header-slot"> in pages/forms/Layout.vue), so a class or data-* attribute set on the header when something is teleported there could replace the :has(). I haven't measured that version.

How to reproduce

  1. Fresh statamic/statamic with statamic/cms 6.44.0. Add the blueprint and generate the entry (both below), then open /cp/collections/pages/entries/repro-30.
  2. Paste in the console:
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const sets = document.querySelectorAll('[data-replicator-set]');
const el = sets[sets.length >> 1];
for (let i = 0; i < 5; i++) {
  await sleep(500);
  const t = performance.now();
  el.classList.toggle('foo');      // a class no rule mentions
  getComputedStyle(el).opacity;    // force style
  console.log((performance.now() - t).toFixed(1), 'ms');
}
  1. Disable the rule and run the loop again:
const off = (needle) => {
  const walk = (list) => { for (const r of list) { if (r.selectorText?.includes(needle)) r.selectorText = 'x-off'; if (r.cssRules) walk(r.cssRules); } };
  for (const s of document.styleSheets) try { walk(s.cssRules); } catch {}
};
off(':not(:empty)) [data-global-header-search] > :not(svg)');

What I get (Chrome 157, Apple M1 Max, same entry, 5 runs each):

6.42.0 6.44.0 6.44.0, rule disabled
Class toggled on a set 10–25 ms 174–207 ms 15–32 ms
Set moved inside its list 23–36 ms 178–212 ms 27–45 ms

Or simply drag a nested set over two siblings with a performance trace running: the same scripted drag takes 1.7 s in 6.42 and 2.9 s in 6.44.

Blueprint and entry generator

resources/blueprints/collections/pages/page.yaml:

title: Page
tabs:
  main:
    display: Main
    sections:
      -
        fields:
          -
            handle: title
            field:
              type: text
              required: true
              validate:
                - required
          -
            handle: page_builder
            field:
              type: replicator
              display: 'Page builder'
              collapse: false
              sets:
                main:
                  display: Sets
                  sets:
                    section:
                      display: Section
                      fields:
                        -
                          handle: heading
                          field:
                            type: text
                            display: Heading
                        -
                          handle: intro
                          field:
                            type: textarea
                            display: Intro
                        -
                          handle: visible
                          field:
                            type: toggle
                            display: Visible
                        -
                          handle: layout
                          field:
                            type: select
                            display: Layout
                            options:
                              left: Left
                              right: Right
                              center: Center
                        -
                          handle: items
                          field:
                            type: replicator
                            display: Items
                            collapse: false
                            sets:
                              main:
                                display: Items
                                sets:
                                  item:
                                    display: Item
                                    fields:
                                      -
                                        handle: label
                                        field:
                                          type: text
                                          display: Label
                                      -
                                        handle: body
                                        field:
                                          type: textarea
                                          display: Body
  sidebar:
    display: Sidebar
    sections:
      -
        fields:
          -
            handle: slug
            field:
              type: slug
              localizable: true
              validate: 'max:200'

generate-entries.php (run from the site root):

<?php
// Usage (from the site root): php generate-entries.php
// Writes content/collections/pages/repro-10.md and repro-30.md:
// a page_builder replicator of N "section" sets, each with 6 to 9 nested "item" sets.
foreach ([10, 30] as $n) {
    $y = "---\nid: repro-$n\nblueprint: page\ntitle: 'Repro $n sets'\npage_builder:\n";
    for ($i = 1; $i <= $n; $i++) {
        $y .= "  -\n    id: s$n-$i\n    type: section\n    enabled: true\n"
            ."    heading: 'Section $i'\n    intro: 'Intro text for section $i, long enough to wrap on two lines in the publish form.'\n"
            ."    visible: true\n    layout: left\n    items:\n";
        for ($j = 1; $j <= 6 + $i % 4; $j++) {
            $y .= "      -\n        id: s$n-$i-$j\n        type: item\n        enabled: true\n"
                ."        label: 'Item $i.$j'\n        body: 'Body of item $i.$j.'\n";
        }
    }
    file_put_contents(__DIR__."/content/collections/pages/repro-$n.md", $y."---\n");
    echo "repro-$n.md\n";
}

Environment

Laravel Version: 13.44.1
PHP Version: 8.4.23
Statamic Version: 6.44.0 Solo
Addons: 0

Fresh statamic/statamic install, no addon. Chrome 157 on macOS (Apple M1 Max). Not tested in Safari or Firefox.

Additional details

Related: #13385. #15679 (6.39) already rewrote some :has() selectors for the same reason; this looks like the same kind of problem coming back through a newer rule.

Side note, older and much smaller: two rules that put [class*=…] inside a :has() (resources/css/elements/base.css, [class*="focus-within"]:has(…), and the body:has(… [class*="max-["] …) rule in components/global-header/Header.vue) make every class change cost 10–25 ms instead of 4–5 ms on the same entry (measured in 6.42, so not new). Happy to open a separate issue if that's useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions