From 1574da58fb5ed2518a8ad85b16a3d88db6a30b5f Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Tue, 6 Jun 2023 17:33:34 -0400 Subject: [PATCH] [web] Remove /#/ from home page URL (flutter/engine#42598) To make the `HashUrlStrategy` a bit cleaner (at least for the home page), let's get rid of the notorious `/#/` when the app is in the home page. Non-home pages continue to have a hash in them. After this PR, here are some example URL changes: - `http://domain.com/#/` => `http://domain.com/` - `http://domain.com/#/page1` => (remains the same) This change is backwards compatible, i.e. if you write `http://domain.com/#/` in the address bar, the app will load and open the home page correctly; and flutter will automatically change the URL to `http://domain.com/`. Fixes https://github.com/flutter/flutter/issues/127608 --- .../src/ui_web/navigation/url_strategy.dart | 13 +++++++++++-- .../lib/web_ui/test/engine/history_test.dart | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) 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';