Skip to content

Commit cab29b2

Browse files
authored
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
1 parent 8a7ded7 commit cab29b2

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ class HashUrlStrategy extends ui_web.UrlStrategy {
9999
String prepareExternalUrl(String internalUrl) {
100100
// It's convention that if the hash path is empty, we omit the `#`; however,
101101
// if the empty URL is pushed it won't replace any existing fragment. So
102-
// when the hash path is empty, we instead return the location's path and
102+
// when the hash path is empty, we still return the location's path and
103103
// query.
104-
return internalUrl.isEmpty
105-
? '${_platformLocation.pathname}${_platformLocation.search}'
106-
: '#$internalUrl';
104+
return '${_platformLocation.pathname}${_platformLocation.search}'
105+
'${internalUrl.isEmpty ? '' : '#$internalUrl'}';
107106
}
108107

109108
@override

packages/flutter_web_plugins/test/navigation/url_strategy_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ void main() {
4646
location.hash = '#';
4747
expect(strategy.getPath(), '/');
4848
});
49+
50+
test('allows location path/search before fragment', () {
51+
const String internalUrl = '/menu?foo=bar';
52+
final HashUrlStrategy strategy = HashUrlStrategy(location);
53+
54+
location.pathname = '/';
55+
expect(strategy.prepareExternalUrl(internalUrl), '/#/menu?foo=bar');
56+
57+
location.pathname = '/main';
58+
expect(strategy.prepareExternalUrl(internalUrl), '/main#/menu?foo=bar');
59+
60+
location.search = '?foo=bar';
61+
expect(
62+
strategy.prepareExternalUrl(internalUrl),
63+
'/main?foo=bar#/menu?foo=bar',
64+
);
65+
});
4966
});
5067

5168
group('$PathUrlStrategy', () {

0 commit comments

Comments
 (0)