@@ -5,6 +5,7 @@ import {TransitionHookFn} from "../transition/interface";
5
5
import { StateDeclaration , LazyLoadResult } from "../state/interface" ;
6
6
import { State } from "../state/stateObject" ;
7
7
import { services } from "../common/coreservices" ;
8
+ import { StateRule } from "../url/interface" ;
8
9
9
10
/**
10
11
* A [[TransitionHookFn]] that performs lazy loading
@@ -33,36 +34,32 @@ import {services} from "../common/coreservices";
33
34
const lazyLoadHook : TransitionHookFn = ( transition : Transition ) => {
34
35
const transitionSource = ( trans : Transition ) =>
35
36
trans . redirectedFrom ( ) ? transitionSource ( trans . redirectedFrom ( ) ) : trans . options ( ) . source ;
37
+ let router = transition . router ;
36
38
37
39
function retryOriginalTransition ( ) {
38
- if ( transitionSource ( transition ) === 'url' ) {
39
- let $loc = transition . router . urlService ,
40
- $reg = transition . router . stateRegistry ,
41
- path = $loc . path ( ) ,
42
- search = $loc . search ( ) ,
43
- hash = $loc . hash ( ) ;
44
-
45
- let matchState = state =>
46
- [ state , state . url && state . url . exec ( path , search , hash ) ] ;
47
-
48
- let matches = $reg . get ( )
49
- . map ( s => s . $$state ( ) )
50
- . map ( matchState )
51
- . filter ( ( [ state , params ] ) => ! ! params ) ;
52
-
53
- if ( matches . length ) {
54
- let [ state , params ] = matches [ 0 ] ;
55
- return transition . router . stateService . target ( state , params , transition . options ( ) ) ;
56
- }
40
+ if ( transitionSource ( transition ) !== 'url' ) {
41
+ // The original transition was not triggered via url sync
42
+ // The lazy state should be loaded now, so re-try the original transition
43
+ let orig = transition . targetState ( ) ;
44
+ return router . stateService . target ( orig . identifier ( ) , orig . params ( ) , orig . options ( ) ) ;
45
+ }
57
46
58
- transition . router . urlRouter . sync ( ) ;
59
- return ;
47
+ // The original transition was triggered via url sync
48
+ // Run the URL rules and find the best match
49
+ let $url = router . urlService ;
50
+ let result = $url . match ( $url . parts ( ) ) ;
51
+ let rule = result && result . rule ;
52
+
53
+ // If the best match is a state, redirect the transition (instead
54
+ // of calling sync() which supersedes the current transition)
55
+ if ( rule && rule . type === "STATE" ) {
56
+ let state = ( rule as StateRule ) . state ;
57
+ let params = result . match ;
58
+ return router . stateService . target ( state , params , transition . options ( ) ) ;
60
59
}
61
60
62
- // The original transition was not triggered via url sync
63
- // The lazy state should be loaded now, so re-try the original transition
64
- let orig = transition . targetState ( ) ;
65
- return transition . router . stateService . target ( orig . identifier ( ) , orig . params ( ) , orig . options ( ) ) ;
61
+ // No matching state found, so let .sync() choose the best non-state match/otherwise
62
+ router . urlRouter . sync ( ) ;
66
63
}
67
64
68
65
let promises = transition . entering ( )
0 commit comments