How to config image URLs to work the same as *.raw.* version?

https://6jgp5-riaaa-aaaai-aapuq-cai.raw.ic0.app/LUV.png
https://6jgp5-riaaa-aaaai-aapuq-cai.ic0.app/LUV.png

“dfx”: “0.10.1”

1 Like

removed “dfx”: “0.10.1”
and edited dfx.json that prevented deploy

      "source": [
        "src/web_site/assets",
        "dist/web_site/"
      ],

to

      "source": [
        "src/web_site/assets"
      ],

now both links broken

It looks like the problem are the automatic redirects by the asset canister. If you, for example, use:

curl -sv \
    --user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36" \
    -o hello1.png \
    https://6jgp5-riaaa-aaaai-aapuq-cai.raw.ic0.app/LUV.png

You will see that you get a 308 HTTP status code redirecting you to “non-raw”: https://6jgp5-riaaa-aaaai-aapuq-cai.ic0.app/LUV.png. This is most likely caused by the asset canister and you can disable it. Here are the instructions.

Also, for your use-case a “non-raw” URL is not possible as on “non-raw” the service worker is always served first. That’s why the image is broken.

Let me know if that helped!

1 Like

called:
dfx canister call web_site set_asset_properties ‘( record { key=“/LUV.png”; allow_raw_access=opt(opt(true)) })’ --network ic
but raw still broken;

so there’s no way to by-pass the service worker for a specific URL?

*.raw.ic0.app bypasses the Service Worker, but the asset canister has by default this redirect to *.ic0.app. I am not familiar with how to configure it properly in the asset canister. @Severin knows better.

*.ic0.app will always serve the Service Worker, unless the user-agent indicates a crawler/bot. Hence, you can bypass the Service Worker by specifying a special user-agent (e.g., Googlebot).

1 Like

This should work. @mnl do you have an idea?

1 Like

Can I edit the list of special user-agents that bypass the Service Worker?

The list of special user-agents in the boundary nodes cannot be edited. If you are curious to see the list, just have a look at isbot on npm, on which the boundary node list is based.

I am not sure about the asset canister and whether it also maintains a list. Here, @mnl is the expert.

1 Like

The asset canister stores a per-asset flag of redirecting the raw URL to non-raw, and the above command should work to update that flag. We’re looking into what the problem is

2 Likes

Just to give you a heads-up. I tested it and seems the problem is only with index.html or base url redirected to non-raw url and for other assets when there allow_raw_access set to true, its working.

2 Likes

dfx canister call web_site set_asset_properties ‘( record { key=“/LUV.png”; allow_raw_access=opt(opt(true)) })’ --network ic
returns
()
does that mean it should of worked?
next thing I’ll try is comparing to latest dfx new project

now live with dfx new 0.13.1 and same thing
also edited the new .ic-assets.json5 file in assets with
"allow_raw_access": true

tried to revert back to “dfx”: “0.10.1”
but get

The Replica returned an error: code 4, message: "Caller is not authorized"

Have you tried the function get_asset_properties? This should show if it set the setting correctly.

Are you willing to have a little bit of downtime? Then I suggest you reinstall the canister. Then all properties should be set correctly. But please test this first with a different canister just in case something could go wrong

1 Like

Are you sure? I just tested a bit and I could not reproduce this. I suspect you forgot/did not know that you don’t only have to edit (assuming the project dfx new hello) src/hello_frontend/assets/.ic-assets.json5 but also src/hello_frontend/src/.ic-assets.json5.

Yes this should work. In my own testing, I needed to do a hard refresh though. The browser cached the allow_raw_access: false setting

Did you edit both .ic-assets.json5 files?

1 Like

only require the raw for assets

yes working with 0.13.1 dfx new project and "allow_raw_access": true
didn’t realize the browser cache masked the fix as it was originally cached with working image link and then broke but didn’t go back to working even when fixed :thinking: strange…

Thank you for all the help! :grinning:

1 Like

Unfortunately switching back and forth between the projects has created an issue with upgrading the canister.
deploy either hangs on "Starting batch."
or after canceling and trying again
The Replica returned an error: code 4, message: "Caller does not have Prepare permission"
tried stopping and starting the canister and still same issue…

using --wallet= cycles wallet in dfx deploy fixed this :grinning:

1 Like

but yea confirmed that 0.13.1 version is redirecting .raw.ic0.app to .ic0.app
with "allow_raw_access": true in both src and assets
and key “/LUV.png” set (record { headers = null; allow_raw_access = opt true; max_age = null })

with dfx 0.14.0 also same
and dfx canister install web_site --mode reinstall --network=ic also same thing

added "Access-Control-Allow-Origin": "*",
in .ic-assets.json5 under "headers"
and the raw URL is working now

how would I use set_asset_properties to set the headers directly?

this works :smile:

dfx canister call project_name_frontend set_asset_properties '( record { key="/specific_per_asset.png"; headers=opt(opt(vec{ record{"Access-Control-Allow-Origin"; "*"}})); allow_raw_access=opt(opt(true)) })' --network ic

hopefully more dfx examples are added to examples repo readmes
like:

dfx canister call quicksort sort '(vec { 5; 3; 0; 9; 8; 2; 1; 4; 7; 6 })'

which is the only example I could find for how to define a vec inline like this

having one full example call for each reference function would be much appreciated :hugs:
https://internetcomputer.org/docs/current/references/asset-canister

1 Like