[web] Encode AssetManifest.bin as JSON and use that on the web. (#131382)

This PR modifies the web build slightly to create an `AssetManifest.json`, that is a JSON(base64)-encoded version of the `AssetManifest.bin` file.

_(This should enable all browsers to download the file without any interference, and all servers to serve it with the correct headers.)_

It also modifies Flutter's `AssetManifest` class so it loads and uses said file `if (kIsWeb)`.

### Issues

* Fixes https://github.com/flutter/flutter/issues/124883

### Tests

* Unit tests added.
* Some tests that run on the Web needed to be informed of the new filename, but their behavior didn't have to change (binary contents are the same across all platforms).
* I've deployed a test app, so users affected by the BIN issue may take a look at the PR in action:
  * https://dit-tests.web.app
This commit is contained in:
David Iglesias
2023-09-19 15:38:51 -07:00
committed by GitHub
parent ba2dde48fa
commit c7c9d8eea6
7 changed files with 205 additions and 27 deletions

View File

@@ -168,6 +168,7 @@ class ManifestAssetBundle implements AssetBundle {
// We assume the main asset is designed for a device pixel ratio of 1.0.
static const String _kAssetManifestJsonFilename = 'AssetManifest.json';
static const String _kAssetManifestBinFilename = 'AssetManifest.bin';
static const String _kAssetManifestBinJsonFilename = 'AssetManifest.bin.json';
static const String _kNoticeFile = 'NOTICES';
// Comically, this can't be name with the more common .gz file extension
@@ -233,8 +234,6 @@ class ManifestAssetBundle implements AssetBundle {
// device.
_lastBuildTimestamp = DateTime.now();
if (flutterManifest.isEmpty) {
entries[_kAssetManifestJsonFilename] = DevFSStringContent('{}');
entryKinds[_kAssetManifestJsonFilename] = AssetKind.regular;
entries[_kAssetManifestJsonFilename] = DevFSStringContent('{}');
entryKinds[_kAssetManifestJsonFilename] = AssetKind.regular;
final ByteData emptyAssetManifest =
@@ -242,6 +241,11 @@ class ManifestAssetBundle implements AssetBundle {
entries[_kAssetManifestBinFilename] =
DevFSByteContent(emptyAssetManifest.buffer.asUint8List(0, emptyAssetManifest.lengthInBytes));
entryKinds[_kAssetManifestBinFilename] = AssetKind.regular;
// Create .bin.json on web builds.
if (targetPlatform == TargetPlatform.web_javascript) {
entries[_kAssetManifestBinJsonFilename] = DevFSStringContent('""');
entryKinds[_kAssetManifestBinJsonFilename] = AssetKind.regular;
}
return 0;
}
@@ -437,8 +441,8 @@ class ManifestAssetBundle implements AssetBundle {
final Map<String, List<String>> assetManifest =
_createAssetManifest(assetVariants, deferredComponentsAssetVariants);
final DevFSStringContent assetManifestJson = DevFSStringContent(json.encode(assetManifest));
final DevFSByteContent assetManifestBinary = _createAssetManifestBinary(assetManifest);
final DevFSStringContent assetManifestJson = DevFSStringContent(json.encode(assetManifest));
final DevFSStringContent fontManifest = DevFSStringContent(json.encode(fonts));
final LicenseResult licenseResult = _licenseCollector.obtainLicenses(packageConfig, additionalLicenseFiles);
if (licenseResult.errorMessages.isNotEmpty) {
@@ -464,6 +468,13 @@ class ManifestAssetBundle implements AssetBundle {
_setIfChanged(_kAssetManifestJsonFilename, assetManifestJson, AssetKind.regular);
_setIfChanged(_kAssetManifestBinFilename, assetManifestBinary, AssetKind.regular);
// Create .bin.json on web builds.
if (targetPlatform == TargetPlatform.web_javascript) {
final DevFSStringContent assetManifestBinaryJson = DevFSStringContent(json.encode(
base64.encode(assetManifestBinary.bytes)
));
_setIfChanged(_kAssetManifestBinJsonFilename, assetManifestBinaryJson, AssetKind.regular);
}
_setIfChanged(kFontManifestJson, fontManifest, AssetKind.regular);
_setLicenseIfChanged(licenseResult.combinedLicenses, targetPlatform);
return 0;

View File

@@ -325,6 +325,102 @@ flutter:
});
});
group('AssetBundle.build (web builds)', () {
late FileSystem testFileSystem;
setUp(() async {
testFileSystem = MemoryFileSystem(
style: globals.platform.isWindows
? FileSystemStyle.windows
: FileSystemStyle.posix,
);
testFileSystem.currentDirectory = testFileSystem.systemTempDirectory.createTempSync('flutter_asset_bundle_test.');
});
testUsingContext('empty pubspec', () async {
globals.fs.file('pubspec.yaml')
..createSync()
..writeAsStringSync('');
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript);
expect(bundle.entries.keys,
unorderedEquals(<String>[
'AssetManifest.json',
'AssetManifest.bin',
'AssetManifest.bin.json',
])
);
expect(
utf8.decode(await bundle.entries['AssetManifest.json']!.contentsAsBytes()),
'{}',
);
expect(
utf8.decode(await bundle.entries['AssetManifest.bin.json']!.contentsAsBytes()),
'""',
);
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
testUsingContext('pubspec contains an asset', () async {
globals.fs.file('.packages').createSync();
globals.fs.file('pubspec.yaml').writeAsStringSync(r'''
name: test
dependencies:
flutter:
sdk: flutter
flutter:
assets:
- assets/bar/lizard.png
''');
globals.fs.file(
globals.fs.path.joinAll(<String>['assets', 'bar', 'lizard.png'])
).createSync(recursive: true);
final AssetBundle bundle = AssetBundleFactory.instance.createBundle();
await bundle.build(packagesPath: '.packages', targetPlatform: TargetPlatform.web_javascript);
expect(bundle.entries.keys,
unorderedEquals(<String>[
'AssetManifest.json',
'AssetManifest.bin',
'AssetManifest.bin.json',
'FontManifest.json',
'NOTICES', // not .Z
'assets/bar/lizard.png',
])
);
final Map<Object?, Object?> manifestJson = json.decode(
utf8.decode(
await bundle.entries['AssetManifest.json']!.contentsAsBytes()
)
) as Map<Object?, Object?>;
expect(manifestJson, isNotEmpty);
expect(manifestJson['assets/bar/lizard.png'], isNotNull);
final Uint8List manifestBinJsonBytes = base64.decode(
json.decode(
utf8.decode(
await bundle.entries['AssetManifest.bin.json']!.contentsAsBytes()
)
) as String
);
final Uint8List manifestBinBytes = Uint8List.fromList(
await bundle.entries['AssetManifest.bin']!.contentsAsBytes()
);
expect(manifestBinJsonBytes, equals(manifestBinBytes),
reason: 'JSON-encoded binary content should be identical to BIN file.');
}, overrides: <Type, Generator>{
FileSystem: () => testFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
});
testUsingContext('Failed directory delete shows message', () async {
final FileExceptionHandler handler = FileExceptionHandler();
final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);