Visitar URL original
Access to fragment in router state on page refresh by vasilich6107 · Pull Request #131123 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Access to fragment in router state on page refresh
  • Loading branch information
vasilich6107 committed Sep 15, 2023
commit 680a013698bd7bc33c52668b34acff422d81d05d
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PathUrlStrategy extends ui_web.HashUrlStrategy {
/// interactions.
PathUrlStrategy([
super.platformLocation,
this.includeHash = false,
]) : _platformLocation = platformLocation,
_basePath = stripTrailingSlash(extractPathname(checkBaseHref(
platformLocation.getBaseHref(),
Expand All @@ -54,9 +55,20 @@ class PathUrlStrategy extends ui_web.HashUrlStrategy {
final ui_web.PlatformLocation _platformLocation;
final String _basePath;

/// There were an issue with url #hash which disappears from URL on first start of the web application
/// This flag allows to preserve that hash and was introduced mainly to preserve backward compatibility
/// with existing applications that rely on a full match on the path. If someone navigates to
/// /profile or /profile#foo, they both will work without this flag otherwise /profile#foo won't match
/// with the /profile route name anymore because the hash became part of the path.
///
/// This flag solves the edge cases when using auth provider which redirects back to the app with
/// token in redirect URL as /#access_token=bla_bla_bla
final bool includeHash;

@override
String getPath() {
final String path = _platformLocation.pathname + _platformLocation.search;
final String? hash = includeHash ? _platformLocation.hash : null;
final String path = _platformLocation.pathname + _platformLocation.search + (hash ?? '');
if (_basePath.isNotEmpty && path.startsWith(_basePath)) {
return ensureLeadingSlash(path.substring(_basePath.length));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ class PathUrlStrategy extends HashUrlStrategy {
///
/// The [PlatformLocation] parameter is useful for testing to mock out browser
/// integrations.
const PathUrlStrategy([PlatformLocation? _]);
const PathUrlStrategy([PlatformLocation? _, bool __ = false,]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,34 @@ void main() {
expect(strategy.getPath(), '/bar');
});

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

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

location.search = '?q=1&t=r';
expect(strategy.getPath(), '/bar?q=1&t=r');

location.hash = '#fragment=1';
expect(strategy.getPath(), '/bar?q=1&t=r');
});

test('gets path correctly in the presence of query params and fragment', () {
location.baseHref = 'https://example.com/foo/';
location.pathname = '/foo/bar';
final PathUrlStrategy strategy = PathUrlStrategy(location, true);

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

location.search = '?q=1&t=r';
expect(strategy.getPath(), '/bar?q=1&t=r');

location.hash = '#fragment=1';
expect(strategy.getPath(), '/bar?q=1&t=r#fragment=1');
});

test('empty route name is ok', () {
Expand Down