From 2a450cbfaaa5aef5bd4e410455bb49df74c5b177 Mon Sep 17 00:00:00 2001 From: arturovt Date: Sun, 15 Nov 2026 13:16:15 +0200 Subject: [PATCH] fix(core): throw NG0512 instead of TypeError when hydration text node is missing Given this template: ```html
{{ value }}
``` During hydration, Angular looks for the text node that the server rendered inside the ``. 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. --- adev/src/content/reference/errors/NG0512.md | 17 +++++++++++++++++ adev/src/content/reference/errors/overview.md | 1 + goldens/public-api/core/errors.api.md | 2 ++ packages/core/src/errors.ts | 1 + packages/core/src/render3/instructions/text.ts | 15 +++++++++++++-- 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 adev/src/content/reference/errors/NG0512.md diff --git a/adev/src/content/reference/errors/NG0512.md b/adev/src/content/reference/errors/NG0512.md new file mode 100644 index 00000000000..7b1571483f3 --- /dev/null +++ b/adev/src/content/reference/errors/NG0512.md @@ -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. diff --git a/adev/src/content/reference/errors/overview.md b/adev/src/content/reference/errors/overview.md index 71d2bff788c..e4f0cc2158a 100644 --- a/adev/src/content/reference/errors/overview.md +++ b/adev/src/content/reference/errors/overview.md @@ -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) | diff --git a/goldens/public-api/core/errors.api.md b/goldens/public-api/core/errors.api.md index f4fc900edae..c2cfc8f3ce1 100644 --- a/goldens/public-api/core/errors.api.md +++ b/goldens/public-api/core/errors.api.md @@ -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, diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.ts index 214c727f0ff..a8eb0ee9796 100644 --- a/packages/core/src/errors.ts +++ b/packages/core/src/errors.ts @@ -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, diff --git a/packages/core/src/render3/instructions/text.ts b/packages/core/src/render3/instructions/text.ts index 7721eecb681..dbee18be49d 100644 --- a/packages/core/src/render3/instructions/text.ts +++ b/packages/core/src/render3/instructions/text.ts @@ -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(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; }