@@ -2675,4 +2675,200 @@ flutter:
26752675 expect (configString, contains ('"main.dart.89abcdef.wasm":' ));
26762676 }),
26772677 );
2678+
2679+ test (
2680+ 'WebServiceWorker emits precache_manifest.json and excludes non-runtime bloat when webContentHash is true' ,
2681+ () => testbed.run (() async {
2682+ final File fileGenerators =
2683+ environment.artifacts.getArtifactPath (Artifact .flutterToolsFileGenerators).isEmpty
2684+ ? environment.fileSystem.file ('flutter_service_worker.js' )
2685+ : environment.fileSystem.file (
2686+ environment.fileSystem.path.join (
2687+ environment.artifacts.getArtifactPath (Artifact .flutterToolsFileGenerators),
2688+ 'js' ,
2689+ 'flutter_service_worker.js' ,
2690+ ),
2691+ );
2692+ fileGenerators
2693+ ..createSync (recursive: true )
2694+ ..writeAsStringSync ('// service worker stub' );
2695+
2696+ const indexBytes = < int > [1 , 2 , 3 , 4 ];
2697+ const bootstrapBytes = < int > [5 , 6 , 7 , 8 , 9 ];
2698+ const mainJsBytes = < int > [10 , 20 , 30 , 40 , 50 , 60 ];
2699+ const logoBytes = < int > [70 , 80 , 90 ];
2700+ const manifestBinBytes = < int > [11 , 22 , 33 ];
2701+ const extensionlessAssetBytes = < int > [44 , 55 , 66 ];
2702+
2703+ final String indexHash = crypto.sha256.convert (indexBytes).toString ().substring (0 , 8 );
2704+ final String bootstrapHash = crypto.sha256.convert (bootstrapBytes).toString ().substring (0 , 8 );
2705+ final String mainJsHash = crypto.sha256.convert (mainJsBytes).toString ().substring (0 , 8 );
2706+ final String logoHash = crypto.sha256.convert (logoBytes).toString ().substring (0 , 8 );
2707+ final String manifestBinHash = crypto.sha256
2708+ .convert (manifestBinBytes)
2709+ .toString ()
2710+ .substring (0 , 8 );
2711+ final String extensionlessHash = crypto.sha256
2712+ .convert (extensionlessAssetBytes)
2713+ .toString ()
2714+ .substring (0 , 8 );
2715+
2716+ environment.outputDir.childFile ('index.html' )
2717+ ..createSync (recursive: true )
2718+ ..writeAsBytesSync (indexBytes);
2719+ environment.outputDir.childFile ('flutter_bootstrap.js' )
2720+ ..createSync (recursive: true )
2721+ ..writeAsBytesSync (bootstrapBytes);
2722+ environment.outputDir.childFile ('main.dart.$mainJsHash .js' )
2723+ ..createSync (recursive: true )
2724+ ..writeAsBytesSync (mainJsBytes);
2725+ environment.outputDir.childFile ('assets/images/logo.$logoHash .png' )
2726+ ..createSync (recursive: true )
2727+ ..writeAsBytesSync (logoBytes);
2728+ environment.outputDir.childFile ('assets/AssetManifest.bin' )
2729+ ..createSync (recursive: true )
2730+ ..writeAsBytesSync (manifestBinBytes);
2731+ environment.outputDir.childFile ('assets/RAW_DATA.$extensionlessHash ' )
2732+ ..createSync (recursive: true )
2733+ ..writeAsBytesSync (extensionlessAssetBytes);
2734+
2735+ // Non-runtime files that must be excluded from precache_manifest.json:
2736+ environment.outputDir.childFile ('main.dart.$mainJsHash .js.map' )
2737+ ..createSync (recursive: true )
2738+ ..writeAsStringSync ('{}' );
2739+ environment.outputDir.childFile ('main.dart.wasm.symbols' )
2740+ ..createSync (recursive: true )
2741+ ..writeAsStringSync ('symbols' );
2742+ environment.outputDir.childFile ('main.dart.js.info.json' )
2743+ ..createSync (recursive: true )
2744+ ..writeAsStringSync ('{}' );
2745+ environment.outputDir.childFile ('.last_build_id' )
2746+ ..createSync (recursive: true )
2747+ ..writeAsStringSync ('build-id' );
2748+ environment.outputDir.childFile ('canvaskit/canvaskit.wasm' )
2749+ ..createSync (recursive: true )
2750+ ..writeAsBytesSync (< int > [0 , 97 , 115 , 109 ]);
2751+
2752+ final target = WebServiceWorker (environment.fileSystem, const < WebCompilerConfig > [
2753+ JsCompilerConfig (webContentHash: true ),
2754+ ], const NoOpAnalytics ());
2755+ await target.build (environment);
2756+
2757+ final File manifestFile = environment.outputDir.childFile ('precache_manifest.json' );
2758+ expect (manifestFile, exists);
2759+
2760+ final decoded = jsonDecode (manifestFile.readAsStringSync ()) as Map <String , Object ?>;
2761+ expect (decoded['version' ], 1 );
2762+
2763+ final entries = decoded['entries' ]! as List <Object ?>;
2764+ expect (entries, < Map <String , Object >> [
2765+ < String , Object > {
2766+ 'url' : 'assets/AssetManifest.bin' ,
2767+ 'hash' : manifestBinHash,
2768+ 'size' : manifestBinBytes.length,
2769+ 'urlHashed' : false ,
2770+ },
2771+ < String , Object > {
2772+ 'url' : 'assets/RAW_DATA.$extensionlessHash ' ,
2773+ 'hash' : extensionlessHash,
2774+ 'size' : extensionlessAssetBytes.length,
2775+ 'urlHashed' : true ,
2776+ },
2777+ < String , Object > {
2778+ 'url' : 'assets/images/logo.$logoHash .png' ,
2779+ 'hash' : logoHash,
2780+ 'size' : logoBytes.length,
2781+ 'urlHashed' : true ,
2782+ },
2783+ < String , Object > {
2784+ 'url' : 'flutter_bootstrap.js' ,
2785+ 'hash' : bootstrapHash,
2786+ 'size' : bootstrapBytes.length,
2787+ 'urlHashed' : false ,
2788+ },
2789+ < String , Object > {
2790+ 'url' : 'index.html' ,
2791+ 'hash' : indexHash,
2792+ 'size' : indexBytes.length,
2793+ 'urlHashed' : false ,
2794+ },
2795+ < String , Object > {
2796+ 'url' : 'main.dart.$mainJsHash .js' ,
2797+ 'hash' : mainJsHash,
2798+ 'size' : mainJsBytes.length,
2799+ 'urlHashed' : true ,
2800+ },
2801+ ]);
2802+
2803+ final File serviceWorkerDepfile = environment.buildDir.childFile ('service_worker.d' );
2804+ expect (serviceWorkerDepfile, exists);
2805+ expect (serviceWorkerDepfile.readAsStringSync (), contains ('precache_manifest.json:' ));
2806+ }),
2807+ );
2808+
2809+ test (
2810+ 'WebServiceWorker includes canvaskit in precache_manifest.json when kUseLocalCanvasKitFlag is true' ,
2811+ () => testbed.run (() async {
2812+ environment.defines[kUseLocalCanvasKitFlag] = 'true' ;
2813+ final File fileGenerators = environment.fileSystem.file (
2814+ environment.fileSystem.path.join (
2815+ environment.artifacts.getArtifactPath (Artifact .flutterToolsFileGenerators),
2816+ 'js' ,
2817+ 'flutter_service_worker.js' ,
2818+ ),
2819+ );
2820+ fileGenerators
2821+ ..createSync (recursive: true )
2822+ ..writeAsStringSync ('// service worker stub' );
2823+
2824+ const wasmBytes = < int > [0 , 97 , 115 , 109 ];
2825+ final String wasmHash = crypto.sha256.convert (wasmBytes).toString ().substring (0 , 8 );
2826+ environment.outputDir.childFile ('canvaskit/canvaskit.wasm' )
2827+ ..createSync (recursive: true )
2828+ ..writeAsBytesSync (wasmBytes);
2829+
2830+ final target = WebServiceWorker (environment.fileSystem, const < WebCompilerConfig > [
2831+ JsCompilerConfig (webContentHash: true ),
2832+ ], const NoOpAnalytics ());
2833+ await target.build (environment);
2834+
2835+ final File manifestFile = environment.outputDir.childFile ('precache_manifest.json' );
2836+ final decoded = jsonDecode (manifestFile.readAsStringSync ()) as Map <String , Object ?>;
2837+ expect (decoded['entries' ], < Map <String , Object >> [
2838+ < String , Object > {
2839+ 'url' : 'canvaskit/canvaskit.wasm' ,
2840+ 'hash' : wasmHash,
2841+ 'size' : wasmBytes.length,
2842+ 'urlHashed' : false ,
2843+ },
2844+ ]);
2845+ }),
2846+ );
2847+
2848+ test (
2849+ 'WebServiceWorker deletes stale precache_manifest.json when webContentHash is false' ,
2850+ () => testbed.run (() async {
2851+ final File fileGenerators = environment.fileSystem.file (
2852+ environment.fileSystem.path.join (
2853+ environment.artifacts.getArtifactPath (Artifact .flutterToolsFileGenerators),
2854+ 'js' ,
2855+ 'flutter_service_worker.js' ,
2856+ ),
2857+ );
2858+ fileGenerators
2859+ ..createSync (recursive: true )
2860+ ..writeAsStringSync ('// service worker stub' );
2861+
2862+ final File staleManifest = environment.outputDir.childFile ('precache_manifest.json' )
2863+ ..createSync (recursive: true )
2864+ ..writeAsStringSync ('{"version":1,"entries":[]}' );
2865+
2866+ final target = WebServiceWorker (environment.fileSystem, const < WebCompilerConfig > [
2867+ JsCompilerConfig (),
2868+ ], const NoOpAnalytics ());
2869+ await target.build (environment);
2870+
2871+ expect (staleManifest, isNot (exists));
2872+ }),
2873+ );
26782874}
0 commit comments