From b42e34690ae42335f535dab31e6dab7923157269 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Fri, 2 Oct 2020 15:12:04 -0700 Subject: [PATCH] [web] Update index.html template to support new path strategy (#67081) --- .../lib/src/isolated/devfs_web.dart | 65 ++++++++++++++- packages/flutter_tools/pubspec.yaml | 2 +- .../templates/app/web/index.html.tmpl | 12 +++ .../general.shard/web/devfs_web_test.dart | 80 ++++++++++++++++++- 4 files changed, 153 insertions(+), 6 deletions(-) diff --git a/packages/flutter_tools/lib/src/isolated/devfs_web.dart b/packages/flutter_tools/lib/src/isolated/devfs_web.dart index 83ff7b443c..e6ce463712 100644 --- a/packages/flutter_tools/lib/src/isolated/devfs_web.dart +++ b/packages/flutter_tools/lib/src/isolated/devfs_web.dart @@ -7,6 +7,8 @@ import 'dart:typed_data'; import 'package:dwds/data/build_result.dart'; import 'package:dwds/dwds.dart'; +import 'package:html/dom.dart'; +import 'package:html/parser.dart'; import 'package:logging/logging.dart' as logging; import 'package:meta/meta.dart'; import 'package:mime/mime.dart' as mime; @@ -56,6 +58,9 @@ typedef DwdsLauncher = Future Function({ // A minimal index for projects that do not yet support web. const String _kDefaultIndex = ''' + + + @@ -108,7 +113,9 @@ class WebAssetServer implements AssetReader { this._modules, this._digests, this._buildInfo, - ); + ) : basePath = _parseBasePathFromIndexHtml(globals.fs.currentDirectory + .childDirectory('web') + .childFile('index.html')); // Fallback to "application/octet-stream" on null which // makes no claims as to the structure of the data. @@ -1009,17 +1016,67 @@ Future _loadDwdsDirectory(FileSystem fileSystem, Logger logger) async } String _stripBasePath(String path, String basePath) { - while (path.startsWith('/')) { - path = path.substring(1); - } + path = _stripLeadingSlashes(path); if (path.startsWith(basePath)) { path = path.substring(basePath.length); } else { // The given path isn't under base path, return null to indicate that. return null; } + return _stripLeadingSlashes(path); +} + +String _stripLeadingSlashes(String path) { while (path.startsWith('/')) { path = path.substring(1); } return path; } + +String _stripTrailingSlashes(String path) { + while (path.endsWith('/')) { + path = path.substring(0, path.length - 1); + } + return path; +} + +String _parseBasePathFromIndexHtml(File indexHtml) { + final String htmlContent = indexHtml.existsSync() + ? indexHtml.readAsStringSync() + : _kDefaultIndex; + + final Document document = parse(htmlContent); + final Element baseElement = document.querySelector('base'); + String baseHref = baseElement?.attributes == null ? null : baseElement.attributes['href']; + + if (baseHref == null) { + baseHref = ''; + } else if (!baseHref.startsWith('/')) { + throw ToolExit( + 'Error: The base href in "web/index.html" must be absolute (i.e. start ' + 'with a "/"), but found: `${baseElement.outerHtml}`.\n' + '$basePathExample', + ); + } else if (!baseHref.endsWith('/')) { + throw ToolExit( + 'Error: The base href in "web/index.html" must end with a "/", but found: `${baseElement.outerHtml}`.\n' + '$basePathExample', + ); + } else { + baseHref = _stripLeadingSlashes(_stripTrailingSlashes(baseHref)); + } + + return baseHref; +} + +const String basePathExample = ''' +For example, to serve from the root use: + + + +To serve from a subpath "foo" (i.e. http://localhost:8080/foo/ instead of http://localhost:8080/) use: + + + +For more information, see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base +'''; diff --git a/packages/flutter_tools/pubspec.yaml b/packages/flutter_tools/pubspec.yaml index c79d0d9bc4..f738e2a6ad 100644 --- a/packages/flutter_tools/pubspec.yaml +++ b/packages/flutter_tools/pubspec.yaml @@ -18,6 +18,7 @@ dependencies: crypto: 2.1.5 file: 6.0.0-nullsafety.2 flutter_template_images: 1.0.1 + html: 0.14.0+3 http: 0.12.2 intl: 0.16.1 meta: 1.3.0-nullsafety.3 @@ -74,7 +75,6 @@ dependencies: devtools_shared: 0.8.0+3 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" fixnum: 0.10.11 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" glob: 1.2.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" - html: 0.14.0+3 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" http_parser: 3.1.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" io: 0.3.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" js: 0.6.3-nullsafety.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" diff --git a/packages/flutter_tools/templates/app/web/index.html.tmpl b/packages/flutter_tools/templates/app/web/index.html.tmpl index bc51a4a5c7..8f42214c65 100644 --- a/packages/flutter_tools/templates/app/web/index.html.tmpl +++ b/packages/flutter_tools/templates/app/web/index.html.tmpl @@ -1,6 +1,18 @@ + + + diff --git a/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart b/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart index dae427070d..0ecc5949d9 100644 --- a/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart +++ b/packages/flutter_tools/test/general.shard/web/devfs_web_test.dart @@ -55,7 +55,6 @@ void main() { null, null, null, - ); }); }); @@ -215,6 +214,85 @@ void main() { expect(response.statusCode, HttpStatus.notFound); })); + test('parses base path from index.html', () => testbed.run(() async { + const String htmlContent = ''; + final Directory webDir = globals.fs.currentDirectory + .childDirectory('web') + ..createSync(); + webDir.childFile('index.html').writeAsStringSync(htmlContent); + + final WebAssetServer webAssetServer = WebAssetServer( + mockHttpServer, + packages, + InternetAddress.loopbackIPv4, + null, + null, + null, + ); + + expect(webAssetServer.basePath, 'foo/bar'); + })); + + test('handles lack of base path in index.html', () => testbed.run(() async { + const String htmlContent = ''; + final Directory webDir = globals.fs.currentDirectory + .childDirectory('web') + ..createSync(); + webDir.childFile('index.html').writeAsStringSync(htmlContent); + + final WebAssetServer webAssetServer = WebAssetServer( + mockHttpServer, + packages, + InternetAddress.loopbackIPv4, + null, + null, + null, + ); + + // Defaults to "/" when there's no base element. + expect(webAssetServer.basePath, ''); + })); + + test('throws if base path is relative', () => testbed.run(() async { + const String htmlContent = ''; + final Directory webDir = globals.fs.currentDirectory + .childDirectory('web') + ..createSync(); + webDir.childFile('index.html').writeAsStringSync(htmlContent); + + expect( + () => WebAssetServer( + mockHttpServer, + packages, + InternetAddress.loopbackIPv4, + null, + null, + null, + ), + throwsToolExit(), + ); + })); + + test('throws if base path does not end with slash', () => testbed.run(() async { + const String htmlContent = ''; + final Directory webDir = globals.fs.currentDirectory + .childDirectory('web') + ..createSync(); + webDir.childFile('index.html').writeAsStringSync(htmlContent); + + expect( + () => WebAssetServer( + mockHttpServer, + packages, + InternetAddress.loopbackIPv4, + null, + null, + null, + ), + throwsToolExit(), + ); + })); + test('serves JavaScript files from in memory cache not from manifest', () => testbed.run(() async { webAssetServer.writeFile('foo.js', 'main() {}');