Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Revert "[web] Accepts assetBase through JS config. (#40615)" #40670

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 20 additions & 39 deletions lib/web_ui/lib/src/engine/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,26 @@ const Map<String, String> testFontUrls = <String, String>{

/// This class downloads assets over the network.
///
/// Assets are resolved relative to [assetsDir] inside the absolute base
/// specified by [assetBase] (optional).
///
/// By default, URLs are relative to the `<base>` of the current website.
/// The assets are resolved relative to [assetsDir] inside the directory
/// containing the currently executing JS script.
class AssetManager {
/// Initializes [AssetManager] with paths.
AssetManager({
this.assetsDir = _defaultAssetsDir,
String? assetBase,
}) : assert(
assetBase == null || assetBase.endsWith('/'),
'`assetBase` must end with a `/` character.',
),
_assetBase = assetBase;
/// Initializes [AssetManager] with path to assets relative to baseUrl.
const AssetManager({this.assetsDir = _defaultAssetsDir});

static const String _defaultAssetsDir = 'assets';

/// The directory containing the assets.
final String assetsDir;

/// The absolute base URL for assets.
String? _assetBase;

// Cache a value for `_assetBase` so we don't hit the DOM multiple times.
String get _baseUrl => _assetBase ??= _deprecatedAssetBase ?? '';

// Retrieves the `assetBase` value from the DOM.
//
// This warns the user and points them to the new initializeEngine style.
String? get _deprecatedAssetBase {
final DomHTMLMetaElement? meta = domWindow.document
.querySelector('meta[name=assetBase]') as DomHTMLMetaElement?;

final String? fallbackBaseUrl = meta?.content;

if (fallbackBaseUrl != null) {
// Warn users that they're using a deprecated configuration style...
domWindow.console.warn('The `assetBase` meta tag is now deprecated.\n'
'Use engineInitializer.initializeEngine(config) instead.\n'
'See: https://docs.flutter.dev/development/platform-integration/web/initialization');
}
return fallbackBaseUrl;
String? get _baseUrl {
return domWindow.document
.querySelectorAll('meta')
.where((DomElement domNode) => domInstanceOfString(domNode,
'HTMLMetaElement'))
.map((DomElement domNode) => domNode as DomHTMLMetaElement)
.firstWhereOrNull(
(DomHTMLMetaElement element) => element.name == 'assetBase')
?.content;
}

/// Returns the URL to load the asset from, given the asset key.
Expand All @@ -89,7 +67,7 @@ class AssetManager {
if (Uri.parse(asset).hasScheme) {
return Uri.encodeFull(asset);
}
return Uri.encodeFull('$_baseUrl$assetsDir/$asset');
return Uri.encodeFull('${_baseUrl ?? ''}$assetsDir/$asset');
}

/// Loads an asset and returns the server response.
Expand All @@ -112,7 +90,7 @@ class AssetManager {
}

/// An asset manager that gives fake empty responses for assets.
class WebOnlyMockAssetManager extends AssetManager {
class WebOnlyMockAssetManager implements AssetManager {
/// Mock asset directory relative to base url.
String defaultAssetsDir = '';

Expand All @@ -135,6 +113,9 @@ class WebOnlyMockAssetManager extends AssetManager {
@override
String get assetsDir => defaultAssetsDir;

@override
String get _baseUrl => '';

@override
String getAssetUrl(String asset) => asset;

Expand All @@ -146,7 +127,7 @@ class WebOnlyMockAssetManager extends AssetManager {
status: 200,
payload: MockHttpFetchPayload(
byteBuffer: _toByteData(utf8.encode(defaultAssetManifest)).buffer,
),
)
);
}
if (asset == getAssetUrl('FontManifest.json')) {
Expand All @@ -155,7 +136,7 @@ class WebOnlyMockAssetManager extends AssetManager {
status: 200,
payload: MockHttpFetchPayload(
byteBuffer: _toByteData(utf8.encode(defaultFontManifest)).buffer,
),
)
);
}

Expand Down
30 changes: 0 additions & 30 deletions lib/web_ui/lib/src/engine/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,6 @@ class FlutterConfiguration {
// runtime. Runtime-supplied values take precedence over environment
// variables.

/// The absolute base URL of the location of the `assets` directory of the app.
///
/// This value is useful when Flutter web assets are deployed to a separate
/// domain (or subdirectory) from which the index.html is served, for example:
///
/// * Application: https://www.my-app.com/
/// * Flutter Assets: https://cdn.example.com/my-app/build-hash/assets/
///
/// The `assetBase` value would be set to:
///
/// * `'https://cdn.example.com/my-app/build-hash/'`
///
/// It is also useful in the case that a Flutter web application is embedded
/// into another web app, in a way that the `<base>` tag of the index.html
/// cannot be set (because it'd break the host app), for example:
///
/// * Application: https://www.my-app.com/
/// * Flutter Assets: https://www.my-app.com/static/companion/flutter/assets/
///
/// The `assetBase` would be set to:
///
/// * `'/static/companion/flutter/'`
///
/// Do not confuse this configuration value with [canvasKitBaseUrl].
String? get assetBase => _configuration?.assetBase;

/// The base URL to use when downloading the CanvasKit script and associated
/// wasm.
///
Expand Down Expand Up @@ -288,10 +262,6 @@ external JsFlutterConfiguration? get _jsConfiguration;
class JsFlutterConfiguration {}

extension JsFlutterConfigurationExtension on JsFlutterConfiguration {
@JS('assetBase')
external JSString? get _assetBase;
String? get assetBase => _assetBase?.toDart;

@JS('canvasKitBaseUrl')
external JSString? get _canvasKitBaseUrl;
String? get canvasKitBaseUrl => _canvasKitBaseUrl?.toDart;
Expand Down
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/initialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Future<void> initializeEngineServices({
}
};

assetManager ??= AssetManager(assetBase: configuration.assetBase);
assetManager ??= const AssetManager();
_setAssetManager(assetManager);

Future<void> initializeRendererCallback () async => renderer.initialize();
Expand Down
141 changes: 0 additions & 141 deletions lib/web_ui/test/engine/assets_test.dart

This file was deleted.