Skip to content

Commit 5558e27

Browse files
committed
fix(core): take skip hydration flag into account while hydrating i18n blocks (#57299)
This commit updates serialization and hydration i18n logic to take into account situations when i18n blocks are located within "skip hydration" blocks. Resolves #57105. PR Close #57299
1 parent 8621679 commit 5558e27

9 files changed

Lines changed: 135 additions & 3 deletions

File tree

‎packages/core/src/hydration/annotate.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import {ApplicationRef} from '../application/application_ref';
10-
import {APP_ID} from '../application/application_tokens';
1110
import {isDetachedByI18n} from '../i18n/utils';
1211
import {ViewEncapsulation} from '../metadata';
1312
import {Renderer2} from '../render';

‎packages/core/src/hydration/i18n.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {assertDefined, assertNotEqual} from '../util/assert';
2121
import type {HydrationContext} from './annotate';
2222
import {DehydratedIcuData, DehydratedView, I18N_DATA} from './interfaces';
2323
import {isDisconnectedRNode, locateNextRNode, tryLocateRNodeByPath} from './node_lookup_utils';
24+
import {isI18nInSkipHydrationBlock} from './skip_hydration';
2425
import {IS_I18N_HYDRATION_ENABLED} from './tokens';
2526
import {
2627
getNgContainerSize,
@@ -187,6 +188,11 @@ export function trySerializeI18nBlock(
187188
return null;
188189
}
189190

191+
const parentTNode = tView.data[tI18n.parentTNodeIndex] as TNode;
192+
if (parentTNode && isI18nInSkipHydrationBlock(parentTNode)) {
193+
return null;
194+
}
195+
190196
const serializedI18nBlock: SerializedI18nBlock = {
191197
caseQueue: [],
192198
disconnectedNodes: new Set(),
@@ -401,7 +407,10 @@ function prepareI18nBlockForHydrationImpl(
401407
parentTNode: TNode | null,
402408
subTemplateIndex: number,
403409
) {
404-
if (!isI18nHydrationSupportEnabled()) {
410+
if (
411+
!isI18nHydrationSupportEnabled() ||
412+
(parentTNode && isI18nInSkipHydrationBlock(parentTNode))
413+
) {
405414
return;
406415
}
407416

‎packages/core/src/hydration/skip_hydration.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ export function isInSkipHydrationBlock(tNode: TNode): boolean {
7070
}
7171
return false;
7272
}
73+
74+
/**
75+
* Check if an i18n block is in a skip hydration section by looking at a parent TNode
76+
* to determine if this TNode is in a skip hydration section or the TNode has
77+
* the `ngSkipHydration` attribute.
78+
*/
79+
export function isI18nInSkipHydrationBlock(parentTNode: TNode): boolean {
80+
return (
81+
hasInSkipHydrationBlockFlag(parentTNode) ||
82+
hasSkipHydrationAttrOnTNode(parentTNode) ||
83+
isInSkipHydrationBlock(parentTNode)
84+
);
85+
}

‎packages/core/src/linker/view_container_ref.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Injector} from '../di/injector';
1010
import {EnvironmentInjector} from '../di/r3_injector';
1111
import {validateMatchingNode} from '../hydration/error_handling';
1212
import {CONTAINERS} from '../hydration/interfaces';
13-
import {hasInSkipHydrationBlockFlag, isInSkipHydrationBlock} from '../hydration/skip_hydration';
13+
import {isInSkipHydrationBlock} from '../hydration/skip_hydration';
1414
import {
1515
getSegmentHead,
1616
isDisconnectedNode,

‎packages/core/src/render3/i18n/i18n_parse.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export function i18nStartFirstCreatePass(
236236
create: createOpCodes,
237237
update: updateOpCodes,
238238
ast: astStack[0],
239+
parentTNodeIndex,
239240
};
240241
}
241242

‎packages/core/src/render3/interfaces/i18n.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ export interface TI18n {
345345
* while the Update and Create OpCodes are used at runtime.
346346
*/
347347
ast: Array<I18nNode>;
348+
349+
/**
350+
* Index of a parent TNode, which represents a host node for this i18n block.
351+
*/
352+
parentTNodeIndex: number;
348353
}
349354

350355
/**

‎packages/core/test/render3/i18n/i18n_spec.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ describe('Runtime i18n', () => {
105105
]),
106106
update: [] as unknown as I18nUpdateOpCodes,
107107
ast: [{kind: 0, index: HEADER_OFFSET + 1}],
108+
parentTNodeIndex: HEADER_OFFSET,
108109
});
109110
});
110111

@@ -154,6 +155,7 @@ describe('Runtime i18n', () => {
154155
},
155156
{kind: 0, index: HEADER_OFFSET + 8},
156157
],
158+
parentTNodeIndex: HEADER_OFFSET,
157159
});
158160
});
159161

@@ -189,6 +191,7 @@ describe('Runtime i18n', () => {
189191
}] as Text).textContent = \`Hello \${lView[i-1]}!\`; }`,
190192
]),
191193
ast: [{kind: 0, index: HEADER_OFFSET + 2}],
194+
parentTNodeIndex: HEADER_OFFSET,
192195
});
193196
});
194197

@@ -218,6 +221,7 @@ describe('Runtime i18n', () => {
218221
}] as Text).textContent = \`Hello \${lView[i-1]} and \${lView[i-2]}, again \${lView[i-1]}!\`; }`,
219222
]),
220223
ast: [{kind: 0, index: HEADER_OFFSET + 2}],
224+
parentTNodeIndex: HEADER_OFFSET,
221225
});
222226
});
223227

@@ -264,6 +268,7 @@ describe('Runtime i18n', () => {
264268
{kind: 2, index: HEADER_OFFSET + 2, children: [], type: 1},
265269
{kind: 0, index: HEADER_OFFSET + 4},
266270
],
271+
parentTNodeIndex: HEADER_OFFSET,
267272
});
268273

269274
/**** First sub-template ****/
@@ -298,6 +303,7 @@ describe('Runtime i18n', () => {
298303
type: 0,
299304
},
300305
],
306+
parentTNodeIndex: HEADER_OFFSET,
301307
});
302308

303309
/**** Second sub-template ****/
@@ -325,6 +331,7 @@ describe('Runtime i18n', () => {
325331
type: 0,
326332
},
327333
],
334+
parentTNodeIndex: HEADER_OFFSET,
328335
});
329336
});
330337

