Dev.to WebDev 🛠 Dev 👁 0 📖 4 min read

Your React accordion may be hiding answers from Google (and forceMount alone won't fix it)

I build Candidate Falcon, a practice site for people facing hiring assessments and one-way video interviews. It's a React app that we prerender to static HTML at build time, and most of its pages are written for search:

I build Candidate Falcon, a practice site for people facing hiring assessments and one-way video interviews. It's a React app that we prerender to static HTML at build time, and most of its pages are written for search: guides, FAQs, pricing.

During an audit of what crawlers actually receive from each page, the FAQ page stood out. It had 22 questions, each with its answer inside a Radix accordion (the shadcn/ui one). In the browser the page looked complete. The prerendered HTML had all 22 questions and none of the 22 answers.

Why the answers were missing

Radix only mounts the content of an accordion item while the item is open. A closed AccordionContent renders nothing: no node, no text. So when the page is rendered on the server, or by a crawler that doesn't click anything, the answers aren't there.

Two things made this worse than it sounds:

  • The page also emitted FAQPage JSON-LD with all 22 answers, built from the same data. So the structured data described text that wasn't in the HTML. Google's structured data guidelines ask you not to mark up content that isn't visible to readers of the page. (FAQ rich results are now limited to well-known government and health sites anyway, so this wasn't about rich results. It was about the answers being indexable at all.)
  • "Hidden" and "absent" are different things. Google has said for years that content tucked into tabs or accordions for usability still gets indexed, as long as it's in the page. Content that only exists after a click is a different case: there's nothing to index.

The quickest way to check your own pages is to fetch the HTML the server sends and search for a sentence that only appears inside a closed panel:

curl -s https://example.com/faq/ | grep -c "a sentence from inside a closed answer"

If it prints 0, the answer isn't in the HTML.

The obvious fix: forceMount

Radix has a prop for exactly this. forceMount keeps the content mounted whether the item is open or closed:

<AccordionContent forceMount className="...">
  {answer}
</AccordionContent>

After this change the answers were in the HTML. They were also all visible, which wasn't the plan.

The catch: with forceMount, you do the hiding

Normally Radix hides a closed panel by giving it the hidden attribute. With forceMount, it doesn't. In the collapsible source, the content keeps an internal isPresent flag that starts from the present prop, and present is always true when you force-mount. The hidden attribute is computed from open || isPresent, so it's never set.

Here's one of our closed answers in the built HTML (attributes trimmed):

<div data-state="closed" role="region" data-slot="accordion-content" class="...">

data-state="closed", but no hidden. Without extra CSS, every answer renders expanded, both in the server HTML and after hydration.

The fix is to hide closed panels yourself, keyed on data-state. Our first version was a rule scoped to the FAQ page:

.faq-page [data-slot="accordion-content"][data-state="closed"] {
  display: none;
}

Later we moved it into the shared shadcn component, so any page that force-mounts gets it for free:

function AccordionContent({ className, children, ...props }) {
  return (
    <AccordionPrimitive.Content
      data-slot="accordion-content"
      className={cn(
        "data-[state=closed]:animate-accordion-up data-[state=open]:animate-accordion-down overflow-hidden text-sm",
        // Force-mounted content stays in the server HTML, so it must hide itself when closed.
        props.forceMount && "data-[state=closed]:hidden",
      )}
      {...props}
    >
      <div className={cn("pt-0 pb-4", className)}>{children}</div>
    </AccordionPrimitive.Content>
  );
}

display: none keeps the text in the DOM and out of view, which is the "hidden, not absent" state we want. Opening and closing still work, because Radix flips data-state on click.

A test that reads the built HTML

A unit test that renders the component in JSDOM would have passed the whole time: the bug only shows in the prerendered output. So the regression test reads dist/faq/index.html after the build and checks it against the same data the page uses (trimmed):

const items = faqGroups.flatMap((group) => group.items);
const html = readFileSync(join(__dirname, '..', 'dist', 'faq', 'index.html'), 'utf8');
const document = new JSDOM(html).window.document;

it('keeps five groups and 22 ordered answers in the HTML body', () => {
  const contents = [...document.querySelectorAll('main [data-slot="accordion-content"]')];
  expect(contents).toHaveLength(22);

  for (const [index, item] of items.entries()) {
    const content = contents[index];
    expect(content.getAttribute('data-state')).toBe('closed');
    for (const section of item.sections) {
      if (section.type === 'paragraph') {
        expect(content.textContent).toContain(section.text);
      } else {
        for (const bullet of section.items) expect(content.textContent).toContain(bullet);
      }
    }
  }
});

A second test parses the JSON-LD on the same page and checks that the FAQPage questions and answers match the visible ones, so the two can't drift apart again.

When not to force-mount

Our pricing page had the same problem: the plan details lived in closed accordions. There we didn't force-mount. We added a short, always-visible summary of each plan and kept the accordions for the long detail. If the hidden content is the core of the page, a visible summary is often better than a hidden wall of text.

Force-mounting fits when:

  • the hidden content is short to medium (FAQ answers, specs, footnotes)
  • you prerender or server-render, and you care about what's in the first HTML response
  • you control the CSS for the closed state

Checklist

  1. Fetch the page's HTML with curl and grep for text from a closed panel.
  2. If it's missing, add forceMount to the content component.
  3. Hide closed panels yourself with [data-state="closed"] { display: none; } or Tailwind's data-[state=closed]:hidden. Radix won't add hidden for you.
  4. Test the built HTML, not just the component.
  5. If you emit FAQ structured data, check it against the visible answers.

I wrote this with the help of an AI assistant, working from the project's commits and code. The snippets come from the repository, trimmed for length.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.