Skip to content

docs: stop hiding snippet fallback prematurely (:defined race)#260

Merged
adamziel merged 1 commit intotrunkfrom
docs-snippet-fallback-hide
May 3, 2026
Merged

docs: stop hiding snippet fallback prematurely (:defined race)#260
adamziel merged 1 commit intotrunkfrom
docs-snippet-fallback-hide

Conversation

@adamziel
Copy link
Copy Markdown
Collaborator

@adamziel adamziel commented May 3, 2026

Summary

Reports of "no snippets visible" on https://wordpress.github.io/php-toolkit/reference/html.html despite the page shipping 9 `` elements + 9 `<pre class="snippet-fallback">` static fallbacks.

Root cause: the CSS rule

php-snippet:defined .snippet-fallback { display: none; }

matches the moment `customElements.define('php-snippet', ...)` runs in Playground's module — before the snippet's shadow DOM has any content. In the gap between "custom element registered" and "shadow DOM populated with code + Run button," the fallback was already hidden and the widget hadn't drawn yet. On a slow client (or one where a later step in the Playground module fails) the user sees a permanent blank gap.

Fix: drop the `:defined` rule from `style.css` and hide the fallback explicitly from `page.js`'s `patchSnippet`, which only runs after `shadowRoot.querySelector('pre code')` returns a real node. By that point the snippet's UI is rendered on screen, so swapping the fallback out is safe. If the cross-origin Playground module never loads at all (CSP, adblock, slow network, no-JS), `patchSnippet` is never reached and the fallback stays visible — readers always see the code.

Verification (headless chromium screenshots)

Scenario Before After
Module reachable Custom element renders, fallback briefly invisible during shadow-DOM render Custom element renders + Run button; fallback hidden only after shadow DOM is populated
Module unreachable (`--host-rules="MAP playground.wordpress.net 127.0.0.1:1"`) Permanent blank gap because `:defined` matched the moment the module's first script ran Static `
` fallback stays visible, readers can read the code

87/87 snippets still match captured stdout. Asset version bumped (`20260503-script-unescape` → `20260503-fallback-explicit-hide`) so browser caches refresh.

Test plan

  • `Verify docs snippets` workflow passes.
  • After deploy, every reference page on https://wordpress.github.io/php-toolkit/reference/ shows code in every snippet — either as the interactive widget (when the module loads) or as the static `
    ` fallback (when it doesn't). No blank gaps.

🤖 Generated with Claude Code

Reports of "no snippets visible" on the live site, despite the page
shipping 9 <php-snippet> elements + 9 <pre class=\"snippet-fallback\">
blocks: the fallback was hidden by a CSS rule:

    php-snippet:defined .snippet-fallback { display: none; }

`:defined` matches the moment `customElements.define('php-snippet', ...)`
runs — i.e. immediately when Playground's php-code-snippet.js is parsed —
*before* the snippet's shadow DOM has any content. So in the gap between
"custom element registered" and "shadow DOM rendered with code + Run
button", the fallback was already gone and the widget hadn't drawn yet.
On a slow client (or one where a later step in the Playground module
fails) the user sees a permanent blank gap.

Fix: drop the `:defined` rule from style.css and hide the fallback
explicitly from page.js's patchSnippet, which only runs once
`shadowRoot.querySelector('pre code')` has actually returned a node.
By that point the snippet's UI is on screen, so swapping the fallback
out is safe. If the cross-origin module never loads, patchSnippet is
never reached and the fallback stays visible.

Verified by rendering the local site twice with chromium:
  * Playground module mapped to 127.0.0.1:1 (unreachable) → fallback
    <pre> visible for every snippet, code readable.
  * Module reachable → custom element renders the editable code area
    + Run button, fallback hidden.

Asset version bumped (20260503-script-unescape → 20260503-fallback-
explicit-hide) so caches refresh.

87/87 snippets still match captured stdout.
@adamziel adamziel merged commit 380a6f1 into trunk May 3, 2026
27 of 28 checks passed
@adamziel adamziel deleted the docs-snippet-fallback-hide branch May 3, 2026 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant