Skip to content

Commit 43407cf

Browse files
authored
Access to fragment in router state on page refresh (flutter#131123)
This PR fixes the issue flutter#108614 Particularly this behaviour flutter#108614 (comment)
1 parent da0f9a9 commit 43407cf

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class PathUrlStrategy extends ui_web.HashUrlStrategy {
4646
/// interactions.
4747
PathUrlStrategy([
4848
super.platformLocation,
49+
this.includeHash = false,
4950
]) : _platformLocation = platformLocation,
5051
_basePath = stripTrailingSlash(extractPathname(checkBaseHref(
5152
platformLocation.getBaseHref(),
@@ -54,9 +55,20 @@ class PathUrlStrategy extends ui_web.HashUrlStrategy {
5455
final ui_web.PlatformLocation _platformLocation;
5556
final String _basePath;
5657

58+
/// There were an issue with url #hash which disappears from URL on first start of the web application
59+
/// This flag allows to preserve that hash and was introduced mainly to preserve backward compatibility
60+
/// with existing applications that rely on a full match on the path. If someone navigates to
61+
/// /profile or /profile#foo, they both will work without this flag otherwise /profile#foo won't match
62+
/// with the /profile route name anymore because the hash became part of the path.
63+
///
64+
/// This flag solves the edge cases when using auth provider which redirects back to the app with
65+
/// token in redirect URL as /#access_token=bla_bla_bla
66+
final bool includeHash;
67+
5768
@override
5869
String getPath() {
59-
final String path = _platformLocation.pathname + _platformLocation.search;
70+
final String? hash = includeHash ? _platformLocation.hash : null;
71+
final String path = _platformLocation.pathname + _platformLocation.search + (hash ?? '');
6072
if (_basePath.isNotEmpty && path.startsWith(_basePath)) {
6173
return ensureLeadingSlash(path.substring(_basePath.length));
6274
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,5 @@ class PathUrlStrategy extends HashUrlStrategy {
129129
///
130130
/// The [PlatformLocation] parameter is useful for testing to mock out browser
131131
/// integrations.
132-
const PathUrlStrategy([PlatformLocation? _]);
132+
const PathUrlStrategy([PlatformLocation? _, bool __ = false,]);
133133
}

packages/flutter_web_plugins/test/navigation/url_strategy_test.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,34 @@ void main() {
8080
expect(strategy.getPath(), '/bar');
8181
});
8282

83-
test('gets path correctly in the presence of query params', () {
83+
test('gets path correctly in the presence of query params and omits fragment if no flag specified', () {
8484
location.baseHref = 'https://example.com/foo/';
8585
location.pathname = '/foo/bar';
8686
final PathUrlStrategy strategy = PathUrlStrategy(location);
8787

88+
location.search = '?q=1';
89+
expect(strategy.getPath(), '/bar?q=1');
90+
91+
location.search = '?q=1&t=r';
92+
expect(strategy.getPath(), '/bar?q=1&t=r');
93+
94+
location.hash = '#fragment=1';
95+
expect(strategy.getPath(), '/bar?q=1&t=r');
96+
});
97+
98+
test('gets path correctly in the presence of query params and fragment', () {
99+
location.baseHref = 'https://example.com/foo/';
100+
location.pathname = '/foo/bar';
101+
final PathUrlStrategy strategy = PathUrlStrategy(location, true);
88102

89103
location.search = '?q=1';
90104
expect(strategy.getPath(), '/bar?q=1');
91105

92106
location.search = '?q=1&t=r';
93107
expect(strategy.getPath(), '/bar?q=1&t=r');
108+
109+
location.hash = '#fragment=1';
110+
expect(strategy.getPath(), '/bar?q=1&t=r#fragment=1');
94111
});
95112

96113
test('empty route name is ok', () {

0 commit comments

Comments
 (0)