From cab29b203499bdab6019c0d33339ed03e90a2ab2 Mon Sep 17 00:00:00 2001 From: Severin Date: Tue, 9 May 2023 18:38:49 +0200 Subject: [PATCH] Fix platformLocation path and search dropping (#126232) This PR fixes the dropping of both the `path` and `search` fields from the platform location in the URL when using Flutter Web and brings it in par with similar technologies (e.g. React Router). It allows developers to keep the original path and/or search parameters in the URL, which are perfectly valid even while fragment routing is present. **Example use case:** Call a Flutter Web app with initial config parameters in the URL: `http://my-flutter.app/?skipIntro=true` **Example:** Before initial routing: `http://localhost:45389/?foo=bar` After routing: `http://localhost:45389/#/menu` After routing (with fix): `http://localhost:45389/?foo=bar#menu` Fixes #116415 --- .../lib/src/navigation/url_strategy.dart | 7 +++---- .../test/navigation/url_strategy_test.dart | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart b/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart index 8647627574..59294723b5 100644 --- a/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart +++ b/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart @@ -99,11 +99,10 @@ class HashUrlStrategy extends ui_web.UrlStrategy { String prepareExternalUrl(String internalUrl) { // It's convention that if the hash path is empty, we omit the `#`; however, // if the empty URL is pushed it won't replace any existing fragment. So - // when the hash path is empty, we instead return the location's path and + // when the hash path is empty, we still return the location's path and // query. - return internalUrl.isEmpty - ? '${_platformLocation.pathname}${_platformLocation.search}' - : '#$internalUrl'; + return '${_platformLocation.pathname}${_platformLocation.search}' + '${internalUrl.isEmpty ? '' : '#$internalUrl'}'; } @override diff --git a/packages/flutter_web_plugins/test/navigation/url_strategy_test.dart b/packages/flutter_web_plugins/test/navigation/url_strategy_test.dart index a797051b28..aa08f027ca 100644 --- a/packages/flutter_web_plugins/test/navigation/url_strategy_test.dart +++ b/packages/flutter_web_plugins/test/navigation/url_strategy_test.dart @@ -46,6 +46,23 @@ void main() { location.hash = '#'; expect(strategy.getPath(), '/'); }); + + test('allows location path/search before fragment', () { + const String internalUrl = '/menu?foo=bar'; + final HashUrlStrategy strategy = HashUrlStrategy(location); + + location.pathname = '/'; + expect(strategy.prepareExternalUrl(internalUrl), '/#/menu?foo=bar'); + + location.pathname = '/main'; + expect(strategy.prepareExternalUrl(internalUrl), '/main#/menu?foo=bar'); + + location.search = '?foo=bar'; + expect( + strategy.prepareExternalUrl(internalUrl), + '/main?foo=bar#/menu?foo=bar', + ); + }); }); group('$PathUrlStrategy', () {