22{ { = < % % >= } } { { flutter_js } } < %= { { } } = % >
33{ { = < % % >= } } { { flutter_build_config } } < %= { { } } = % >
44
5- const progressBar = document . querySelector ( '#progress-bar' ) ;
65const progressText = document . querySelector ( '#progress-text' ) ;
76const progressIndicator = document . querySelector ( '#progress-indicator' ) ;
87
98async function readAssets ( ) {
10- // NOTE: AssetManifest.json will be deprecated in favour of AssetManifest.bin:
11- // https://github.com/VeryGoodOpenSource/flutter_web_preloader/issues/28
12- const response = await fetch ( 'assets/AssetManifest.json' ) ;
13- const manifest = await response . json ( ) ;
14- const assets = Object . values ( manifest )
15- . map ( ( list ) => list . map ( ( url ) => 'assets/' + url ) )
16- . reduce ( ( arr , curr ) => [ ...arr , ...curr ] , [ ] ) ;
9+ // AssetManifest.bin is encoded with Flutter's Standard Message Codec.
10+ // See: https://docs.flutter.dev/platform-integration/web/initialization
11+ // See also: https://docs.flutter.dev/release/breaking-changes/asset-manifest-dot-json#reading-asset-manifest-information-from-dart-code-outside-of-a-flutter-app
12+ // Keep in mind that AssetManifest.bin is an implementation detail of Flutter.
13+ // Reading this file isn't an officially supported workflow. The contents or format of the file might change in a future Flutter release without an announcement.
14+ const response = await fetch ( 'assets/AssetManifest.bin' ) ;
15+ const buffer = await response . arrayBuffer ( ) ;
16+ const view = new DataView ( buffer ) ;
17+ let o = 0 ;
18+
19+ const readByte = ( ) => view . getUint8 ( o ++ ) ;
20+ const readSize = ( ) => {
21+ const b = readByte ( ) ;
22+ if ( b < 254 ) return b ;
23+ if ( b === 254 ) { const s = view . getUint16 ( o , true ) ; o += 2 ; return s ; }
24+ const s = view . getUint32 ( o , true ) ; o += 4 ; return s ;
25+ } ;
26+ const readString = ( ) => {
27+ const n = readSize ( ) ;
28+ const s = new TextDecoder ( ) . decode ( new Uint8Array ( buffer , o , n ) ) ;
29+ o += n ;
30+ return s ;
31+ } ;
32+ const skip = ( ) => {
33+ const t = readByte ( ) ;
34+ if ( t <= 2 ) return ;
35+ if ( t === 7 || t === 8 ) { const n = readSize ( ) ; o += n ; return ; }
36+ if ( t === 12 ) { for ( let i = readSize ( ) ; i > 0 ; i -- ) skip ( ) ; return ; }
37+ if ( t === 13 ) { for ( let i = readSize ( ) * 2 ; i > 0 ; i -- ) skip ( ) ; }
38+ } ;
39+
40+ // The manifest is a map; we only need its keys (the asset paths).
41+ readByte ( ) ; // type 13 (map)
42+ const count = readSize ( ) ;
43+ const assets = [ ] ;
44+ for ( let i = 0 ; i < count ; i ++ ) {
45+ readByte ( ) ; // type 7 (string)
46+ const path = readString ( ) ;
47+ if ( ! path . startsWith ( 'packages/' ) ) assets . push ( path ) ;
48+ skip ( ) ;
49+ }
1750 return assets ;
1851}
1952
@@ -29,15 +62,16 @@ async function beginPreloading() {
2962 return ;
3063 }
3164
32- const batchSize = { { batch_size} } ;
65+ const batchSize = { { batch_size }
66+ } ;
3367
34- progressIndicator . style . width = '0%' ;
35- progressText . textContent = `Loaded ${ loadedAssets } of ${ totalAssets } assets` ;
68+ progressIndicator . style . width = '0%' ;
69+ progressText . textContent = `Loaded ${ loadedAssets } of ${ totalAssets } assets` ;
3670
37- for ( let i = 0 ; i < assets . length ; i += batchSize ) {
38- const batch = assets . slice ( i , i + batchSize ) ;
39- await loadBatch ( batch ) ;
40- }
71+ for ( let i = 0 ; i < assets . length ; i += batchSize ) {
72+ const batch = assets . slice ( i , i + batchSize ) ;
73+ await loadBatch ( batch ) ;
74+ }
4175}
4276
4377function reportProgress ( ) {
@@ -76,10 +110,7 @@ async function loadBatch(urls) {
76110}
77111
78112_flutter . loader . load ( {
79- serviceWorkerSettings : {
80- serviceWorkerVersion : { { = < % % >= } } { { flutter_service_worker_version} } < %= { { } } = % > ,
81- } ,
82- onEntrypointLoaded : async function ( engineInitializer ) {
113+ onEntrypointLoaded : async function ( engineInitializer ) {
83114 await Promise . all ( [
84115 beginPreloading ( ) ,
85116 engineInitializer . initializeEngine ( ) ,
0 commit comments