From 81588f93673f14413ed90209e53d9135c4deb32c Mon Sep 17 00:00:00 2001 From: Ryan Christian Date: Wed, 21 May 2025 16:58:56 -0500 Subject: [PATCH] chore: Throw a clear error when the LocationProvider is missing --- src/router.js | 3 +++ test/router.test.js | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/router.js b/src/router.js index 4926f33..545d22c 100644 --- a/src/router.js +++ b/src/router.js @@ -118,6 +118,9 @@ export function Router(props) { const [c, update] = useReducer(c => c + 1, 0); const { url, query, wasPush, path } = useLocation(); + if (!url) { + throw new Error(`preact-iso's must be used within a , see: https://github.com/preactjs/preact-iso#locationprovider`); + } const { rest = path, params = {} } = useContext(RouteContext); const isLoading = useRef(false); diff --git a/test/router.test.js b/test/router.test.js index 5cd8e98..a652177 100644 --- a/test/router.test.js +++ b/test/router.test.js @@ -42,6 +42,22 @@ describe('Router', () => { }); + it('should throw a clear error if the LocationProvider is missing', () => { + const Home = () =>

Home

; + + try { + render( + + + , + scratch + ); + expect.fail('should have thrown'); + } catch (e) { + expect(e.message).to.include('must be used within a '); + } + }); + it('should strip trailing slashes from path', async () => { render( @@ -953,7 +969,7 @@ describe('Router', () => { ); shadowlink.click(); - + await sleep(1); expect(loc).to.deep.include({ url: '/shadow' });