HTTP response with compressed assets (*.br/*.gz, if available)

One of the effective methods of speeding up the loading of the frontend is file compression. Does the asset canister support compressed asset responses if available? What needs to be done to implement support?

Yes. “Just” upload Gzip files to your assets canister and it should do.

Not sure where documented but e.g. in code you can spot the list of encodes there https://github.com/dfinity/sdk/blob/cc58656e8d0e585fa89aaa32713e18b34bb8e79b/src/canisters/frontend/ic-certified-assets/src/state_machine.rs#L37

To compress the files you can for example run a postbuild script before uploading to your canister

In a script build.compress.sh:

#!/usr/bin/env bash

find build/ -type f | xargs -I{} gzip -fnk "{}"

in package.json

"scripts": {
		"build:compress": "./scripts/build.compress.sh",
                "build": "vite build && npm run build:compress",

I do that in the console of Juno build with SvelteKit https://github.com/buildwithjuno/juno/blob/main/scripts/build.compress.sh

2 Likes

Thanks for the code examples, David! On the builder side, there are no problems creating compressed files, I generate both versions - gzip and brotli. But when loading, I see that the canister returns gzip, as I understand it, priority is given to gzip.

1 Like

Yes, that’s correct. You can force another encoding if you remove anything else from the Accept-Encoding header.

Tangentially related Warning: If you try to verify the retuned data with the certificate it will not work right now. For more details, have a look at the Asset Canister section of the changelog, for now the unreleased section

1 Like