From d03582d183b1e4ab7fb82607366471f46a97f2e0 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Tue, 18 Feb 2020 16:19:36 -0800 Subject: [PATCH] URL-encode asset URLs so assets are properly loaded (#16630) * URL-encode asset URLs so assets are properly loaded * Add comment --- lib/web_ui/lib/src/engine/assets.dart | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/lib/src/engine/assets.dart b/lib/web_ui/lib/src/engine/assets.dart index 83d4c28a6..7591f27ba 100644 --- a/lib/web_ui/lib/src/engine/assets.dart +++ b/lib/web_ui/lib/src/engine/assets.dart @@ -24,11 +24,28 @@ class AssetManager { ?.content; } + /// Returns the URL to load the asset from, given the asset key. + /// + /// We URL-encode the asset URL in order to correctly issue the right + /// HTTP request to the server. + /// + /// For example, if you have an asset in the file "assets/hello world.png", + /// two things will happen. When the app is built, the asset will be copied + /// to an asset directory with the file name URL-encoded. So our asset will + /// be copied to something like "assets/hello%20world.png". To account for + /// the assets being copied over with a URL-encoded name, the Flutter + /// framework URL-encodes the asset key so when it sends a request to the + /// engine to load "assets/hello world.png", it actually sends a request to + /// load "assets/hello%20world.png". However, on the web, if we try to load + /// "assets/hello%20world.png", the request will be URL-decoded, we will + /// request "assets/hello world.png", and the request will 404. Therefore, we + /// must URL-encode the asset key *again* so when it is decoded, it is + /// requesting the once-URL-encoded asset key. String getAssetUrl(String asset) { if (Uri.parse(asset).hasScheme) { - return asset; + return Uri.encodeFull(asset); } - return (_baseUrl ?? '') + '$assetsDir/$asset'; + return Uri.encodeFull((_baseUrl ?? '') + '$assetsDir/$asset'); } Future load(String asset) async { -- GitLab