# Nested & dynamic custom element composition

**URL:** <https://discourse.aurelia.io/t/nested-dynamic-custom-element-composition/4231>\
**Category:** vNext\
**Created:** [May 31, 2021, 8:04am UTC](https://discourse.aurelia.io/t/nested-dynamic-custom-element-composition/4231 "2021-05-31T08:04:12Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![estaylorco](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/estaylorco/32/1887_2.png) [@estaylorco](https://discourse.aurelia.io/u/estaylorco)\
**Post date:** [January 4, 2025, 1:58am UTC](https://discourse.aurelia.io/t/nested-dynamic-custom-element-composition/4231/13 "2025-01-04T01:58:34Z")

</div>

We have a 3.2M LOC application (107 top-level routes, dynamic composition only within each of those routes) that we’re migrating from Aurelia 1.0 to 2.0 (the application actually began back in the days of Durandal, but it was much smaller then). As the first step in the journey, I used `npx makes aurelia` to scaffold a default Aurelia application (the first option).

I’m encountering issues that are discussed in this topic by others (minus the dynamic composition aspect). **To synopsize, use of the `@customElement` decorator prevents the leaking of memory from one route to the next, but sacrifices nested custom elements. Omitting `@customElement` and simply running with conventions as discussed by @huochunpeng brings back nested custom elements, but causes the leaking of memory.**

I can both cause and resolve these issues at will by going back and forth between `@customElement` on each component, and commenting them out. This is true without even using `au-compose`.

**I’m using the nightly build (“dev”) of Aurelia, but the problem exists even in the “latest” build.**

From **main.js** (configured for direct routing):

```auto
import Aurelia from 'aurelia';
import { MyApp } from './my-app';
import { RouterConfiguration } from '@aurelia/router';

Aurelia.register(RouterConfiguration.customize({ useUrlFragmentHash: false }))
    .app(MyApp)
    .start();

```

From **my-app.html** :

```auto
<template>
    <import from="./t1-shell"></import>

    <t1-shell></t1-shell>
</template>

```

From **t1-shell.html** :

```auto
<template>
    <import from="./some-component"></import>
    <import from="./another-component"></import>

    <a load="/some-component">Some Component</a>
    <a load="/another-component">Another Component</a>

    <au-viewport></au-viewport>
</template>

```

There is nothing significant about **another-component.html** , but **some-component.html** sports a nested custom element, like so:

```auto
<template>
    <import from="./nested-component"></import>

    <div class="some-component">We're in some component!</div>
    <nested-component></nested-component>
</template>

```

**SCENARIO 1: Using `@customElement` on each component:**

![Decorator @customElement present](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/3/3f5f0f8ddd55ececee557149571d15b6c313d8ac.gif)

You can see above when I navigate to the `some-component` route, `nested-component` does not show up. Note the heap snapshots:

![Heap snapshots with @customElement present](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/c/c59679fe6e8cf0e4ed507523276ec0423a043735.gif)

Off to the left (offscreen), I’m going to each route, and then making sure the previous route doesn’t show up in the snapshot.

So, then, with the decorator present, we don’t leak. Consider the next scenario.

**SCENARIO 2: Commenting out `@customElement`**

![Decorator commented out (conventional approach)](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/2/2cc8961219fab38035962087817f80b6eaeb1f5e.gif)

You can see above the nested custom element when I navigate to `some-component`. However, take a look at the heap snapshots:

![Heap snapshots (no decorator)](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/8/8355996739496956230fcbb3ee5b3dee22ec6fc0.gif)

So, then, without the decorator, the nested component is present, but we leak from route to route.

(Just to clarify, when I typed in the component name to check the heap, I was typing in the component _I navigated away from_).

**CONCLUSION**

This seems like a bug to me (or perhaps I’m missing something). The same is true if I use dynamic composition. But I thought that for the sake of this post, I would keep it simple.

---

_[View the full topic](https://discourse.aurelia.io/t/nested-dynamic-custom-element-composition/4231)._
