MCPcopy Create free account
hub / github.com/angular/angular / retrieveHydrationInfoImpl

Function retrieveHydrationInfoImpl

packages/core/src/hydration/utils.ts:122–205  ·  view source on GitHub ↗
(
  rNode: RElement,
  injector: Injector,
  isRootView = false,
)

Source from the content-addressed store, hash-verified

120let _retrieveHydrationInfoImpl: typeof retrieveHydrationInfoImpl = () => null;
121
122export function retrieveHydrationInfoImpl(
123 rNode: RElement,
124 injector: Injector,
125 isRootView = false,
126): DehydratedView | null {
127 let nghAttrValue = rNode.getAttribute(NGH_ATTR_NAME);
128 if (nghAttrValue == null) return null;
129
130 // For cases when a root component also acts as an anchor node for a ViewContainerRef
131 // (for example, when ViewContainerRef is injected in a root component), there is a need
132 // to serialize information about the component itself, as well as an LContainer that
133 // represents this ViewContainerRef. Effectively, we need to serialize 2 pieces of info:
134 // (1) hydration info for the root component itself and (2) hydration info for the
135 // ViewContainerRef instance (an LContainer). Each piece of information is included into
136 // the hydration data (in the TransferState object) separately, thus we end up with 2 ids.
137 // Since we only have 1 root element, we encode both bits of info into a single string:
138 // ids are separated by the `|` char (e.g. `10|25`, where `10` is the ngh for a component view
139 // and 25 is the `ngh` for a root view which holds LContainer).
140 const [componentViewNgh, rootViewNgh] = nghAttrValue.split('|');
141 nghAttrValue = isRootView ? rootViewNgh : componentViewNgh;
142 if (!nghAttrValue) return null;
143
144 // We've read one of the ngh ids, keep the remaining one, so that
145 // we can set it back on the DOM element.
146 const rootNgh = rootViewNgh ? `|${rootViewNgh}` : '';
147 const remainingNgh = isRootView ? componentViewNgh : rootNgh;
148
149 let data: SerializedView = {};
150 // An element might have an empty `ngh` attribute value (e.g. `<comp ngh="" />`),
151 // which means that no special annotations are required. Do not attempt to read
152 // from the TransferState in this case.
153 if (nghAttrValue !== '') {
154 const transferState = injector.get(TransferState, null, {optional: true});
155 if (transferState !== null) {
156 const nghData = transferState.get(NGH_DATA_KEY, []);
157
158 // The nghAttrValue is always a number referencing an index
159 // in the hydration TransferState data.
160 data = nghData[Number(nghAttrValue)];
161
162 // If the `ngh` attribute exists and has a non-empty value,
163 // the hydration info *must* be present in the TransferState.
164 // If there is no data for some reasons, this is an error.
165 ngDevMode && assertDefined(data, 'Unable to retrieve hydration info from the TransferState.');
166 }
167 }
168 const dehydratedView: DehydratedView = {
169 data,
170 firstChild: getFirstChild(rNode as unknown as Node),
171 };
172
173 if (isRootView) {
174 // If there is hydration info present for the root view, it means that there was
175 // a ViewContainerRef injected in the root component. The root component host element
176 // acted as an anchor node in this scenario. As a result, the DOM nodes that represent
177 // embedded views in this ViewContainerRef are located as siblings to the host node,
178 // i.e. `<app-root /><#VIEW1><#VIEW2>...<!--container-->`. In this case, the current
179 // node becomes the first child of this root view and the next sibling is the first

Callers

nothing calls this directly

Calls 9

assertDefinedFunction · 0.90
getFirstChildFunction · 0.90
getNextSiblingFunction · 0.90
setSegmentHeadFunction · 0.85
getAttributeMethod · 0.65
getMethod · 0.65
setAttributeMethod · 0.65
removeAttributeMethod · 0.65

Tested by

no test coverage detected