@@ -117,24 +117,33 @@ export function Router(props) {
117
117
// was the most recent render successful (did not suspend):
118
118
const didSuspend = useRef ( ) ;
119
119
didSuspend . current = false ;
120
-
121
- useMemo ( ( ) => {
122
- // This hack prevents Preact from diffing when we swap `cur` to `prev`:
123
- if ( this . __v && this . __v . __k ) this . __v . __k . reverse ( ) ;
124
-
125
- count . current ++ ;
126
- prev . current = cur . current ;
127
- } , [ url ] ) ;
120
+ // has the route component changed
121
+ const routeChanged = useRef ( false ) ;
128
122
129
123
let pr , d , m ;
130
124
toChildArray ( props . children ) . some ( vnode => {
131
125
const matches = exec ( rest , vnode . props . path , ( m = { ...vnode . props , path : rest , query, params, rest : '' } ) ) ;
132
126
if ( matches ) return ( pr = cloneElement ( vnode , m ) ) ;
133
127
if ( vnode . props . default ) d = cloneElement ( vnode , m ) ;
134
128
} ) ;
129
+
130
+ let incoming = pr || d ;
131
+ useMemo ( ( ) => {
132
+ prev . current = cur . current ;
133
+
134
+ // Only mark as an update if the route component changed.
135
+ const outgoing = prev . current && prev . current . props . children ;
136
+ if ( ! outgoing || ! incoming || incoming . type !== outgoing . type || incoming . props . component !== outgoing . props . component ) {
137
+ // This hack prevents Preact from diffing when we swap `cur` to `prev`:
138
+ if ( this . __v && this . __v . __k ) this . __v . __k . reverse ( ) ;
139
+ count . current ++ ;
140
+ routeChanged . current = true ;
141
+ } else routeChanged . current = false ;
142
+ } , [ url ] ) ;
143
+
135
144
const isHydratingSuspense = cur . current && cur . current . __u & MODE_HYDRATE && cur . current . __u & MODE_SUSPENDED ;
136
145
const isHydratingBool = cur . current && cur . current . __h ;
137
- cur . current = h ( RouteContext . Provider , { value : m } , pr || d ) ;
146
+ cur . current = h ( RouteContext . Provider , { value : m } , incoming ) ;
138
147
if ( isHydratingSuspense ) {
139
148
cur . current . __u |= MODE_HYDRATE ;
140
149
cur . current . __u |= MODE_SUSPENDED ;
@@ -220,7 +229,9 @@ export function Router(props) {
220
229
} , [ path , wasPush , c ] ) ;
221
230
222
231
// Note: curChildren MUST render first in order to set didSuspend & prev.
223
- return [ h ( RenderRef , { r : cur } ) , h ( RenderRef , { r : prev } ) ] ;
232
+ return routeChanged . current
233
+ ? [ h ( RenderRef , { r : cur } ) , h ( RenderRef , { r : prev } ) ]
234
+ : h ( RenderRef , { r : cur } ) ;
224
235
}
225
236
226
237
const MODE_HYDRATE = 1 << 5 ;
0 commit comments