# Is it possible to Dynamically install code to FRONTEND canisters?

**URL:** <https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746>\
**Category:** Developers\
**Created:** [February 28, 2023, 5:53pm UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746 "2023-02-28T17:53:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [February 28, 2023, 5:53pm UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/1 "2023-02-28T17:53:19Z")

</div>

I’m trying to dynamically install canister code to UI canisters. First of all, is there a mechanism for doing such a thing? I’m attempting to do so using the following function

```auto
ic.install_code({
            arg : Blob, 
            wasm_module : Blob,
            mode = #upgrade;
            canister_id : Principal;
        });

```

If this is the proper function to use, what value must I insert as the `arg` value when the canister that I’m installing the code to is a frontend/UI canister?

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 1, 2023, 10:28pm UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/2 "2023-03-01T22:28:26Z")

</div>

@peterparker , might you know the answer to this one? you were spot on with my last [post](https://forum.dfinity.org/t/need-help-implementing-install-code-canister-method/18313) regarding the `install_code()` method.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 6:11am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/3 "2023-03-02T06:11:33Z")

</div>

> [@Jesse](#):
>
> install canister code to UI canisters

do you mean replace the code of a [certified asset canister](Do you mean `install_code` to a certified asset canister ([sdk/src/canisters/frontend at master · dfinity/sdk · GitHub](https://github.com/dfinity/sdk/tree/master/src/canisters/frontend))?) provided and deployed with dfx?

not an expert, notably because I wrote and use my own custom asset canister in Juno, but I would say yes, you can `install_code` as you can `install_code` in any canister on the IC as long as you are the controller

that said, assuming you want to preserve the canister features, `install_code` replace the all code so if you want to replace its code, then you will have to use the wasm of the certified canister you have compiled but maybe your goal is to replace the wasm code with something totally different?

regarding args, as far as I can see from this piece of code [sdk/src/canisters/frontend/ic-certified-assets/src/lib.rs at b4a337f8ce25042894983fe47b2e3241b35cd453 · dfinity/sdk · GitHub](https://github.com/dfinity/sdk/blob/b4a337f8ce25042894983fe47b2e3241b35cd453/src/canisters/frontend/ic-certified-assets/src/lib.rs#L374), I would say it needs no args - i.e. empty. @Vivienne can confirm or unconfirm this.

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 2, 2023, 7:19am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/4 "2023-03-02T07:19:46Z")

</div>

> [@peterparker](#):
>
> do you mean replace the code of a [certified asset canister](Do you mean `install_code` to a certified asset canister ([sdk/src/canisters/frontend at master · dfinity/sdk · GitHub](https://github.com/dfinity/sdk/tree/master/src/canisters/frontend))?) provided and deployed with dfx?

I believe the UI canister I’m referring to corresponds to the `ic-frontend-canister` file. I’m talking about the canister that you get as a result of deploying the frontend code of a project that was created using `dfx new`. Does that make sense?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 7:23am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/5 "2023-03-02T07:23:35Z")

</div>

> [@Jesse](#):
>
> I believe the UI canister I’m referring to corresponds to the `ic-frontend-canister` file. I’m talking about the canister that you get as a result of deploying the frontend code of a project that was created using `dfx new`. Does that make sense?

Thanks for clearing that. In that case we were indeed speaking about the same canister.

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 2, 2023, 7:23am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/6 "2023-03-02T07:23:47Z")

</div>

> [@peterparker](#):
>
> , I would say it needs no args - i.e. empty.

So i would do the following?

```auto

ic.install_code({ 
            wasm_module : Blob,
            mode = #upgrade;
            canister_id : Principal;
        });

```

Just omit the `arg` property?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 7:29am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/7 "2023-03-02T07:29:37Z")

</div>

I think `arg` is mandatory so I meant an empty blob like `arg = Blob.fromArray([])` or something but would wait for Severin feedback.

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 2, 2023, 7:44am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/8 "2023-03-02T07:44:24Z")

</div>

> [@peterparker](#):
>
> that said, assuming you want to preserve the canister features, `install_code` replace the all code so if you want to replace its code, then you will have to use the wasm of the certified canister you have compiled but maybe your goal is to replace the wasm code with something totally different?

I’m pretty sure i want to preserve the canister features. I’m not familiar with what goes on when the frontend canister code is compiled and deployed, but I’ll describe the context to you so that you can derive the goal.

Context: im creating an app that is designed to have many instances deployed to the IC. The app has a frontend canister and backend canister. So each instance of the app that is deployed to the IC will have its own frontend canister (and consequently, it’s own URL) and it’s own backend canister.

I need a way to deliver upgrades to these many different instances of the app that are deployed to the IC. For that, I’ll be using the `install_code` method to programmatically deliver the upgrades to these many different instances. When calling `install_code` on the frontend canisters, i need to do so in a manner that does not change the canister ids that are specified within the `canister_ids.json ` file. The reason being is that each instance of the app will have canister Id’s that are unique to that particular instance. Those canister id’s are specified in the `canister_ids.json` file, therefore, that file needs to be unchanged, while the rest of the code is still delivered to the frontend canister. Will i be able to achieve this using the `install_code` method to deliver the wasm modules to the UI canister’s? Or will the `install_code` cause the canister ids specified in the `canister_ids.json` file to be overwritten?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 8:01am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/9 "2023-03-02T08:01:11Z")

</div>

`install_code` replaces the wasm code of a canister without changing it’s id.

e.g. if I install a wasm-v0.0.2 in a canister ID 123 that runs wasm-v0.0.1, after install code the ID is still 123 and the code is wasm-v0.0.2

so in that sense, yes installing code does not impact the canister ids.

now, if I understand what you are trying to achieve, I think there are two distinct upgrades for your target goal

1. upgrade the wasm of your frontend asset canister to stay up-to-date with the last enhancement and fix of the certified asset cansiter provided through dfx

2. update the app (JS, HTML, etc) that exists in memory of your various canister instances to propagate the last enhancement and fix that your are developing

The 1. can be achieved with `install_code` by providing a newer version of the wasm and the 2., I think, can be achieved by replicating the upload process achieved by `dfx deploy`.

I don’t think 2. can be achieved by install\_code out-of-the-box with that particular canister because the assets of your app aren’t part of the wasm that is uploaded.

Does that help and answer your question a bit?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 8:07am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/10 "2023-03-02T08:07:11Z")

</div>

Personal note: for that type of architecture with multiple instances of the same apps, don’t know your project but, spontaneously, I would personaly go for a solution where I would develop my own canister that does both backend and frontend - i.e. not two canisters but one.

Time consuming to develop but worth it at the end. In Papyrs I got that two canisters approach and retrospectively I think it was an incorrect decision. In Juno I went for one canister - satellite - that does everything.

Of course it depends of the project, scalability etc. but for mine, never been so happy that way.

Just my two spontaneous cents for what is worth 😁.

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 2, 2023, 8:20am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/11 "2023-03-02T08:20:54Z")

</div>

> [@peterparker](#):
>
> I don’t think 2. can be achieved by install\_code out-of-the-box with that particular canister because the assets of your app aren’t part of the wasm that is uploaded.
> 
> Does that help and answer your question a bit?

This does help, however i have 1 clarifying question:

If i make the updates to the JSX and HTML code of my frontend, and then call `dfx build` on the now-updated frontend code, I’ll get a .wasm file as a result. Are you saying that _that_ .wasm file (if installed to the other instances of frontend canisters using the `install_code` method) Would **not** result in the other frontend canisters receiving the updated JSX and HTML? But it **would** result in them receiving the other updates you describe in the 1.?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 9:13am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/12 "2023-03-02T09:13:52Z")

</div>

> [@Jesse](#):
>
> Are you saying that _that_ .wasm file (if installed to the other instances of frontend canisters using the `install_code` method) Would **not** result in the other frontend canisters receiving the updated JSX and HTML?

Exactly. The wasm file does not contain the JSX and HTML. These outcome are uploaded afterwards.

`dfx deploy` a frontend canister does two things my understanding:

1. upgrade wasm code
2. upload the JS and HTML

Makes sense?

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 2, 2023, 9:32am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/13 "2023-03-02T09:32:04Z")

</div>

Yes that makes sense. It’s unfortunate news for me because now i have no idea how to programmatically propagate updates of the JSX/HTML code to the frontend canister instances. Is there any documentation regarding the process by which the JSX/HTML code is uploaded to the canisters? That’d be a helpful starting point in this new researching arc that i must now embark on 😭

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [March 2, 2023, 10:03am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/14 "2023-03-02T10:03:33Z")

</div>

> [@Jesse](#):
>
> Is there any documentation regarding the process by which the JSX/HTML code is uploaded to the canisters?

Not really documentation, but the entry point is [`ic-asset::sync`](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/ic-asset/src/sync.rs#L51). If you want to run this standalone (without `dfx deploy`ing wasm), you’d use [`icx-asset sync`](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/icx-asset/src/commands/sync.rs#L13).

The reason for using this install-and-only-after-upload approach is that if we baked all frontend code into the wasm, we’d hit size limits instantly - `install_code` accepts gzipped input, but still only ~2MB. Most frontends are larger than that, so we had to choose a different approach.

I see three ways forward for you: 1) Either write your own asset canister that bakes assets into it, the same way the NNS frontend dapp does it (and you’d be limited to the 2MB total asset size). 2) Or you can write your own syncing logic in a canister so that your canisters can automatically push/pull new frontend versions. 3) Or you manually use `ic-asset::sync` from your own computer, which would require access to all of your service’s canisters

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 2, 2023, 10:55am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/15 "2023-03-02T10:55:26Z")

</div>

> [@Vivienne](#):
>
> 1. Or you can write your own syncing logic in a canister so that your canisters can automatically push/pull new frontend versions.

This option sounds like the best option, but what’s stopping this option from encountering the same 2MB issue? I’m assuming some chunking would have to be written into the logic then, right? Option 1 prohibitively limited and option 3 wouldn’t be possible since i wouldn’t be the canister controller of the other canisters that are due to receive the updates.

Now, what i need to do is understand the syncing logic and then implement it. Is there any reason why i wouldn’t be able to implement the syncing logic in Motoko? I don’t know rust at all. If necessary, I’ll bite the bullet and learn it, but would rather avoid adding such a task to my To-do list.

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 2, 2023, 11:13am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/16 "2023-03-02T11:13:25Z")

</div>

The easiest solution is probably to have all your frontend canister within your `dfx.json` (that’s possible right @Vivienne) and create a bash script that does following pseudo-code:

```auto
for each canister ids of type asset declared in your dfx.json
    dfx deploy the_canister_name --network ic etc.
end

```

not the prettiest, not the most sustanable, not the cheapest, not the more secure - i.e. what if one update fails and the other not -, not the more engineered things but, can do the job

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [March 2, 2023, 11:30am UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/17 "2023-03-02T11:30:52Z")

</div>

The syncing/chunking logic isn’t that bad. Here’s the [canister interface](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/ic-certified-assets/assets.did) and the [code where data gets processed](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/ic-certified-assets/src/state_machine.rs).

I don’t see any reason why it would be harder in Motoko than in Rust. In the end it’s just chunking blobs and making inter canister calls.

You’ll probably have to learn to read Rust if you want to copy the syncing logic, but that’s not too bad. If you have any questions on specific implementation details feel free to ask here.

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 16, 2023, 3:36pm UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/18 "2023-03-16T15:36:17Z")

</div>

> [@Vivienne](#):
>
> The syncing/chunking logic isn’t that bad. Here’s the [canister interface](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/ic-certified-assets/assets.did)

@Vivienne , I noticed that the `commit_batch` method (along with several others) isn’t an asynchronous method. I mean to make it so that a motoko canister can call the `commit_batch` method of an assets canister in order to perform the upgrade for the `JSX/HTML` code within the asset canister. The fact that the `commit_batch` method isn’t an async method implies that this method cannot be called from another canister, correct?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [March 16, 2023, 3:46pm UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/19 "2023-03-16T15:46:58Z")

</div>

Every function in any canister interface is an async method. [Here’s](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/ic-asset/src/asset_canister/batch.rs#L57-L72) how it gets called in the code that uploads assets via dfx.

If you’re referring to [this function declaration](https://github.com/dfinity/sdk/blob/master/src/canisters/frontend/ic-certified-assets/src/lib.rs#L229), then you should not think of it as ‘not async’, but instead as an update function because it has `#[candid_method(update)]`, which makes it callable from the outside, no matter if it is `async fn` or `fn`

---

<div class="post-metadata">

**Author:** ![Jesse](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jesse/32/37836_2.png) [@Jesse](https://forum.dfinity.org/u/Jesse)\
**Post date:** [March 16, 2023, 3:52pm UTC](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746/20 "2023-03-16T15:52:33Z")

</div>

Thats perfect. Thanks a bunch!

[Next page](https://forum.dfinity.org/t/is-it-possible-to-dynamically-install-code-to-frontend-canisters/18746.md?page=2)