@@ -390,6 +397,7 @@ describe('Runtime i18n', () => {
390397
currentCaseLViewIndex: HEADER_OFFSET + 3,
391398
},
392399
],
400+
parentTNodeIndex: HEADER_OFFSET,
393401
});
394402
expect(getTIcu(tView, HEADER_OFFSET + 2)).toEqual(<TIcu>{
395403
type: 1,
@@ -511,6 +519,7 @@ describe('Runtime i18n', () => {
511519
currentCaseLViewIndex: HEADER_OFFSET + 3,
512520
},
513521
],
522+
parentTNodeIndex: HEADER_OFFSET,
514523
});
515524
expect(getTIcu(tView, HEADER_OFFSET + 2)).toEqual({
516525
type: 1,

‎packages/core/test/render3/is_shape_of.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const ShapeOfTI18n: ShapeOf<TI18n> = {
7777
create: true,
7878
update: true,
7979
ast: true,
80+
parentTNodeIndex: true,
8081
};
8182

8283
/**

‎packages/platform-server/test/hydration_spec.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function verifyNodeHasMismatchInfo(doc: Document, selector = 'app'): void {
117117
expect(readHydrationInfo(doc.querySelector(selector)!)?.status).toBe(HydrationStatus.Mismatched);
118118
}
119119

120+
function verifyNodeHasSkipHydrationMarker(element: HTMLElement): void {
121+
expect(readHydrationInfo(element)?.status).toBe(HydrationStatus.Skipped);
122+
}
123+
120124
/** Checks whether a given element is a <script> that contains transfer state data. */
121125
function isTransferStateScript(el: HTMLElement): boolean {
122126
return (
@@ -2744,6 +2748,97 @@ describe('platform-server hydration integration', () => {
27442748
const clientContents = stripExcessiveSpaces(clientRootNode.innerHTML);
27452749
expect(clientContents).toBe('<ol><li>1</li><li>2</li><li>3</li><!--container--></ol>');
27462750
});
2751+
2752+
describe('with ngSkipHydration', () => {
2753+
it('should skip hydration when ngSkipHydration and i18n attributes are present on a same node', async () => {
2754+
loadTranslations({
2755+
[computeMsgId(' Some {$START_TAG_STRONG}strong{$CLOSE_TAG_STRONG} content ')]:
2756+
'Some normal content',
2757+
});
2758+
2759+
@Component({
2760+
standalone: true,
2761+
selector: 'cmp-a',
2762+
template: `<ng-content />`,
2763+
})
2764+
class CmpA {}
2765+
2766+
@Component({
2767+
standalone: true,
2768+
selector: 'app',
2769+
imports: [CmpA],
2770+
template: `
2771+
<cmp-a i18n ngSkipHydration>
2772+
Some <strong>strong</strong> content
2773+
</cmp-a>
2774+
`,
2775+
})
2776+
class SimpleComponent {}
2777+
2778+
const hydrationFeatures = [withI18nSupport()] as unknown as HydrationFeature<any>[];
2779+
const html = await ssr(SimpleComponent, {hydrationFeatures});
2780+
const ssrContents = getAppContents(html);
2781+
expect(ssrContents).toContain('<app ngh');
2782+
2783+
resetTViewsFor(SimpleComponent);
2784+
2785+
const appRef = await renderAndHydrate(doc, html, SimpleComponent, {hydrationFeatures});
2786+
const compRef = getComponentRef<SimpleComponent>(appRef);
2787+
appRef.tick();
2788+
2789+
const clientRootNode = compRef.location.nativeElement;
2790+
verifyAllNodesClaimedForHydration(clientRootNode);
2791+
verifyClientAndSSRContentsMatch(ssrContents, clientRootNode);
2792+
2793+
const cmpA = clientRootNode.querySelector('cmp-a');
2794+
expect(cmpA.textContent).toBe('Some normal content');
2795+
verifyNodeHasSkipHydrationMarker(cmpA);
2796+
});
2797+
2798+
it('should skip hydration when i18n is inside of an ngSkipHydration block', async () => {
2799+
loadTranslations({
2800+
[computeMsgId('strong')]: 'very strong',
2801+
});
2802+
2803+
@Component({
2804+
standalone: true,
2805+
selector: 'cmp-a',
2806+
template: `<ng-content />`,
2807+
})
2808+
class CmpA {}
2809+
2810+
@Component({
2811+
standalone: true,
2812+
selector: 'app',
2813+
imports: [CmpA],
2814+
template: `
2815+
<cmp-a ngSkipHydration>
2816+
Some <strong i18n>strong</strong> content
2817+
</cmp-a>
2818+
`,
2819+
})
2820+
class SimpleComponent {}
2821+
2822+
const hydrationFeatures = [withI18nSupport()] as unknown as HydrationFeature<any>[];
2823+
const html = await ssr(SimpleComponent, {hydrationFeatures});
2824+
const ssrContents = getAppContents(html);
2825+
expect(ssrContents).toContain('<app ngh');
2826+
2827+
resetTViewsFor(SimpleComponent);
2828+
2829+
const appRef = await renderAndHydrate(doc, html, SimpleComponent, {hydrationFeatures});
2830+
const compRef = getComponentRef<SimpleComponent>(appRef);
2831+
appRef.tick();
2832+
2833+
const clientRootNode = compRef.location.nativeElement;
2834+
verifyAllNodesClaimedForHydration(clientRootNode);
2835+
verifyClientAndSSRContentsMatch(ssrContents, clientRootNode);
2836+
2837+
const cmpA = clientRootNode.querySelector('cmp-a');
2838+
expect(cmpA.textContent.trim()).toBe('Some very strong content');
2839+
verifyNodeHasSkipHydrationMarker(cmpA);
2840+
});
2841+
});
27472842
});
27482843

27492844
// Note: hydration for i18n blocks is not *yet* fully supported, so the tests

0 commit comments

Comments
 (0)