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/**
@@ -137,6 +137,7 @@ export function Router(props) {
137137 if ( ! url ) {
138138 throw new Error ( `preact-iso's <Router> must be used within a <LocationProvider>, see: https://github.com/preactjs/preact-iso#locationprovider` ) ;
139139 }
140+ const { rest = path } = useContext ( RouterContext ) ;
140141
141142 const isLoading = useRef ( false ) ;
142143 const prevRoute = useRef ( path ) ;
@@ -156,7 +157,7 @@ export function Router(props) {
156157
157158 let pathRoute , defaultRoute , matchProps ;
158159 toChildArray ( props . children ) . some ( ( /** @type {VNode<any> } */ vnode ) => {
159- const matches = exec ( path , vnode . props . path , ( matchProps = { ...vnode . props , path, pathParams, searchParams } ) ) ;
160+ const matches = exec ( rest , vnode . props . path , ( matchProps = { ...vnode . props , path : rest , pathParams, searchParams } ) ) ;
160161 if ( matches ) return ( pathRoute = cloneElement ( vnode , matchProps ) ) ;
161162 if ( vnode . props . default ) defaultRoute = cloneElement ( vnode , matchProps ) ;
162163 } ) ;
@@ -169,7 +170,7 @@ export function Router(props) {
169170 const routeChanged = useMemo ( ( ) => {
170171 prev . current = cur . current ;
171172
172- cur . current = /** @type {VNode<any> } */ ( h ( Fragment , { key : path } , incoming ) ) ;
173+ cur . current = /** @type {VNode<any> } */ ( h ( RouterContext . Provider , { value : matchProps } , incoming ) ) ;
173174
174175 // Only mark as an update if the route component changed.
175176 const outgoing = prev . current && prev . current . props . children ;
@@ -283,6 +284,9 @@ Router.Provider = LocationProvider;
283284LocationProvider . ctx = createContext (
284285 /** @type {import('./router.d.ts').LocationHook & { wasPush: boolean } } */ ( { } )
285286) ;
287+ const RouterContext = createContext (
288+ /** @type {{ rest: string } } */ ( { } )
289+ ) ;
286290
287291export const Route = props => h ( props . component , props ) ;
288292
0 commit comments