Skip to content

🍕 vite: mount components added after page load - #294

Merged
jjpaulino merged 2 commits into
masterfrom
jordan/mount-added-components
Nov 19, 2026
Merged

jjpaulino merged 2 commits into
masterfrom
jordan/mount-added-components

Conversation

@jjpaulino

@jjpaulino jjpaulino commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Lets the Vite bootstrap mount components whose HTML is added after page load, in two ways:

  • clay:mount event (always on): dispatch it from an element that holds new HTML, and the bootstrap mounts the components at or under it. Pass an object as detail to get a promise on detail.mounted.
  • autoMount (opt-in, autoMount: true in claycli.config.js): a MutationObserver mounts component elements as they are added, with no event.

Both go through one mount path. Each element mounts once, and a run that finds one still mounting waits for it. An element whose module failed to load, or whose legacy controller had no DS yet, can be tried again. An element that is off the page by the time its module (or a legacy controller's dependencies) is ready is skipped, and mounts if it comes back.

Page load keeps the same preload barrier and clay-components measure. One change there: a component removed by another component's mount before its turn is now skipped; before, it was mounted off the page.

No issue: pages need this for infinite scroll, lazy recirculation, server-fetched modals and articles restored after load.

Why are we doing this? Any context or related work?

Under clay vite the bootstrap mounts only what is on the page when it runs. Pages that add server-rendered component HTML later had no supported way to run those components' client.js.

Where should a reviewer start?

mountEls in the runtime in lib/cmd/vite/generate-bootstrap.js, used by page load, the event and autoMount. Then the CLAY-VITE.md section "Components added after page load".

Manual testing steps?
  • npm test: 32 suites, 737 tests, lint clean. Each new rule has a test that fails when its code is removed.
  • The runtime in real Chrome (real DOM, events and MutationObserver), 20 scenarios:
    • overlapping events
    • a data-uri changed while its module loads
    • events from document, window or a detached node
    • autoMount on inserted HTML, HTML inserted and removed in one task, and moved elements
    • HTML a component inserts while it mounts
    • page-load components mounting once
    • an element removed while its module or a legacy controller's dependencies load, put back before or after the load
    • on page load, a component removed by another component's mount (master mounts it off the page)
  • sites' bootstrap generated with autoMount off and on. Both parse, and they differ only by the _clayAutoMount line. The edit-mode bootstrap has no mount runtime.
Screenshots

None.

Additional Context

  • autoMount is off by default. Code that copies a component element (cloneNode) or inserts component HTML and then mounts it itself would mount it twice. In sites, sticky-container (its newsletter copy) and subscription-form-gift-recipient do this; drop those mount calls before turning it on.
  • In sites, every component element removed during a mount is gone for good or replaced by a copy, so the page-load change has no visible effect there.
  • Not in this PR: telling components when their element is removed (cleanup).

A clay:mount event from an element mounts the components at or under it
(detail.mounted is a promise for when they are done), and autoMount: true
in claycli.config.js mounts added component elements with a
MutationObserver. Both share one mount path: each element mounts once, a
run waits for elements another run is still mounting, and a failed module
load can be retried. The page-load mount is unchanged.
@coveralls

coveralls commented Nov 17, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 85.722% (+0.01%) from 85.71% — jordan/mount-added-components into master

@jjpaulino jjpaulino self-assigned this Nov 17, 2026
@jjpaulino
jjpaulino marked this pull request as ready for review November 17, 2026 16:05
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T17:23:46.775500Z 6f6eb2b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@elgreg elgreg left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works well for me locally. Claude had one nit, but it doesn't apply to us as we've managed to ditch DS

 Mounts elements removed while their module loads (line 206). The "is it still on the page" check (isConnected) only runs when an element is claimed. An infinite-scroll item or modal removed before its module loads still mounts on the detached node and leaks listeners and timers. The fix is cheap: check again just before mod(el) / DS.get.

A run claimed an element while it was on the page, then mounted it once its
module (or a legacy controller's dependencies) loaded, even if it had been
removed by then. Check again right before mounting: skip it, and release it
so it mounts if it comes back. Page load gets the same check.
@jjpaulino

Copy link
Copy Markdown
Member Author

Thanks for trying it. That nit applies without DS too: a clay:mount or autoMount run has no preload barrier, so the first time a component shows up its module can still be loading when the element is removed, and mod(el) then ran on the detached node. Put back and sent clay:mount again, it did not mount, because it already counted as mounted.

Fixed in 6f6eb2b: the runtime checks again right before mod(el) and DS.get, skips an element that is off the page, and releases it, so it mounts if it comes back (on the next clay:mount, or on its own with autoMount). Page load gets the same check; master also mounted removed elements there. If your code takes a component element off the page and puts that same element back later, send clay:mount after putting it back.

@jjpaulino

Copy link
Copy Markdown
Member Author

@codex review this once more.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 6f6eb2be2d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@jjpaulino
jjpaulino merged commit 78c88cb into master Nov 19, 2026
1 check passed
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.

3 participants