1- import { h , Fragment , createContext , cloneElement , toChildArray } from 'preact' ;
1+ import { h , createContext , cloneElement , toChildArray } from 'preact' ;
22import { useContext , useMemo , useReducer , useLayoutEffect , useRef } from 'preact/hooks' ;
33
44/**
@@ -117,6 +117,7 @@ export function Router(props) {
117117 const [ c , update ] = useReducer ( c => c + 1 , 0 ) ;
118118
119119 const { url, path, pathParams, searchParams, wasPush } = useLocation ( ) ;
120+ const { rest = path } = useContext ( RouterContext ) ;
120121
121122 const isLoading = useRef ( false ) ;
122123 const prevRoute = useRef ( path ) ;
@@ -136,7 +137,7 @@ export function Router(props) {
136137
137138 let pathRoute , defaultRoute , matchProps ;
138139 toChildArray ( props . children ) . some ( ( /** @type {VNode<any> } */ vnode ) => {
139- const matches = exec ( path , vnode . props . path , ( matchProps = { ...vnode . props , path, pathParams, searchParams } ) ) ;
140+ const matches = exec ( rest , vnode . props . path , ( matchProps = { ...vnode . props , path : rest , pathParams, searchParams } ) ) ;
140141 if ( matches ) return ( pathRoute = cloneElement ( vnode , matchProps ) ) ;
141142 if ( vnode . props . default ) defaultRoute = cloneElement ( vnode , matchProps ) ;
142143 } ) ;
@@ -149,7 +150,7 @@ export function Router(props) {
149150 const routeChanged = useMemo ( ( ) => {
150151 prev . current = cur . current ;
151152
152- cur . current = /** @type {VNode<any> } */ ( h ( Fragment , { key : path } , incoming ) ) ;
153+ cur . current = /** @type {VNode<any> } */ ( h ( RouterContext . Provider , { value : matchProps } , incoming ) ) ;
153154
154155 // Only mark as an update if the route component changed.
155156 const outgoing = prev . current && prev . current . props . children ;
@@ -263,6 +264,9 @@ Router.Provider = LocationProvider;
263264LocationProvider . ctx = createContext (
264265 /** @type {import('./router.d.ts').LocationHook & { wasPush: boolean } } */ ( { } )
265266) ;
267+ const RouterContext = createContext (
268+ /** @type {{ rest: string } } */ ( { } )
269+ ) ;
266270
267271export const Route = props => h ( props . component , props ) ;
268272
0 commit comments