Skip to content

Commit

Permalink
Fix platformLocation path and search dropping (#126232)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Bungeefan committed May 9, 2023
1 parent 8a7ded7 commit cab29b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Expand Up @@ -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
Expand Down
Expand Up @@ -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', () {
Expand Down

0 comments on commit cab29b2

Please sign in to comment.