Skip to content

Commit b9a8a15

Browse files
authored
[flutter_tools] Add Cache-Control headers for web test assets (flutter#196414)
When running `flutter test --platform=chrome`, suites run sequentially inside iframes within one browser session. Because `FlutterWebPlatform` only set `Cache-Control` for local CanvasKit files, Chrome re-fetched `/flutter.js`, `/dart_sdk.js`, DDC bootstrap scripts, `*.dart.lib.js` modules, static assets, and Wasm files on every suite. This PR adds `Cache-Control: public, max-age=3600` and `Content-Length` on immutable test assets (leaving suite HTML wrappers, goldens, and WebSockets uncached), checks `dart_sdk.js.map` before `dart_sdk.js`, and root-anchors `mainWasmPath` (`/main.dart.wasm`) and `jsSupportRuntimePath` (`/main.dart.mjs`) because [`_loadWasmEntrypoint`](https://github.com/flutter/flutter/blob/b98afb52407777ee1b2544b93b064335ed56e340/engine/src/flutter/lib/web_ui/flutter_js/src/entrypoint_loader.js#L153-L197) resolves them via [`resolveUrlWithSegments`](https://github.com/flutter/flutter/blob/b98afb52407777ee1b2544b93b064335ed56e340/engine/src/flutter/lib/web_ui/flutter_js/src/utils.js#L5-L7) against `document.baseURI`. Interleaved try benchmarks (`b98afb52407` vs PR; e.g. CanvasKit [flutter#115333](https://ci.chromium.org/ui/p/flutter/builders/try/Linux%20web_canvaskit_tests_6/115333/overview) vs [flutter#115334](https://ci.chromium.org/ui/p/flutter/builders/try/Linux%20web_canvaskit_tests_6/115334/overview), Skwasm [flutter#63572](https://ci.chromium.org/ui/p/flutter/builders/try/Linux%20web_skwasm_tests_1/63572/overview) vs [flutter#63573](https://ci.chromium.org/ui/p/flutter/builders/try/Linux%20web_skwasm_tests_1/63573/overview)): | Builder (`N`/arm) | Metric | Before | After | Raw Diff (`p`) | Machine-Adjusted (`p`) | |---|---|---:|---:|---:|---:| | `Linux web_canvaskit_tests_6` (`6`) | Step time | `842.9s` | `509.0s` | `-333.8s` (`-39.6%`, `9.94e-4`) | `-293.2s` (`4.58e-8`) | | `Linux web_canvaskit_tests_6` (`6`) | Suite load | `438.8s` | `119.4s` | `-319.4s` (`-72.8%`, `1.72e-4`) | `-276.3s` (`1.78e-11`) | | `Linux web_skwasm_tests_1` (`20`) | Step time | `642.4s` | `593.9s` | `-48.5s` (`-7.5%`, `0.011`) | `-28.3s` (`2.21e-4`) | | `Linux web_skwasm_tests_1` (`20`) | Suite load | `159.7s` | `127.8s` | `-31.8s` (`-19.9%`, `1.43e-6`) | `-26.9s` (`3.72e-9`) | CanvasKit runs used `bc5f0d30e95` and Skwasm runs used `4db031bf3fd` (pooling all `30` Skwasm pairs gives `-15.6s` step, `p = 0.019`, and `-20.2s` suite load, `p = 2.12e-7`). Part of flutter#174961 May help flutter#195700
1 parent e3fcea9 commit b9a8a15

3 files changed

Lines changed: 330 additions & 92 deletions

File tree

‎packages/flutter_tools/lib/src/test/flutter_web_platform.dart‎

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ import 'test_compiler.dart';
4141
import 'test_golden_comparator.dart';
4242
import '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+
4471
shelf.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
};
335366
window.\$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,

‎packages/flutter_tools/lib/src/web/chrome.dart‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ class ChromiumLauncher {
205205
return userDataDir;
206206
}
207207
}
208+
// WARNING: `flutter test --platform chrome` depends on this directory being
209+
// new for every launch. The test server in `FlutterWebPlatform` sends
210+
// `Cache-Control: max-age` with no validator (`ETag` or `Last-Modified`),
211+
// so Chrome serves cached test assets without asking the server. A fresh
212+
// directory means an empty HTTP cache, which limits that to one invocation.
213+
//
214+
// If this directory (or its `Cache` and `Code Cache` subdirectories) is
215+
// ever reused across invocations, a later run that binds the same port can
216+
// load stale compiled test code from an earlier run. Before reusing it,
217+
// either add a per-invocation validator to those responses or put a
218+
// per-invocation token in the asset URLs.
208219
return _fileSystem.systemTempDirectory.createTempSync('flutter_tools_chrome_device.');
209220
}
210221

@@ -576,7 +587,11 @@ class ChromiumLauncher {
576587
}
577588
}
578589

579-
// Cache, Code Cache, and GPUCache are nearly 1GB of data
590+
// Cache, Code Cache, and GPUCache are nearly 1GB of data.
591+
//
592+
// Skipping `Cache` and `Code Cache` also keeps Chrome's HTTP cache from
593+
// outliving a launch, which `flutter test --platform chrome` depends on. See
594+
// the warning in [_createUserDataDirectory].
580595
bool _isNotCacheDirectory(Directory directory) {
581596
return !directory.path.endsWith('Cache') &&
582597
!directory.path.endsWith('Code Cache') &&

0 commit comments

Comments
 (0)