@@ -41,6 +41,33 @@ import 'test_compiler.dart';
4141import 'test_golden_comparator.dart' ;
4242import 'test_time_recorder.dart' ;
4343
44+ /// Cache-Control header applied to test server assets that remain immutable
45+ /// for the lifetime of a single `flutter test` invocation.
46+ ///
47+ /// These responses carry no validator (`ETag` or `Last-Modified` ), so Chrome
48+ /// serves a cached copy without asking the server until `max-age` elapses.
49+ /// This is only safe because the cache cannot outlive the invocation:
50+ /// [ChromiumLauncher.launch] creates a fresh `--user-data-dir` for every
51+ /// launch, and the tool deletes that directory when it exits. The code that
52+ /// creates the directory carries a matching warning.
53+ const _kStaticAssetCacheControl = 'public, max-age=3600' ;
54+
55+ shelf.Response _cachedFileResponse (
56+ File file, {
57+ String ? contentType,
58+ Map <String , String > extraHeaders = const < String , String > {},
59+ }) {
60+ return shelf.Response .ok (
61+ file.openRead (),
62+ headers: < String , Object > {
63+ HttpHeaders .contentTypeHeader: ? contentType,
64+ HttpHeaders .cacheControlHeader: _kStaticAssetCacheControl,
65+ HttpHeaders .contentLengthHeader: file.lengthSync ().toString (),
66+ ...extraHeaders,
67+ },
68+ );
69+ }
70+
4471shelf.Handler createDirectoryHandler (Directory directory, {required bool crossOriginIsolated}) {
4572 final resolver = mime.MimeTypeResolver ();
4673 final FileSystem fileSystem = directory.fileSystem;
@@ -58,12 +85,12 @@ shelf.Handler createDirectoryHandler(Directory directory, {required bool crossOr
5885 }
5986 final String ? contentType = resolver.lookup (file.path);
6087 final bool needsCrossOriginIsolated = crossOriginIsolated && uriPath.endsWith ('.html' );
61- return shelf. Response . ok (
62- file. openRead () ,
63- headers : < String , String > {
64- 'Content-Type' : ? contentType,
65- if (needsCrossOriginIsolated) ... kCrossOriginIsolationHeaders,
66- },
88+ return _cachedFileResponse (
89+ file,
90+ contentType : contentType,
91+ extraHeaders : needsCrossOriginIsolated
92+ ? kCrossOriginIsolationHeaders
93+ : const < String , String > { },
6794 );
6895 };
6996}
@@ -191,16 +218,10 @@ class FlutterWebPlatform extends PlatformPlugin {
191218 final PackageConfig packageConfig = await currentPackageConfig (toolContext: toolContext);
192219 testPackageUri = packageConfig['test' ]! .packageUriRoot;
193220 }
194- final File testDartJs = fs.file (fs.path.join (testPackageUri.toFilePath (), 'dart.js' ));
221+ final String testPackagePath = fs.path.fromUri (testPackageUri);
222+ final File testDartJs = fs.file (fs.path.join (testPackagePath, 'dart.js' ));
195223 final File testHostDartJs = fs.file (
196- fs.path.join (
197- testPackageUri.toFilePath (),
198- 'src' ,
199- 'runner' ,
200- 'browser' ,
201- 'static' ,
202- 'host.dart.js' ,
203- ),
224+ fs.path.join (testPackagePath, 'src' , 'runner' , 'browser' , 'static' , 'host.dart.js' ),
204225 );
205226 return FlutterWebPlatform ._(
206227 server,
@@ -287,16 +308,26 @@ class FlutterWebPlatform extends PlatformPlugin {
287308 'canvaskit' ,
288309 );
289310
311+ shelf.Response _cachedBodyResponse (Object ? body, {required String contentType}) {
312+ return shelf.Response .ok (
313+ body,
314+ headers: < String , Object > {
315+ HttpHeaders .contentTypeHeader: contentType,
316+ HttpHeaders .cacheControlHeader: _kStaticAssetCacheControl,
317+ },
318+ );
319+ }
320+
290321 Future <shelf.Response > _handleTestRequest (shelf.Request request) async {
291322 if (request.url.path.endsWith ('main.dart.browser_test.dart.js' )) {
292- return shelf. Response . ok (
323+ return _cachedBodyResponse (
293324 generateDDCLibraryBundleBootstrapScript (
294325 ddcModuleLoaderUrl: 'ddc_module_loader.js' ,
295326 mapperUrl: 'dart_stack_trace_mapper.js' ,
296327 generateLoadingIndicator: false ,
297328 isWindows: _toolContext.platform.isWindows,
298329 ),
299- headers : < String , String > { HttpHeaders .contentTypeHeader : 'text/javascript' } ,
330+ contentType : 'text/javascript' ,
300331 );
301332 }
302333 if (request.url.path.endsWith ('main_module.bootstrap.js' )) {
@@ -334,88 +365,60 @@ window.\$dartLoader.loadConfig.loadScriptFn = function(loader) {
334365};
335366window.\$ dartLoader.loader.nextAttempt();
336367''' ;
337- return shelf.Response .ok (
338- mainModuleSrc,
339- headers: < String , String > {HttpHeaders .contentTypeHeader: 'text/javascript' },
340- );
368+ return _cachedBodyResponse (mainModuleSrc, contentType: 'text/javascript' );
341369 }
342370 if (request.url.path.endsWith ('on_load_end_bootstrap.js' )) {
343- return shelf. Response . ok (
371+ return _cachedBodyResponse (
344372 generateDDCLibraryBundleOnLoadEndBootstrap (),
345- headers : < String , String > { HttpHeaders .contentTypeHeader : 'text/javascript' } ,
373+ contentType : 'text/javascript' ,
346374 );
347375 }
348376 if (request.url.path.endsWith ('.dart.lib.js' )) {
349377 final String path = request.url.path;
350- return shelf.Response .ok (
351- webMemoryFS.files[path],
352- headers: < String , String > {HttpHeaders .contentTypeHeader: 'text/javascript' },
353- );
378+ return _cachedBodyResponse (webMemoryFS.files[path], contentType: 'text/javascript' );
354379 }
355380 if (request.url.path.endsWith ('.lib.js.map' )) {
356- return shelf. Response . ok (
381+ return _cachedBodyResponse (
357382 webMemoryFS.sourcemaps[request.url.path],
358- headers : < String , String > { HttpHeaders .contentTypeHeader : 'text/plain' } ,
383+ contentType : 'text/plain' ,
359384 );
360385 }
361386 return shelf.Response .notFound ('' );
362387 }
363388
364389 Future <shelf.Response > _handleStaticArtifact (shelf.Request request) async {
365- if (request.requestedUri.path.contains ('ddc_module_loader.js' )) {
366- return shelf.Response .ok (
367- _ddcModuleLoaderJs.openRead (),
368- headers: < String , String > {'Content-Type' : 'text/javascript' },
369- );
370- } else if (request.requestedUri.path.contains ('ahem.ttf' )) {
371- return shelf.Response .ok (_ahem.openRead ());
372- } else if (request.requestedUri.path.contains ('dart_sdk.js' )) {
373- return shelf.Response .ok (
374- _dartSdk.openRead (),
375- headers: < String , String > {'Content-Type' : 'text/javascript' },
376- );
377- } else if (request.requestedUri.path.contains ('dart_sdk.js.map' )) {
378- return shelf.Response .ok (
379- _dartSdkSourcemaps.openRead (),
380- headers: < String , String > {'Content-Type' : 'text/javascript' },
381- );
382- } else if (request.requestedUri.path.contains ('dart_stack_trace_mapper.js' )) {
383- return shelf.Response .ok (
384- _stackTraceMapper.openRead (),
385- headers: < String , String > {'Content-Type' : 'text/javascript' },
386- );
387- } else if (request.requestedUri.path.contains ('static/dart.js' )) {
388- return shelf.Response .ok (
389- _testDartJs.openRead (),
390- headers: < String , String > {'Content-Type' : 'text/javascript' },
391- );
392- } else if (request.requestedUri.path.contains ('host.dart.js' )) {
393- return shelf.Response .ok (
394- _testHostDartJs.openRead (),
395- headers: < String , String > {'Content-Type' : 'text/javascript' },
396- );
397- } else if (request.requestedUri.path.contains ('flutter.js' )) {
398- return shelf.Response .ok (
399- _flutterJs.openRead (),
400- headers: < String , String > {'Content-Type' : 'text/javascript' },
401- );
402- } else if (request.requestedUri.path.contains ('main.dart.mjs' )) {
403- return shelf.Response .ok (
404- _buildDirectory.childFile ('main.dart.mjs' ).openRead (),
405- headers: < String , String > {'Content-Type' : 'text/javascript' },
406- );
407- } else if (request.requestedUri.path.contains ('main.dart.wasm' )) {
408- final File wasmFile = _buildDirectory.childFile ('main.dart.wasm' );
409- return shelf.Response .ok (
410- wasmFile.openRead (),
411- headers: < String , String > {
412- HttpHeaders .contentTypeHeader: 'application/wasm' ,
413- HttpHeaders .contentLengthHeader: wasmFile.lengthSync ().toString (),
414- },
415- );
390+ final String path = request.requestedUri.path;
391+ final File file;
392+ String ? contentType = 'text/javascript' ;
393+ if (path.contains ('ddc_module_loader.js' )) {
394+ file = _ddcModuleLoaderJs;
395+ } else if (path.contains ('ahem.ttf' )) {
396+ file = _ahem;
397+ contentType = null ;
398+ } else if (path.contains ('dart_sdk.js.map' )) {
399+ file = _dartSdkSourcemaps;
400+ } else if (path.contains ('dart_sdk.js' )) {
401+ file = _dartSdk;
402+ } else if (path.contains ('dart_stack_trace_mapper.js' )) {
403+ file = _stackTraceMapper;
404+ } else if (path.contains ('static/dart.js' )) {
405+ file = _testDartJs;
406+ } else if (path.contains ('host.dart.js' )) {
407+ file = _testHostDartJs;
408+ } else if (path.contains ('flutter.js' )) {
409+ file = _flutterJs;
410+ } else if (path.contains ('main.dart.mjs' )) {
411+ file = _buildDirectory.childFile ('main.dart.mjs' );
412+ } else if (path.contains ('main.dart.wasm' )) {
413+ file = _buildDirectory.childFile ('main.dart.wasm' );
414+ contentType = 'application/wasm' ;
416415 } else {
417416 return shelf.Response .notFound ('Not Found' );
418417 }
418+ if (! file.existsSync ()) {
419+ return shelf.Response .notFound ('Not Found' );
420+ }
421+ return _cachedFileResponse (file, contentType: contentType);
419422 }
420423
421424 FutureOr <shelf.Response > _packageFilesHandler (shelf.Request request) async {
@@ -503,14 +506,7 @@ window.\$dartLoader.loader.nextAttempt();
503506 }
504507
505508 final File canvasKitFile = _fileSystem.file (filePath);
506- return shelf.Response .ok (
507- canvasKitFile.openRead (),
508- headers: < String , Object > {
509- HttpHeaders .contentTypeHeader: contentType,
510- HttpHeaders .cacheControlHeader: 'public, max-age=3600' ,
511- HttpHeaders .contentLengthHeader: canvasKitFile.lengthSync ().toString (),
512- },
513- );
509+ return _cachedFileResponse (canvasKitFile, contentType: contentType);
514510 }
515511
516512 String _makeBuildConfigString () {
@@ -519,8 +515,8 @@ window.\$dartLoader.loader.nextAttempt();
519515 {
520516 compileTarget: "dart2wasm",
521517 renderer: "${webRenderer .name }",
522- mainWasmPath: "main.dart.wasm",
523- jsSupportRuntimePath: "main.dart.mjs",
518+ mainWasmPath: "/ main.dart.wasm",
519+ jsSupportRuntimePath: "/ main.dart.mjs",
524520 }
525521'''
526522 : '''
@@ -957,6 +953,11 @@ class BrowserManager {
957953 required Logger logger,
958954 required SystemClock systemClock,
959955 }) async {
956+ // WARNING: Do not pass a reused `--user-data-dir` here. The cached test
957+ // assets (see [_kStaticAssetCacheControl]) have no validator, so a Chrome
958+ // HTTP cache that survives this invocation can serve stale compiled test
959+ // code to a later run. [ChromiumLauncher.launch] creates a fresh profile
960+ // when no `--user-data-dir` flag is given.
960961 final Chromium chrome = await chromiumLauncher.launch (
961962 url.toString (),
962963 headless: headless,
0 commit comments