diff --git a/engine/src/flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/url_strategy.dart b/engine/src/flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/url_strategy.dart index 4d9e383e20..7932221eb3 100644 --- a/engine/src/flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/url_strategy.dart +++ b/engine/src/flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/url_strategy.dart @@ -179,8 +179,17 @@ class HashUrlStrategy implements UrlStrategy { // if the empty URL is pushed it won't replace any existing fragment. So // when the hash path is empty, we still return the location's path and // query. - return '${_platformLocation.pathname}${_platformLocation.search}' - '${internalUrl.isEmpty ? '' : '#$internalUrl'}'; + final String hash; + if (internalUrl.isEmpty || internalUrl == '/') { + // Let's not add the hash at all when the app is in the home page. That + // way, the URL of the home page is cleaner. + // + // See: https://github.com/flutter/flutter/issues/127608 + hash = ''; + } else { + hash = '#$internalUrl'; + } + return '${_platformLocation.pathname}${_platformLocation.search}$hash'; } @override diff --git a/engine/src/flutter/lib/web_ui/test/engine/history_test.dart b/engine/src/flutter/lib/web_ui/test/engine/history_test.dart index b661385e1d..e34c452641 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/history_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/history_test.dart @@ -663,6 +663,23 @@ void testMain() { ); }); + test('removes /#/ from the home page', () { + const String internalUrl = '/'; + final HashUrlStrategy strategy = HashUrlStrategy(location); + + location.pathname = '/'; + expect(strategy.prepareExternalUrl(internalUrl), '/'); + + location.pathname = '/main'; + expect(strategy.prepareExternalUrl(internalUrl), '/main'); + + location.search = '?foo=bar'; + expect( + strategy.prepareExternalUrl(internalUrl), + '/main?foo=bar', + ); + }); + test('addPopStateListener fn unwraps DomPopStateEvent state', () { final HashUrlStrategy strategy = HashUrlStrategy(location); const String expected = 'expected value';