fix(angular-server): fallback to scoped+annotations when SSR DOM lacks attachShadow#30967
Conversation
|
@SebastianKohler is attempting to deploy a commit to the Ionic Team on Vercel. A member of the Team first needs to authorize it. |
|
@gnbm Are there any plans from the Ionic team to take a look at this PR? I see you are hard at work developing Angular 21 och 22 support for Ionic v9 due in Q3 2026, but does that include Angular SSR support? Angular SSR has not worked in Ionic since v8.2.2, which this PR attempts to fix. Btw, I love the fact that your focus for v9 is to "keep up" rather than implement new features 👍👍 |
|
Hey @SebastianKohler! Thanks for the PR. It looks fine, but a couple of concerns:
|
|
I tested the PR fallback against Ionic 8.8.13, which bundles Stencil 4.43.5. The server-side
The minified function maps to Angular’s Stencil 4.40+ contains the fix for the related non-shadow DOM traversal issue (the current test confirms A useful regression test would need to load the SSR result with Angular client hydration, assert that there are no page/console errors, and verify that the I published the exact verification setup here: https://github.com/SebastianKohler/ionic-angular-ssr-attachShadow-typeError/tree/codex/verify-pr-30967. It pins Ionic 8.8.13, which bundles Stencil 4.43.5, and applies the PR implementation locally. SSR completes successfully, but Angular client hydration still fails because Stencil’s annotations alter the expected node order. I’m sharing this reproduction and diagnosis so the Ionic team has a concrete starting point. I won’t be able to continue investigating the Angular/Stencil hydration interaction or develop the framework-level fix. Given the remaining client-side failure, I don’t think this PR can currently be considered a complete resolution of #29751. I’ll leave it to the maintainers to decide whether the server-side fallback has value independently or whether the PR should be closed. |
|
One important clarification to my previous comment: Angular client hydration is optional. I repeated the test after removing Without Angular client hydration, Angular replaces the server-rendered DOM during client bootstrap instead of reconciling it. Consequently, Angular does not encounter the Stencil hydration markers that caused the failure described in my previous comment. This means the PR appears to be a working fix for Angular SSR applications that do not enable The original reproduction has always included Therefore, if #29751 is scoped to the server-side error and SSR without Angular client hydration, the PR appears to resolve it. If Ionic intends to support Angular client hydration as well, that path still requires additional work. |
|
We derived a patch from this PR, that we use in production. Including it here in case it helps move this along. I really hope SSR works out of the box in Ionic v9 👍 package.json in Angular 21, we had But in Angular 22, those are defaulted, so we simplified it to The page does flicker between ssr to the hydrated version, but I suspect that is something in our own code that I need to dig into. |
|
@DavidBowdoin Is your browser console clean when Angular client hydration runs? In particular, do you see a |
|
@SebastianKohler Yes, both the browser console, and the node express console, are clean. |
ShaneK
left a comment
There was a problem hiding this comment.
I tested this manually and I think I found an issue, please let me know if I'm off base here
| ? { clientHydrateAnnotations: false } | ||
| : { | ||
| serializeShadowRoot: 'scoped', | ||
| clientHydrateAnnotations: true, |
There was a problem hiding this comment.
| clientHydrateAnnotations: true, | |
| clientHydrateAnnotations: false, |
I think this should be false rather than true. I ran renderToString on Stencil 4.43.5 (what main ships) with both values: with true the serializer injects <!--r.1-->/<!--r.2--> root hydrate markers as siblings right before <ion-button>, which are the exact nodes traced to Angular client hydration throwing hasAttribute is not a function on Angular 18. With false those markers are gone, and there's still only one native <button>, so the duplicate-internals symptom you originally needed true to avoid doesn't come back on 4.43.5 (that was a pre-4.40 Stencil bug that's since been fixed).
So false gives no server crash, no duplicate internals, and no hydration-breaking markers, which should work with provideClientHydration() across Angular versions rather than only without it. One caveat: I verified this at the Stencil serialization layer in Node, not with a live Angular hydration runtime, so it'd be worth confirming in the ng18 repro with provideClientHydration() enabled before merging.
There was a problem hiding this comment.
Thanks — I tested the suggested clientHydrateAnnotations: false change in a live Angular 18 client-hydration runtime, rather than only at the Stencil serialization layer.
The setup uses Angular 18.2.14 with provideClientHydration(), Ionic 8.8.13/Stencil 4.43.5, serializeShadowRoot: 'scoped', and no ngSkipHydration.
At the serialization layer, my result agrees with yours:
- SSR completes without the
attachShadowerror. - The
<!--r.*-->markers are gone. - The serialized HTML contains exactly one native
<button>.
However, the end-to-end browser result still fails:
- Angular throws
TypeError: s.hasAttribute is not a function. - An empty comment node still precedes
<ion-button>inside<ion-app>. <ion-button>remains hidden.- After client bootstrap, there is one
button.button-nativein the light DOM and another in the shadow root.
So the duplicate internals are not visible in the serialized HTML; the second native control is created during Stencil client bootstrap. A serializer-only button count does not catch that.
The exact verification setup is here: https://github.com/SebastianKohler/ionic-angular-ssr-attachShadow-typeError/tree/codex/verify-annotations-false-ng18.
I also investigated the Angular 22 application linked by @DavidBowdoin. Its current SSR HTML contains <ion-app ngskiphydration> while retaining Stencil’s <!--r.*--> markers. Angular therefore skips hydration for the entire Ionic subtree and recreates it on the client. That explains why its console can remain clean and may also explain the reported flicker; it does not exercise Angular hydration of the Stencil-annotated Ionic subtree.
Based on the Angular 18 runtime test, changing the value to false does not appear to resolve Angular client hydration end-to-end.
Issue number: resolves #29751
What is the current behavior?
In Angular SSR environments using a server DOM that does not implement native
attachShadow(for example Domino), Ionic server hydration can fail with:TypeError: this.attachShadow is not a functionThis occurs in the server hydrate path used by
@ionic/angular-serverwhen callinghydrateDocument(...)with the current default behavior.What is the new behavior?
This PR adds a capability check in
packages/angular-server/src/ionic-server-module.tsand applies a conditional fallback only whenattachShadowis unavailable:attachShadowis supported:clientHydrateAnnotations: false)attachShadowis not supported:serializeShadowRoot: 'scoped'clientHydrateAnnotations: trueThis keeps the current behavior for environments with native
attachShadow, while providing a compatible SSR path for Domino-like environments.Does this introduce a breaking change?
Other information
The change is intentionally scoped to
@ionic/angular-serverintegration logic and does not modify component behavior or the existingexcludeComponentslist.I could not run the full Ionic test/lint suite locally in my current Windows environment setup, so this PR relies on CI for full validation.
Note: this PR addresses the
attachShadowSSR crash path in@ionic/angular-serverby applying a conditional fallback.From upstream investigation, there is also a Stencil runtime fix in
>= 4.40.0related to non-shadow component patching (children/childNodes) that may affect Angular client hydration behavior. So full end-to-end resolution of all symptoms may also depend on the Stencil bump tracked separately.