fix(core): throw NG0512 instead of TypeError when hydration text node is missing

Given this template:

```html
<main>
  <span>{{ value }}</span>
</main>
```

During hydration, Angular looks for the text node that the server rendered
inside the `<span>`. If the HTML from the server was changed and this text
node is missing, dev mode throws a clear NG0500 error. But this check only
runs in dev mode.

Prior to this fix, production continued with the missing text node and failed
on the first update of that binding with a raw error that doesn't say what
went wrong:

```
TypeError: Cannot set properties of null (setting 'nodeValue')
```

With this fix, production throws NG0512 during hydration instead.

Note that static text behaves differently. Nothing ever updates it, so it
hydrated without an error before this fix and left a gap in the DOM. It now
throws as well.
This commit is contained in:
arturovt
2026-11-16 14:12:24 -08:00
committed by Alon Mishne
parent 277de596fe
commit 2a450cbfaa
5 changed files with 34 additions and 2 deletions
@@ -0,0 +1,17 @@
# Hydration Missing Text
This error is similar to the [error NG0500](errors/NG0500), but it means Angular's hydration process expected a text node at a specific spot in the DOM and found nothing there. Check the [error NG0500](errors/NG0500) page for causes related to direct DOM manipulation.
## A common cause: the server HTML changed before hydration
Hydration reuses the text nodes that server-side rendering produced. If something removes or merges those text nodes before the browser runs the application, the node Angular looks for is gone.
Common sources are an HTML minifier, a proxy or CDN that rewrites the response, a browser extension, or application code that writes text with `innerHTML` outside of Angular.
## Debugging the error
See the [error NG0500](errors/NG0500) for tips on debugging causes related to direct DOM manipulation.
Compare the HTML that the server sent with the HTML that the browser received. Use the page source rather than the element inspector, because the inspector shows the DOM after hydration already ran. If the text is present in the server response but missing in the browser, look for a tool in between that rewrites the HTML.
If you do not control the HTML for a component, you can add the `ngSkipHydration` attribute to its host node as a last resort. This disables hydration for that component and its children, and should be considered a bug that needs to be fixed.
@@ -27,6 +27,7 @@
| `NG0506` | [NgZone remains unstable](errors/NG0506) |
| `NG0507` | [HTML content was altered after SSR](errors/NG0507) |
| `NG0509` | [Hydration Missing Node on Path](errors/NG0509) |
| `NG0512` | [Hydration Missing Text](errors/NG0512) |
| `NG0600` | [Signal write in a disallowed context](errors/NG0600) |
| `NG0602` | [Disallowed function call inside reactive context](errors/NG0602) |
| `NG0750` | [@defer dependencies failed to load](errors/NG0750) |
+2
View File
@@ -68,6 +68,8 @@ export const enum RuntimeErrorCode {
// (undocumented)
HYDRATION_MISSING_SIBLINGS = -501,
// (undocumented)
HYDRATION_MISSING_TEXT = -512,
// (undocumented)
HYDRATION_NODE_MISMATCH = -500,
// (undocumented)
HYDRATION_STABLE_TIMEDOUT = -506,
+1
View File
@@ -92,6 +92,7 @@ export const enum RuntimeErrorCode {
HYDRATION_MISSING_NODE_ON_PATH = -509,
PARENT_NODE_NOT_FOUND = 510,
EVENT_REPLAY_CONTRACT_ALREADY_CONSUMED = 511,
HYDRATION_MISSING_TEXT = -512,
// Signal Errors
SIGNAL_WRITE_FROM_ILLEGAL_CONTEXT = -600,
+13 -2
View File
@@ -5,6 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {RuntimeError, RuntimeErrorCode} from '../../errors';
import {validateMatchingNode} from '../../hydration/error_handling';
import {locateNextRNode} from '../../hydration/node_lookup_utils';
import {canHydrateNode, markRNodeAsClaimedByHydration} from '../../hydration/utils';
@@ -83,10 +84,20 @@ function locateOrCreateTextNodeImpl(
// Hydration mode, looking up an existing element in DOM.
const hydrationInfo = lView[HYDRATION]!;
const textNative = locateNextRNode(hydrationInfo, tView, lView, tNode) as RText;
const textNative = locateNextRNode<RText>(hydrationInfo, tView, lView, tNode);
ngDevMode && validateMatchingNode(textNative, Node.TEXT_NODE, null, lView, tNode);
ngDevMode && markRNodeAsClaimedByHydration(textNative);
ngDevMode && markRNodeAsClaimedByHydration(textNative!);
// `validateMatchingNode` above only runs in dev mode. Without this check,
// production throws a raw TypeError when the caller uses the missing text.
if (textNative == null) {
throw new RuntimeError(
RuntimeErrorCode.HYDRATION_MISSING_TEXT,
ngDevMode &&
'During hydration Angular expected a text node at this location, but no matching DOM node was found. This usually means the client-rendered DOM no longer matches the server-rendered HTML.',
);
}
return textNative;
}