File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,9 @@ export function Router(props) {
118118 const [ c , update ] = useReducer ( c => c + 1 , 0 ) ;
119119
120120 const { url, query, wasPush, path } = useLocation ( ) ;
121+ if ( ! url ) {
122+ throw new Error ( `preact-iso's <Router> must be used within a <LocationProvider>, see: https://github.com/preactjs/preact-iso#locationprovider` ) ;
123+ }
121124 const { rest = path , params = { } } = useContext ( RouteContext ) ;
122125
123126 const isLoading = useRef ( false ) ;
Original file line number Diff line number Diff line change @@ -42,6 +42,22 @@ describe('Router', () => {
4242 } ) ;
4343
4444
45+ it ( 'should throw a clear error if the LocationProvider is missing' , ( ) => {
46+ const Home = ( ) => < h1 > Home</ h1 > ;
47+
48+ try {
49+ render (
50+ < Router >
51+ < Home path = "/" test = "2" />
52+ </ Router > ,
53+ scratch
54+ ) ;
55+ expect . fail ( 'should have thrown' ) ;
56+ } catch ( e ) {
57+ expect ( e . message ) . to . include ( 'must be used within a <LocationProvider>' ) ;
58+ }
59+ } ) ;
60+
4561 it ( 'should strip trailing slashes from path' , async ( ) => {
4662 render (
4763 < LocationProvider url = "/a/" >
@@ -953,7 +969,7 @@ describe('Router', () => {
953969 ) ;
954970
955971 shadowlink . click ( ) ;
956-
972+
957973 await sleep ( 1 ) ;
958974
959975 expect ( loc ) . to . deep . include ( { url : '/shadow' } ) ;
You can’t perform that action at this time.
0 commit comments