mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
[web] Fix url updates when using Router (flutter/engine#24798)
This commit is contained in:
parent
4d2b114fe0
commit
ee8a5e9bd0
@ -44,13 +44,17 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow {
|
||||
/// button, etc.
|
||||
@visibleForTesting
|
||||
BrowserHistory get browserHistory {
|
||||
return _browserHistory ??=
|
||||
MultiEntriesBrowserHistory(urlStrategy: _urlStrategyForInitialization);
|
||||
}
|
||||
|
||||
UrlStrategy? get _urlStrategyForInitialization {
|
||||
final UrlStrategy? urlStrategy = _isUrlStrategySet
|
||||
? _customUrlStrategy
|
||||
: _createDefaultUrlStrategy();
|
||||
// Prevent any further customization of URL strategy.
|
||||
_isUrlStrategySet = true;
|
||||
return _browserHistory ??=
|
||||
MultiEntriesBrowserHistory(urlStrategy: urlStrategy);
|
||||
return urlStrategy;
|
||||
}
|
||||
|
||||
BrowserHistory? _browserHistory;
|
||||
@ -59,8 +63,14 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow {
|
||||
if (_browserHistory is SingleEntryBrowserHistory) {
|
||||
return;
|
||||
}
|
||||
final UrlStrategy? strategy = _browserHistory?.urlStrategy;
|
||||
await _browserHistory?.tearDown();
|
||||
|
||||
final UrlStrategy? strategy;
|
||||
if (_browserHistory == null) {
|
||||
strategy = _urlStrategyForInitialization;
|
||||
} else {
|
||||
strategy = _browserHistory?.urlStrategy;
|
||||
await _browserHistory?.tearDown();
|
||||
}
|
||||
_browserHistory = SingleEntryBrowserHistory(urlStrategy: strategy);
|
||||
}
|
||||
|
||||
@ -68,8 +78,14 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow {
|
||||
if (_browserHistory is MultiEntriesBrowserHistory) {
|
||||
return;
|
||||
}
|
||||
final UrlStrategy? strategy = _browserHistory?.urlStrategy;
|
||||
await _browserHistory?.tearDown();
|
||||
|
||||
final UrlStrategy? strategy;
|
||||
if (_browserHistory == null) {
|
||||
strategy = _urlStrategyForInitialization;
|
||||
} else {
|
||||
strategy = _browserHistory?.urlStrategy;
|
||||
await _browserHistory?.tearDown();
|
||||
}
|
||||
_browserHistory = MultiEntriesBrowserHistory(urlStrategy: strategy);
|
||||
}
|
||||
|
||||
@ -261,7 +277,7 @@ external set _jsSetUrlStrategy(_JsSetUrlStrategy? newJsSetUrlStrategy);
|
||||
|
||||
UrlStrategy? _createDefaultUrlStrategy() {
|
||||
return ui.debugEmulateFlutterTesterEnvironment
|
||||
? null
|
||||
? TestUrlStrategy.fromEntry(TestHistoryEntry('default', null, '/default'))
|
||||
: const HashUrlStrategy();
|
||||
}
|
||||
|
||||
|
||||
@ -128,6 +128,51 @@ void testMain() {
|
||||
expect(window.browserHistory.urlStrategy.getPath(), '/baz');
|
||||
});
|
||||
|
||||
test('initialize browser history with default url strategy (single)', () async {
|
||||
// On purpose, we don't initialize history on the window. We want to let the
|
||||
// window to self-initialize when it receives a navigation message.
|
||||
|
||||
Completer<void> callback = Completer<void>();
|
||||
window.sendPlatformMessage(
|
||||
'flutter/navigation',
|
||||
JSONMethodCodec().encodeMethodCall(MethodCall(
|
||||
'routeUpdated',
|
||||
<String, dynamic>{'routeName': '/bar'},
|
||||
)),
|
||||
(_) { callback.complete(); },
|
||||
);
|
||||
await callback.future;
|
||||
expect(window.browserHistory is SingleEntryBrowserHistory, true);
|
||||
// The url strategy should've been set to the default, and the path
|
||||
// should've been correctly set to "/bar".
|
||||
expect(window.browserHistory.urlStrategy, isNot(isNull));
|
||||
expect(window.browserHistory.urlStrategy.getPath(), '/bar');
|
||||
}, skip: browserEngine == BrowserEngine.webkit); // https://github.com/flutter/flutter/issues/50836
|
||||
|
||||
test('initialize browser history with default url strategy (multiple)', () async {
|
||||
// On purpose, we don't initialize history on the window. We want to let the
|
||||
// window to self-initialize when it receives a navigation message.
|
||||
|
||||
Completer<void> callback = Completer<void>();
|
||||
window.sendPlatformMessage(
|
||||
'flutter/navigation',
|
||||
JSONMethodCodec().encodeMethodCall(MethodCall(
|
||||
'routeInformationUpdated',
|
||||
<String, dynamic>{
|
||||
'location': '/baz',
|
||||
'state': null,
|
||||
},
|
||||
)),
|
||||
(_) { callback.complete(); },
|
||||
);
|
||||
await callback.future;
|
||||
expect(window.browserHistory is MultiEntriesBrowserHistory, true);
|
||||
// The url strategy should've been set to the default, and the path
|
||||
// should've been correctly set to "/baz".
|
||||
expect(window.browserHistory.urlStrategy, isNot(isNull));
|
||||
expect(window.browserHistory.urlStrategy.getPath(), '/baz');
|
||||
}, skip: browserEngine == BrowserEngine.webkit); // https://github.com/flutter/flutter/issues/50836
|
||||
|
||||
test('can disable location strategy', () async {
|
||||
// Disable URL strategy.
|
||||
expect(() => jsSetUrlStrategy(null), returnsNormally);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user