Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/core/src/hydration/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DEHYDRATED_VIEWS,
LContainer,
} from '../render3/interfaces/container';
import {isLetDeclaration, isTNodeShape} from '../render3/interfaces/node';
import {Renderer} from '../render3/interfaces/renderer';
import {RNode} from '../render3/interfaces/renderer_dom';
import {isLContainer, isLView} from '../render3/interfaces/type_checks';
Expand Down Expand Up @@ -112,6 +113,13 @@ export function cleanupLView(lView: LView) {

const tView = lView[TVIEW];
for (let i = HEADER_OFFSET; i < tView.bindingStartIndex; i++) {
// Let declarations store arbitrary values in the LView, which may look like an LView
// or an LContainer (e.g. an array of objects), so we need to exclude them.
const tNode = tView.data[i];
if (isTNodeShape(tNode) && isLetDeclaration(tNode)) {
continue;
}

if (isLContainer(lView[i])) {
const lContainer = lView[i];
cleanupLContainer(lContainer);
Expand Down
34 changes: 34 additions & 0 deletions packages/platform-server/test/full_app_hydration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7999,6 +7999,40 @@ describe('platform-server full application hydration integration', () => {
expect(clientRootNode.textContent).toContain('foo');
});

it('should not mutate an array of objects stored in a let declaration during cleanup', async () => {
@Component({
selector: 'app',
template: `
@let list = items;
@for (item of list; track item.id) {
{{ item.id }}/{{ list.length }}
}
`,
})
class SimpleComponent {
items = Array.from({length: 20}, (_, id) => ({id}));
}

const html = await ssr(SimpleComponent);
const ssrContents = getAppContents(html);

expect(ssrContents).toContain('<app ngh');
expect(ssrContents).toContain('19/20');

resetTViewsFor(SimpleComponent);

const appRef = await prepareEnvironmentAndHydrate(doc, html, SimpleComponent);
const compRef = getComponentRef<SimpleComponent>(appRef);
appRef.tick();
await appRef.whenStable();

const clientRootNode = compRef.location.nativeElement;
verifyAllNodesClaimedForHydration(clientRootNode);
verifyClientAndSSRContentsMatch(ssrContents, clientRootNode);
const {items} = compRef.instance;
expect(items.map((item) => Object.keys(item))).toEqual(items.map(() => ['id']));
});

it('should handle let declaration inside a projected control flow node', async () => {
@Component({
selector: 'test',
Expand Down
Loading