[flutter_tools] adds etag/cache control header to debug asset server (#51143)
This commit is contained in:
@@ -137,6 +137,10 @@ class WebAssetServer implements AssetReader {
|
||||
return shelf.Response.notFound('');
|
||||
}
|
||||
|
||||
// Track etag headers for better caching of resources.
|
||||
final String ifNoneMatch = request.headers[HttpHeaders.ifNoneMatchHeader];
|
||||
headers[HttpHeaders.cacheControlHeader] = 'max-age=0, must-revalidate';
|
||||
|
||||
// NOTE: shelf removes leading `/` for some reason.
|
||||
final String requestPath = request.url.path.startsWith('/')
|
||||
? request.url.path
|
||||
@@ -146,16 +150,29 @@ class WebAssetServer implements AssetReader {
|
||||
// Attempt to look up the file by URI.
|
||||
if (_files.containsKey(requestPath)) {
|
||||
final List<int> bytes = getFile(requestPath);
|
||||
// Use the underlying buffer hashCode as a revision string. This buffer is
|
||||
// replaced whenever the frontend_server produces new output files, which
|
||||
// will also change the hashCode.
|
||||
final String etag = bytes.hashCode.toString();
|
||||
if (ifNoneMatch == etag) {
|
||||
return shelf.Response.notModified();
|
||||
}
|
||||
headers[HttpHeaders.contentLengthHeader] = bytes.length.toString();
|
||||
headers[HttpHeaders.contentTypeHeader] = 'application/javascript';
|
||||
headers[HttpHeaders.etagHeader] = etag;
|
||||
return shelf.Response.ok(bytes, headers: headers);
|
||||
}
|
||||
// If this is a sourcemap file, then it might be in the in-memory cache.
|
||||
// Attempt to lookup the file by URI.
|
||||
if (_sourcemaps.containsKey(requestPath)) {
|
||||
final List<int> bytes = getSourceMap(requestPath);
|
||||
final String etag = bytes.hashCode.toString();
|
||||
if (ifNoneMatch == etag) {
|
||||
return shelf.Response.notModified();
|
||||
}
|
||||
headers[HttpHeaders.contentLengthHeader] = bytes.length.toString();
|
||||
headers[HttpHeaders.contentTypeHeader] = 'application/json';
|
||||
headers[HttpHeaders.etagHeader] = etag;
|
||||
return shelf.Response.ok(bytes, headers: headers);
|
||||
}
|
||||
|
||||
@@ -172,6 +189,13 @@ class WebAssetServer implements AssetReader {
|
||||
if (!file.existsSync()) {
|
||||
return shelf.Response.notFound('');
|
||||
}
|
||||
|
||||
// For real files, use a serialized file stat as a revision
|
||||
final String etag = file.lastModifiedSync().toIso8601String();
|
||||
if (ifNoneMatch == etag) {
|
||||
return shelf.Response.notModified();
|
||||
}
|
||||
|
||||
final int length = file.lengthSync();
|
||||
// Attempt to determine the file's mime type. if this is not provided some
|
||||
// browsers will refuse to render images/show video et cetera. If the tool
|
||||
@@ -186,6 +210,7 @@ class WebAssetServer implements AssetReader {
|
||||
mimeType ??= _kDefaultMimeType;
|
||||
headers[HttpHeaders.contentLengthHeader] = length.toString();
|
||||
headers[HttpHeaders.contentTypeHeader] = mimeType;
|
||||
headers[HttpHeaders.etagHeader] = etag;
|
||||
return shelf.Response.ok(file.openRead(), headers: headers);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user