Skip to content

vite: let pages mount components added after load - #291

Closed
elgreg wants to merge 1 commit into
clay:masterfrom
slategroup:vite-mount-roots
Closed

elgreg wants to merge 1 commit into
clay:masterfrom
slategroup:vite-mount-roots

Conversation

@elgreg

@elgreg elgreg commented Nov 13, 2026 •

Copy link
Copy Markdown

What this Does

Pages that inject server-rendered HTML later (e.g. restoring a truncated article) dispatch a bubbling clay:mount event from the container, and the bootstrap mounts the component elements at or under it. A caller that needs to wait passes an object as detail and gets a promise on detail.mounted.

An event rather than a window global, so component code needs no guard in edit mode or under clay compile, where nothing listens.

Each element is mounted at most once, so repeated or overlapping events are safe, and each later run gets its own performance measure. The page-load mount behaves exactly as before.

Why

The bootstrap mounts only the components that are in the DOM when it runs. Pages that add server-rendered component HTML after load had no supported way to run those components' client.js. Examples are infinite scroll, lazy-loaded recirculation modules, server-fetched modals, and content restored after load.

Where should a reviewer start?

  1. CLAY-VITE.md, section "Mounting components added after page load: the clay:mount event". It covers the contract: what mounts, detail.mounted, mounting at most once, the edit-mode and clay compile behavior, and the timing.
  2. lib/cmd/vite/generate-bootstrap.js:
    • mountComponentModules(roots): it now looks only at or under the given root, and skips elements already in the mountedEls WeakSet. Called with no argument it does the same as before.
    • The clay:mount listener at the end of the generated bootstrap.
    • The performance marks: page load keeps clay-components, and later runs get clay-components-2, -3, and so on.
  3. lib/cmd/vite/controller-deps.test.js, describe('the clay:mount event'), with seven tests:
    • scope to the root
    • no detail object passed
    • dispatched from document
    • never mounting twice, three ways: the same root, a root inside another, a root already on the page
    • overlapping events
    • a DS.get dependency outside the root
    • performance marks for each run

Manual testing steps

In a site using clay vite, linked to this branch:

  1. Load a page. The page-load mount logs [clay vite] mounted N/M components as before.
  2. Fetch a component's rendered HTML (e.g. /_components/<name>/instances/<id>.html), insert it into a container, and dispatch clay:mount from that container.
  3. The new components' client.js runs, a second [clay vite] mounted … line logs only the new elements, and components already on the page are not mounted again.

@elgreg
elgreg force-pushed the vite-mount-roots branch 2 times, most recently from 1af0e73 to 1e278c7 Compare November 16, 2026 13:48
@jjpaulino

Copy link
Copy Markdown
Member

@codex review this

@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-17T00:17:08.720696Z 1e278c7 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.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 85.61%. remained the same — slategroup:vite-mount-roots into clay:master

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1e278c753e

ℹ️ 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".

Comment thread lib/cmd/vite/generate-bootstrap.js Outdated
Comment on lines +179 to +181
// Claimed before any module loads, so an overlapping run cannot take it too
if (claimedEls.get(el) === uri) return false;
claimedEls.set(el, uri);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Await in-flight mounts when handling overlapping events

When two clay:mount events cover an element whose module is still loading, the second run filters out the claimed element and resolves its detail.mounted promise before the controller runs. This violates the documented guarantee that the promise resolves once every element has mounted or failed, allowing callers to proceed against uninitialized components. Retain and await the existing per-element mount promise while still preventing a duplicate controller invocation.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will address.

Comment on lines +215 to +216
var uri = el.dataset.uri;
var m = CLAY_INSTANCE_KIND.exec(uri);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Invalidate pending mounts when an element's URI changes

In the newly supported element-reuse flow, changing data-uri and dispatching another event while the previous import is pending leaves both runs targeting the same element with different captured URIs. The new controller can mount first, after which the old controller still runs against the repurposed DOM—for example, a slow recirc controller initializes an element already mounted as video. Verify that the element still owns the same claim/generation immediately before invoking either controller path, including after the DS dependency wait.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Is a changing data-uri really likely?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have decided it really isn't - so I'll remove support for it and that should also remove this issue.

function isOnPage(name) {
if (!onPage) {
onPage = {};
document.querySelectorAll(COMPONENT_SELECTOR).forEach(function(el) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include scoped shadow-tree components in dependency discovery

When the documented composed: true event is dispatched from inside an open shadow root, dependencies that exist only in that shadow tree are invisible to document.querySelectorAll. Consequently, isOnPage(dep) returns false even for components found by the scoped mount, and a fast legacy controller runs without waiting for its slower dependency to register. Its DS.get lookup can fail, and the retained claim prevents recovery on a later event. Include components from the mounting scope or its tree when building the dependency-presence set.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think I'll just drop the promise of addressing legacy DS in shadow tree - for this to be an issue, it needs a legacy DS controller and its dependency to both be inside the same shadow tree and only in there.

Pages that inject server-rendered HTML later (e.g. restoring a truncated
article) dispatch a clay:mount event from the container, and the bootstrap
mounts the component elements at or under it. A caller that needs to wait
passes an object as detail and gets a promise on detail.mounted.

An event rather than a window global, so component code needs no guard in
edit mode or under clay compile, where nothing listens. The listener is on
window in the capture phase, so the event is caught whether or not it
bubbles, and from document or window (both mean the whole page). Components
inside a shadow root are not supported.

Each element is mounted at most once, so repeated events are safe, and an
event that overlaps another waits for the mounts that one is still running.
An element whose module failed to load is released so a later event can
retry it; a controller that ran and threw is not run again.

Mounts under a root skip the preload pass and only walk the document when a
controller has DS dependencies, so one slow chunk or a long page does not
hold them up. Only the page-load run goes on the performance timeline; its
mount behaves as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@elgreg elgreg closed this Nov 17, 2026
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