Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/@ember/-internals/glimmer/lib/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,19 @@ export function renderComponent(
* We can only replace the inner HTML the first time.
* Because destruction is async, it won't be safe to
* do this again, and we'll have to rely on the above destroy.
*
* Use a structural check instead of `into instanceof Element` so the
* renderer doesn't depend on the `Element` constructor being a
* global. Browsers always have it; Node / Bun servers running with a
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You're likely going to need to simulate a browser env anyway

Which reminds me! This RFC for Deprecation targeting v8 may affect you

emberjs/rfcs#1178

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.

Thanks for the RFC heads-up I really appreciate it. Our adapter is using all three of simple-dom + hasDOM: true + isInteractive: false, so this would have ambushed us hard in v8. Will plan the migration to a happy-dom-shaped pattern (matching vite-ember-ssr) before we do anything user-facing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

well, it's not an accepted RFC yet. the community may revolt agaist this idea. haha

in general tho, I think all SSR/SSG environments should be no different from browser tho, becaues otherwise you infect all library code like a virus, special casing the existence or non-existence of various things everywhere (window, etc (fastboot was really bad at this, for example))

Copy link
Copy Markdown
Author

@alexkahndev alexkahndev May 3, 2026

Choose a reason for hiding this comment

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

Strong agreement on the underlying stance and worth saying that we've watched four other frameworks land in the same place from different angles, which is some signal that the principle is sound.

Quick tour through what AbsoluteJS sees across our existing adapters:

React's SSR is string-based (renderToReadableStream). The "is this SSR?" question is bounded to a single boundary in the user's mental model: effects don't run on the server. Render code is identical on both sides. Users essentially never write if (typeof window !== 'undefined') for anything outside an effect and even there, the convention is to put it inside useEffect/useLayoutEffect which only fires client-side. The split is in when things run, not what code runs.

Vue lands almost the same way string-based via renderToWebStream, lifecycle hooks like onMounted are client-only by contract. Vue's small improvement on the React shape is onServerPrefetch, an additive primitive ("do this on the server") rather than a defensive check ("am I on the server?"). Better posture against the virus problem because it inverts the polarity. Svelte moves the boundary to compile time entirely. The Svelte compiler emits components twice, once as a string-concatenating function (SSR), once as DOM-mutating code (client). User-written .svelte files are identical for both; the compiler picks the
codegen. From a user's perspective, "am I in SSR or browser?" basically doesn't exist there's nothing to ask. The cost is the build pipeline knows about both modes; the user doesn't.

Svelte moves the boundary to compile time entirely. The Svelte compiler emits each component twice once as a string-concatenating function (SSR mode), once as DOM-mutating code (client mode). User-written .svelte files are identical for both; the
compiler picks the codegen. From a user's perspective, "am I in SSR or browser?" basically doesn't exist there's nothing to ask.

Angular is the one that maps closest to where this RFC takes Ember.
@angular/platform-server wraps Domino internally, so when user code does inject(DOCUMENT) or reads el.nativeElement it gets a real Document / Element instance regardless of side. There is no isFastBoot / isInteractive flag visible to component authors. Angular's user code is server-agnostic because the platform owns the simulated DOM, not the user.

The common thread every framework that successfully kept the "this is SSR" virus out of library code did it by eliminating the runtime question, not by exposing a flag for it. React/Vue split it on lifecycle phases. Svelte splits it at compile time. Angular pretends the DOM is always there. None of them ask user code or addon code "are you
running on the server?" at runtime, and none of them have the FastBoot-style ecosystem fragmentation.

Ember post-RFC ends up on Angular's branch of that tree happy-dom (or any DOM) installs Element/Node/Document, the renderer always assumes interactive mode, and user/addon code stops needing to ask. From outside Ember, that looks like the framework finally adopting the same architectural answer the rest of the ecosystem converged on years ago just by removing the affordance to ask the question, rather than by adding a new abstraction layer.

The empirical evidence across React, Vue, Svelte, and Angular is consistent: the runtime "am I in SSR?" question is the source of the virus. Removing the question by whichever mechanism a framework can is what stops the spread. That seems like a strong technical case for the direction even outside the specifics of isInteractive / hasDOM.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is a fantastic summary, but can you post in on the RFC as a comment? <3

* bare simple-dom Document do not, and would otherwise hit
* `ReferenceError: Element is not defined` here. The `'element' in
* into` test matches the existing `intoTarget()` helper above:
* `Cursor` has `element`; `Element` / `SimpleElement` do not. The
* `'innerHTML' in into` follow-up keeps the clearing scoped to the
* real-Element case (`SimpleElement` has no `innerHTML` setter).
*/
if (!existing && into instanceof Element) {
into.innerHTML = '';
if (!existing && !('element' in into) && 'innerHTML' in into) {
(into as Element).innerHTML = '';
}

/**
Expand All @@ -689,8 +699,11 @@ export function renderComponent(
*/
let renderTarget: IntoTarget = into;
if (existing?.glimmerResult) {
let parentElement =
into instanceof Element ? (into as unknown as SimpleElement) : (into as Cursor).element;
// Reuse the same `intoTarget()` shape used by the lower-level
// `BaseRenderer#render` path so all code that needs a parent
// Element from an `IntoTarget` agrees on the discriminator. As a
// bonus, this avoids the `globalThis.Element` dependency above.
let parentElement = intoTarget(into).element;
let firstNode = existing.glimmerResult.firstNode();
renderTarget = { element: parentElement, nextSibling: firstNode };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,48 @@ moduleFor(
assertHTML('');
this.assertStableRerender();
}

'@test renderComponent does not depend on a global Element constructor'(assert: Assert) {
// Stash and unset `globalThis.Element` so the previous
// `into instanceof Element` check would crash with
// `ReferenceError: Element is not defined`. After the structural-
// check fix, `renderComponent` should still resolve the parent
// element and render without consulting any global. This matters
// for non-browser hosts (Node / Bun servers, edge workers) that
// don't ship a DOM Element constructor by default — only the
// simple-dom node types they were given via `env.document`.
let saved = (globalThis as { Element?: unknown }).Element;
(globalThis as { Element?: unknown }).Element = undefined;

try {
let Foo = setComponentTemplate(precompileTemplate('Hello, world!'), templateOnly());

let owner = buildOwner({});
let manualDestroy: () => void;

run(() => {
let result = renderComponent(Foo, {
owner,
into: this.element,
});
manualDestroy = result.destroy;
this.component = {
...result,
rerender() {
// unused, but asserted against
},
};
});

assertHTML('Hello, world!');
assert.ok(true, 'renderComponent ran to completion with `globalThis.Element` undefined');

run(() => manualDestroy());
run(() => destroy(owner));
} finally {
(globalThis as { Element?: unknown }).Element = saved;
}
}
}
);

Expand Down
Loading